summaryrefslogtreecommitdiff
path: root/odoo/tests
diff options
context:
space:
mode:
authorRaphael Collet <rco@odoo.com>2020-05-05 12:55:52 +0000
committerRaphael Collet <rco@odoo.com>2020-05-20 07:56:14 +0000
commit020536142cf9fe9124675ef5c7e46eaf41b9c528 (patch)
tree4a043e8755f59ed89b68ff85a682ea3f573bc054 /odoo/tests
parente3be55d3f6d3ee8145e182fd54185115f6699bb6 (diff)
[IMP] core: better tests on queries made by search
Diffstat (limited to 'odoo/tests')
-rw-r--r--odoo/tests/common.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/odoo/tests/common.py b/odoo/tests/common.py
index d8ea680415c..51fedd67173 100644
--- a/odoo/tests/common.py
+++ b/odoo/tests/common.py
@@ -40,6 +40,7 @@ from xmlrpc import client as xmlrpclib
from odoo.models import BaseModel
from odoo.osv.expression import normalize_domain, TRUE_LEAF, FALSE_LEAF
+from odoo.sql_db import Cursor
from odoo.tools import float_compare, single_email_re
from odoo.tools.misc import find_in_path
from odoo.tools.safe_eval import safe_eval
@@ -380,6 +381,37 @@ class BaseCase(TreeCase, MetaCase('DummyCase', (object,), {})):
return self._assertRaises(exception, **kwargs)
@contextmanager
+ def assertQueries(self, expected):
+ """ Check the queries made by the current cursor. ``expected`` is a list
+ of strings representing the expected queries being made. Query strings
+ are matched against each other, ignoring case and whitespaces.
+ """
+ Cursor_execute = Cursor.execute
+ actual_queries = []
+
+ def execute(self, query, params=None, log_exceptions=None):
+ actual_queries.append(query)
+ return Cursor_execute(self, query, params, log_exceptions)
+
+ def get_unaccent_wrapper(cr):
+ return lambda x: x
+
+ with patch('odoo.sql_db.Cursor.execute', execute):
+ with patch('odoo.osv.expression.get_unaccent_wrapper', get_unaccent_wrapper):
+ yield actual_queries
+
+ self.assertEqual(
+ len(actual_queries), len(expected),
+ "%d queries done, %d expected" % (len(actual_queries), len(expected)),
+ )
+ for actual_query, expect_query in zip(actual_queries, expected):
+ self.assertEqual(
+ "".join(actual_query.lower().split()),
+ "".join(expect_query.lower().split()),
+ "\n---- actual query:\n%s\n---- not like:\n%s" % (actual_query, expect_query),
+ )
+
+ @contextmanager
def assertQueryCount(self, default=0, flush=True, **counters):
""" Context manager that counts queries. It may be invoked either with
one value, or with a set of named arguments like ``login=value``::