jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/363749 )

Change subject: QueryBuilder: fix subgroup close at the end of query
......................................................................


QueryBuilder: fix subgroup close at the end of query

* When a query was having subgroups that were closed at the end of the
  query, QueryBuilder was not calling the close_subgroup() method of the
  related backend as it should have. For example in a query like:
    host1* and (R:Class = Foo or R:Class = Bar)

Change-Id: I53c7aa66666cb61f1ebbfa217fadce326c8af39c
---
M cumin/query.py
M cumin/tests/unit/test_query.py
2 files changed, 33 insertions(+), 0 deletions(-)

Approvals:
  Giuseppe Lavagetto: Looks good to me, but someone else must approve
  jenkins-bot: Verified
  Volans: Looks good to me, approved



diff --git a/cumin/query.py b/cumin/query.py
index a816882..bf2ff59 100644
--- a/cumin/query.py
+++ b/cumin/query.py
@@ -76,6 +76,10 @@
             if not self._replace_alias(token_dict, level):
                 self._build_token(token_dict, level)
 
+        while self.level > level:
+            self.query.close_subgroup()
+            self.level -= 1
+
     def _build_token(self, token_dict, level):
         """Build a token into the query object for the configured backend.
 
diff --git a/cumin/tests/unit/test_query.py b/cumin/tests/unit/test_query.py
index aefe8b8..7ea6a08 100644
--- a/cumin/tests/unit/test_query.py
+++ b/cumin/tests/unit/test_query.py
@@ -92,6 +92,7 @@
             mock.call(category='R', key='key2', operator='~', value='regex')])
         query_builder.query.add_and.assert_called_once_with()
         query_builder.query.close_subgroup.assert_called_once_with()
+        assert query_builder.level == 0
 
     @mock.patch('cumin.query.Query', QueryFactory)
     def test_build_valid_with_aliases(self):
@@ -105,6 +106,7 @@
         query_builder.query.add_or.assert_has_calls([mock.call(), mock.call(), 
mock.call()])
         query_builder.query.open_subgroup.assert_called_once_with()
         query_builder.query.close_subgroup.assert_called_once_with()
+        assert query_builder.level == 0
 
     @mock.patch('cumin.query.Query', QueryFactory)
     def test_build_valid_with_nested_aliases(self):  # pylint: 
disable=invalid-name
@@ -117,6 +119,8 @@
              mock.call(hosts=NodeSet('host1')), 
mock.call(hosts=NodeSet('host10[10-22]'))])
         query_builder.query.add_or.assert_has_calls([mock.call(), mock.call()])
         query_builder.query.open_subgroup.assert_has_calls([mock.call(), 
mock.call()])
+        query_builder.query.close_subgroup.assert_has_calls([mock.call(), 
mock.call()])
+        assert query_builder.level == 0
 
     @mock.patch('cumin.query.Query', QueryFactory)
     def test_build_invalid_alias_syntax(self):
@@ -138,6 +142,7 @@
         query_builder = QueryBuilder('host1*', self.config)
         query_builder.build()
         
query_builder.query.add_hosts.assert_called_once_with(hosts=NodeSet('host1*'))
+        assert query_builder.level == 0
 
     @mock.patch('cumin.query.Query', QueryFactory)
     def test_build_invalid(self):
@@ -147,6 +152,30 @@
             query_builder.build()
 
     @mock.patch('cumin.query.Query', QueryFactory)
+    def test_build_subgroup(self):
+        """QueryBuilder.build() should open and close a subgroup properly."""
+        query_builder = QueryBuilder('(host1)', self.config)
+        query_builder.build()
+
+        
query_builder.query.add_hosts.assert_has_calls([mock.call(hosts=NodeSet('host1'))])
+        query_builder.query.open_subgroup.assert_called_once_with()
+        query_builder.query.close_subgroup.assert_called_once_with()
+        assert query_builder.level == 0
+
+    @mock.patch('cumin.query.Query', QueryFactory)
+    def test_build_subgroups(self):
+        """QueryBuilder.build() should open and close multiple subgroups 
properly."""
+        query_builder = QueryBuilder('(host1 or (host2 or host3))', 
self.config)
+        query_builder.build()
+
+        query_builder.query.add_hosts.assert_has_calls(
+            [mock.call(hosts=NodeSet('host1')), 
mock.call(hosts=NodeSet('host2')),
+             mock.call(hosts=NodeSet('host3'))])
+        query_builder.query.open_subgroup.assert_has_calls([mock.call(), 
mock.call()])
+        query_builder.query.close_subgroup.assert_has_calls([mock.call(), 
mock.call()])
+        assert query_builder.level == 0
+
+    @mock.patch('cumin.query.Query', QueryFactory)
     def test__parse_token(self):
         """QueryBuilder._parse_token() should raise RuntimeError for an 
invalid token."""
         query_builder = QueryBuilder(self.invalid_query_string, self.config)

-- 
To view, visit https://gerrit.wikimedia.org/r/363749
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I53c7aa66666cb61f1ebbfa217fadce326c8af39c
Gerrit-PatchSet: 1
Gerrit-Project: operations/software/cumin
Gerrit-Branch: master
Gerrit-Owner: Volans <[email protected]>
Gerrit-Reviewer: Faidon Liambotis <[email protected]>
Gerrit-Reviewer: Gehel <[email protected]>
Gerrit-Reviewer: Giuseppe Lavagetto <[email protected]>
Gerrit-Reviewer: Volans <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to