From e1ef96f05b6c7fb98ded16ddf9e4c7e9898b0cf3 Mon Sep 17 00:00:00 2001 From: Itai Shirav Date: Tue, 4 Apr 2017 14:41:23 +0300 Subject: [PATCH] Add a test for "with totals" --- src/infi/clickhouse_orm/utils.py | 9 +-------- tests/test_database.py | 9 +++++++++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/infi/clickhouse_orm/utils.py b/src/infi/clickhouse_orm/utils.py index bdaca46..f5b5b22 100644 --- a/src/infi/clickhouse_orm/utils.py +++ b/src/infi/clickhouse_orm/utils.py @@ -38,14 +38,7 @@ def unescape(value): def parse_tsv(line): if PY3 and isinstance(line, binary_type): line = line.decode() - # GROUP BY WITH TOTALS modifier contains a blank line (second last row) - # separating normal result from the totals (last row) - # Let's skip the blank line - try: - last_element = line[-1] - except IndexError: - return - if last_element == '\n': + if line and line[-1] == '\n': line = line[:-1] return [unescape(value) for value in line.split('\t')] diff --git a/tests/test_database.py b/tests/test_database.py index 62b340b..1897d8f 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -89,6 +89,15 @@ class DatabaseTestCase(unittest.TestCase): self.assertEqual(results[0].get_database(), self.database) self.assertEqual(results[1].get_database(), self.database) + def test_select_with_totals(self): + self._insert_and_check(self._sample_data(), len(data)) + query = "SELECT last_name, sum(height) as height FROM `test-db`.person GROUP BY last_name WITH TOTALS" + results = list(self.database.select(query)) + total = sum(r.height for r in results[:-1]) + # Last line has an empty last name, and total of all heights + self.assertFalse(results[-1].last_name) + self.assertEquals(total, results[-1].height) + def test_pagination(self): self._insert_and_check(self._sample_data(), len(data)) # Try different page sizes