From 6261fa486fe81b5ab09e438d8200ad68b51938bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 27 Sep 2016 12:18:16 +0200 Subject: [PATCH] Improve ModelStorage.search_count by using no order and COUNT(*) issue5877 review27651003 --- trytond/model/modelsql.py | 2 +- trytond/model/modelstorage.py | 2 +- trytond/tests/test_modelstorage.py | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/trytond/model/modelsql.py b/trytond/model/modelsql.py index 246e187493..2f07e807c7 100644 --- a/trytond/model/modelsql.py +++ b/trytond/model/modelsql.py @@ -1136,7 +1136,7 @@ class ModelSQL(ModelStorage): table = convert_from(None, tables) if count: - cursor.execute(*table.select(Count(Literal(1)), + cursor.execute(*table.select(Count(Literal('*')), where=expression, limit=limit, offset=offset)) return cursor.fetchone()[0] # execute the "main" query to fetch the ids we were searching for diff --git a/trytond/model/modelstorage.py b/trytond/model/modelstorage.py index aa25c0f37c..9a5fd5d489 100644 --- a/trytond/model/modelstorage.py +++ b/trytond/model/modelstorage.py @@ -366,7 +366,7 @@ class ModelStorage(Model): ''' Return the number of records that match the domain. ''' - res = cls.search(domain, count=True) + res = cls.search(domain, order=[], count=True) if isinstance(res, list): return len(res) return res diff --git a/trytond/tests/test_modelstorage.py b/trytond/tests/test_modelstorage.py index 72868ec753..88d979b37c 100644 --- a/trytond/tests/test_modelstorage.py +++ b/trytond/tests/test_modelstorage.py @@ -36,6 +36,19 @@ class ModelStorageTestCase(unittest.TestCase): self.assertTrue( all(x['name'] >= y['name'] for x, y in zip(rows, rows[1:]))) + @with_transaction() + def test_search_count(self): + "Test search_count" + pool = Pool() + ModelStorage = pool.get('test.modelstorage') + ModelStorage.create([{'name': 'Test %s' % i} for i in range(10)]) + + count = ModelStorage.search_count([]) + self.assertEqual(count, 10) + + count = ModelStorage.search_count([('name', '=', 'Test 5')]) + self.assertEqual(count, 1) + @with_transaction() def test_browse_context(self): 'Test context when browsing'