Skip to content

Commit f41c2ba

Browse files
committed
[IMP] base: Clear precommits on test cleanup
Precommit hooks would stock data until a call to ``cr.flush`` was made. Notably, this happens when the ``assertRaises`` method is called. Functions were applied on records already cleared from the cache. This change adds a cleanup call for `TransactionCase` as it keeps the same cursor for all tests. Cursor precommits can now be safely executed inside tests. Task-2834304 closes odoo#118011 X-original-commit: 4d2fdba Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com> Signed-off-by: Thiry Renaud (reth) <reth@odoo.com>
1 parent 0472569 commit f41c2ba

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

odoo/addons/base/tests/test_db_cursor.py

+24
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,27 @@ def test_hooks_on_testcursor(self):
214214
self.prepare_hooks(cr)
215215
cr.close()
216216
self.assertEqual(self.log, ['preR', 'postR'])
217+
218+
class TestCursorHooksTransactionCaseCleanup(common.TransactionCase):
219+
"""Check savepoint cases handle commit hooks properly."""
220+
def test_isolation_first(self):
221+
def mutate_second_test_ref():
222+
for name in ['precommit', 'postcommit', 'prerollback', 'postrollback']:
223+
del self.env.cr.precommit.data.get(f'test_cursor_hooks_savepoint_case_cleanup_test_second_{name}', [''])[0]
224+
self.env.cr.precommit.add(mutate_second_test_ref)
225+
226+
def test_isolation_second(self):
227+
references = [['not_empty']] * 4
228+
cr = self.env.cr
229+
commit_callbacks = [cr.precommit, cr.postcommit, cr.prerollback, cr.postrollback]
230+
callback_names = ['precommit', 'postcommit', 'prerollback', 'postrollback']
231+
232+
for callback_name, callbacks, reference in zip(callback_names, commit_callbacks, references):
233+
callbacks.data.setdefault(f"test_cursor_hooks_savepoint_case_cleanup_test_second_{callback_name}", reference)
234+
235+
for callback in commit_callbacks:
236+
callback.run()
237+
238+
for callback_name, reference in zip(callback_names, references):
239+
self.assertTrue(bool(reference), f"{callback_name} failed to clean up between transaction tests")
240+
self.assertTrue(reference[0] == 'not_empty', f"{callback_name} failed to clean up between transaction tests")

odoo/tests/common.py

+11
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,17 @@ def setUp(self):
833833

834834
self.addCleanup(self.registry.clear_caches)
835835

836+
# This prevents precommit functions and data from piling up
837+
# until cr.flush is called in 'assertRaises' clauses
838+
# (these are not cleared in self.env.clear or envs.clear)
839+
cr = self.env.cr
840+
841+
def _reset(cb, funcs, data):
842+
cb._funcs = funcs
843+
cb.data = data
844+
for callback in [cr.precommit, cr.postcommit, cr.prerollback, cr.postrollback]:
845+
self.addCleanup(_reset, callback, collections.deque(callback._funcs), dict(callback.data))
846+
836847
# flush everything in setUpClass before introducing a savepoint
837848
self.env['base'].flush()
838849

0 commit comments

Comments
 (0)