summaryrefslogtreecommitdiff
path: root/odoo/addons/base/tests/test_db_cursor.py
diff options
context:
space:
mode:
authorLaurent Mignon (ACSONE) <laurent.mignon@acsone.eu>2020-07-10 10:43:38 +0000
committerRaphael Collet <rco@odoo.com>2020-10-20 08:43:02 +0000
commit09bf7bd760136ddf55fc7e695a4b6c08d5fc4fed (patch)
tree7256c2776168a67c945e17fd4965205c09291354 /odoo/addons/base/tests/test_db_cursor.py
parent436a94c0335120c2e6f0a91b5c8247e0a787e3a8 (diff)
[FIX] core: Ensure that rollback hooks are called when the cursor is closed
Before this change the rollback hooks were never called since rollback() was never explicitely called by the framework. At the end of a transaction, if no error occurs the famework call commit() on the odoo cursor. In all cases, the transaction ends with a call to close(). Into the implementation of _close() rollback() is called on the underlying connection to ensure that not committed changes are rollbacked. That's the reason why despite the fact that rollback() was not called on the Odoo cursor, changes are not committed into the db in case of exception. To keep the same behaviour and avoid to have to explicitely call rollback() on the odoo cursor to trigger the execution of registered rollback hooks, these hooks are now processed in _close(). Since the list of registered hooks is emptied if commit() is called, we are sure that rollback hooks are only executed in case of rollback. OPW #2294911 closes odoo/odoo#60334 X-original-commit: dce9a05f3a5d37fab711c8ca3c5444941f98e814 Signed-off-by: Raphael Collet (rco) <rco@openerp.com> Co-authored-by: Raphael Collet <rco@odoo.com>
Diffstat (limited to 'odoo/addons/base/tests/test_db_cursor.py')
-rw-r--r--odoo/addons/base/tests/test_db_cursor.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/odoo/addons/base/tests/test_db_cursor.py b/odoo/addons/base/tests/test_db_cursor.py
index 37026cc967a..648e0f04beb 100644
--- a/odoo/addons/base/tests/test_db_cursor.py
+++ b/odoo/addons/base/tests/test_db_cursor.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
+from functools import partial
import odoo
from odoo.sql_db import TestCursor
@@ -115,3 +116,60 @@ class TestTestCursor(common.TransactionCase):
self.cr.rollback()
self.check(self.record, 'A')
+
+
+class TestCursorHooks(common.TransactionCase):
+ def setUp(self):
+ super().setUp()
+ self.log = []
+
+ def prepare_hooks(self, cr, precommit_msg, postcommit_msg, prerollback_msg, postrollback_msg):
+ cr.precommit.add(partial(self.log.append, precommit_msg))
+ cr.postcommit.add(partial(self.log.append, postcommit_msg))
+ cr.prerollback.add(partial(self.log.append, prerollback_msg))
+ cr.postrollback.add(partial(self.log.append, postrollback_msg))
+
+ def test_hooks(self):
+ cr = self.registry.cursor()
+
+ # check hook on commit()
+ self.prepare_hooks(cr, 'C1a', 'C1b', 'R1a', 'R1b')
+ self.assertEqual(self.log, [])
+ cr.commit()
+ self.assertEqual(self.log, ['C1a', 'C1b'])
+
+ # check hook on rollback()
+ self.prepare_hooks(cr, 'C2a', 'C2b', 'R2a', 'R2b')
+ self.assertEqual(self.log, ['C1a', 'C1b'])
+ cr.rollback()
+ self.assertEqual(self.log, ['C1a', 'C1b', 'R2a', 'R2b'])
+
+ # check hook on close()
+ self.prepare_hooks(cr, 'C3a', 'C3b', 'R3a', 'R3b')
+ self.assertEqual(self.log, ['C1a', 'C1b', 'R2a', 'R2b'])
+ cr.close()
+ self.assertEqual(self.log, ['C1a', 'C1b', 'R2a', 'R2b', 'R3a', 'R3b'])
+
+ def test_hooks_on_testcursor(self):
+ self.registry.enter_test_mode(self.cr)
+ self.addCleanup(self.registry.leave_test_mode)
+
+ cr = self.registry.cursor()
+
+ # check hook on commit(); post-commit hooks are ignored
+ self.prepare_hooks(cr, 'C1a', 'C1b', 'R1a', 'R1b')
+ self.assertEqual(self.log, [])
+ cr.commit()
+ self.assertEqual(self.log, ['C1a'])
+
+ # check hook on rollback()
+ self.prepare_hooks(cr, 'C2a', 'C2b', 'R2a', 'R2b')
+ self.assertEqual(self.log, ['C1a'])
+ cr.rollback()
+ self.assertEqual(self.log, ['C1a', 'R2a', 'R2b'])
+
+ # check hook on close()
+ self.prepare_hooks(cr, 'C3a', 'C3b', 'R3a', 'R3b')
+ self.assertEqual(self.log, ['C1a', 'R2a', 'R2b'])
+ cr.close()
+ self.assertEqual(self.log, ['C1a', 'R2a', 'R2b', 'R3a', 'R3b'])