diff --git a/addons/test_mail_full/tests/test_rating.py b/addons/test_mail_full/tests/test_rating.py index d0a16a30ecb..17b91df7f68 100644 --- a/addons/test_mail_full/tests/test_rating.py +++ b/addons/test_mail_full/tests/test_rating.py @@ -184,7 +184,7 @@ class TestRatingPerformance(TestRatingCommon): @users('employee') @warmup def test_rating_last_value_perfs_with_rating_mixin(self): - with self.assertQueryCount(employee=1312): # tmf 1312 / com 1312 + with self.assertQueryCount(employee=1319): # tmf 1319 / com 1319 self.create_ratings('mail.test.rating') with self.assertQueryCount(employee=2004): # tmf 2004 diff --git a/odoo/addons/base/tests/test_db_cursor.py b/odoo/addons/base/tests/test_db_cursor.py index 985ff228783..eefb9ebce60 100644 --- a/odoo/addons/base/tests/test_db_cursor.py +++ b/odoo/addons/base/tests/test_db_cursor.py @@ -255,3 +255,27 @@ class TestCursorHooks(common.TransactionCase): self.prepare_hooks(cr) cr.close() self.assertEqual(self.log, ['preR', 'postR']) + +class TestCursorHooksTransactionCaseCleanup(common.TransactionCase): + """Check savepoint cases handle commit hooks properly.""" + def test_isolation_first(self): + def mutate_second_test_ref(): + for name in ['precommit', 'postcommit', 'prerollback', 'postrollback']: + del self.env.cr.precommit.data.get(f'test_cursor_hooks_savepoint_case_cleanup_test_second_{name}', [''])[0] + self.env.cr.precommit.add(mutate_second_test_ref) + + def test_isolation_second(self): + references = [['not_empty']] * 4 + cr = self.env.cr + commit_callbacks = [cr.precommit, cr.postcommit, cr.prerollback, cr.postrollback] + callback_names = ['precommit', 'postcommit', 'prerollback', 'postrollback'] + + for callback_name, callbacks, reference in zip(callback_names, commit_callbacks, references): + callbacks.data.setdefault(f"test_cursor_hooks_savepoint_case_cleanup_test_second_{callback_name}", reference) + + for callback in commit_callbacks: + callback.run() + + for callback_name, reference in zip(callback_names, references): + self.assertTrue(bool(reference), f"{callback_name} failed to clean up between transaction tests") + self.assertTrue(reference[0] == 'not_empty', f"{callback_name} failed to clean up between transaction tests") diff --git a/odoo/tests/common.py b/odoo/tests/common.py index 9c6594d889b..af909161de5 100644 --- a/odoo/tests/common.py +++ b/odoo/tests/common.py @@ -28,7 +28,7 @@ import time import unittest from . import case import warnings -from collections import defaultdict +from collections import defaultdict, deque from concurrent.futures import Future, CancelledError, wait try: from concurrent.futures import InvalidStateError @@ -735,6 +735,17 @@ class TransactionCase(BaseCase): self.addCleanup(self.registry.clear_caches) + # This prevents precommit functions and data from piling up + # until cr.flush is called in 'assertRaises' clauses + # (these are not cleared in self.env.clear or envs.clear) + cr = self.env.cr + + def _reset(cb, funcs, data): + cb._funcs = funcs + cb.data = data + for callback in [cr.precommit, cr.postcommit, cr.prerollback, cr.postrollback]: + self.addCleanup(_reset, callback, deque(callback._funcs), dict(callback.data)) + # flush everything in setUpClass before introducing a savepoint self.env.flush_all()