From 79dd7b9423dd00e0489880e1e2dc52418c6c00be Mon Sep 17 00:00:00 2001 From: Yu Ming Zhu Date: Nov 28 2019 12:13:39 +0000 Subject: [PATCH 1/2] fix countOnly for group sql fixes: #1844 --- diff --git a/hub/kojihub.py b/hub/kojihub.py index aae60c5..3cfba91 100644 --- a/hub/kojihub.py +++ b/hub/kojihub.py @@ -8673,7 +8673,9 @@ SELECT %(col_str)s %(limit_str)s """ if self.opts.get('countOnly'): - if self.opts.get('offset') or self.opts.get('limit'): + if self.opts.get('offset') \ + or self.opts.get('limit') \ + or (self.enable_group and self.opts.get('group')): # If we're counting with an offset and/or limit, we need # to wrap the offset/limited query and then count the results, # rather than trying to offset/limit the single row returned @@ -8699,7 +8701,9 @@ SELECT %(col_str)s query = query % locals() if self.opts.get('countOnly') and \ - (self.opts.get('offset') or self.opts.get('limit')): + (self.opts.get('offset') or + self.opts.get('limit') or + (self.enable_group and self.opts.get('group'))): query = 'SELECT count(*)\nFROM (' + query + ') numrows' if self.opts.get('rowlock'): query += '\n FOR UPDATE' From 309ecc5378fd3717a93df6faa0ec8e8533709277 Mon Sep 17 00:00:00 2001 From: Yu Ming Zhu Date: Nov 28 2019 12:20:13 +0000 Subject: [PATCH 2/2] add tests for group and countOnly for QueryProcessor --- diff --git a/tests/test_hub/test_query_processor.py b/tests/test_hub/test_query_processor.py index 64eb5bb..570b267 100644 --- a/tests/test_hub/test_query_processor.py +++ b/tests/test_hub/test_query_processor.py @@ -75,6 +75,7 @@ class TestQueryProcessor(unittest.TestCase): " ORDER BY something OFFSET 10 LIMIT 3" self.assertEqual(actual, expected) + @mock.patch('kojihub.context') def test_simple_with_execution(self, context): cursor = mock.MagicMock() @@ -95,6 +96,18 @@ class TestQueryProcessor(unittest.TestCase): cursor.execute.assert_called_once_with('\nSELECT count(*)\n FROM awesome\n\n\n \n \n\n \n', {}) self.assertEqual(results, 'some count') + cursor.reset_mock() + args['opts']['group'] = 'id' + args['enable_group'] = True + proc = kojihub.QueryProcessor(**args) + results = proc.execute() + cursor.execute.assert_called_once_with( + 'SELECT count(*)\nFROM (\nSELECT 1\n' + ' FROM awesome\n\n\n GROUP BY id\n \n\n \n) numrows', {}) + self.assertEqual(results, 'some count') + + + @mock.patch('kojihub.context') def test_simple_execution_with_iterate(self, context): cursor = mock.MagicMock()