From 3fe0ee5ae691e18e3512bbf55983c2fda765829d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Mon, 9 Apr 2018 15:28:55 +0200 Subject: [PATCH] [IMP] tests: allow to add a margin on performance tests Purpose of this commit is to avoid errors for some performance tests that have a slightly variable number of queries. Indeed some tests in test_mail module vary of 1 or 2 queries due to a behavior still not found that does not seem deterministic. This should not be considered as a real error, but should not be considered as completely valid. This is why a margin is accepted in some tests that will raise a warning instead of an error. This warning tells people to check if they have increased the query counter or if it is due to a random effect in query count. --- odoo/tests/common.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/odoo/tests/common.py b/odoo/tests/common.py index 79b1d75f625..17ee4a29bf5 100644 --- a/odoo/tests/common.py +++ b/odoo/tests/common.py @@ -192,7 +192,7 @@ class BaseCase(TreeCase, MetaCase('DummyCase', (object,), {})): return self._assertRaises(exception) @contextmanager - def assertQueryCount(self, default=0, **counters): + def assertQueryCount(self, default=0, margin=0, **counters): """ Context manager that counts queries. It may be invoked either with one value, or with a set of named arguments like ``login=value``:: @@ -210,13 +210,17 @@ class BaseCase(TreeCase, MetaCase('DummyCase', (object,), {})): count0 = self.cr.sql_log_count yield count = self.cr.sql_log_count - count0 - if not count <= expected: - msg = "Query count for user %s: got %d instead of %d" - self.fail(msg % (login, count, expected)) + if count > (expected + margin): + msg = "Too much query count: user %s: got %d instead of %d (margin %s)" + self.fail(msg % (login, count, expected, margin)) + elif count > expected and count <= (expected + margin): + logger = logging.getLogger(type(self).__module__) + msg = "Query count greater but still in margin : user %s: got %d instead of %d (margin %s)" + logger.warn(msg, login, count, expected, margin) elif count < expected: logger = logging.getLogger(type(self).__module__) - msg = "Query count for user %s: got %d instead of %d" - logger.info(msg, login, count, expected) + msg = "Better query count: user %s: got %d instead of %d (margin %s)" + logger.info(msg, login, count, expected, margin) else: yield