From f2735e6d71d3f74356ab53bf590f790c261e5fee Mon Sep 17 00:00:00 2001 From: SparkDrago05 Date: Sat, 17 Oct 2026 10:25:49 +0500 Subject: [PATCH] [FIX] base_exception: make the swallow test helper run the test swallow_base_exception_error returned its inner wrapper without calling it, so every test it decorated passed without running its body. - Call the test inside the helper, so tests in other modules that use it run again. - Add expect_base_exception_error, which checks that BaseExceptionError is raised without the savepoint rollback of assertRaises, so the exceptions stored by the detection can still be checked. - Rewrite the six decorated base_exception tests with it. Their bodies now run to the end. Since the rollback change, a detected exception raises BaseExceptionError instead of ValidationError, so their expectations are updated. Fixes #3784 Signed-off-by: SparkDrago05 --- base_exception/tests/common.py | 24 ++++++++--- base_exception/tests/test_base_exception.py | 44 +++++++++++---------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/base_exception/tests/common.py b/base_exception/tests/common.py index 47a928a09de..c704a1db7ae 100644 --- a/base_exception/tests/common.py +++ b/base_exception/tests/common.py @@ -14,13 +14,25 @@ @decorator def swallow_base_exception_error(func, self): - def wrapper(*args, **kwargs): - try: - return func(*args, **kwargs) - except BaseExceptionError: - return None + try: + return func(self) + except BaseExceptionError: + return None + - return wrapper +@contextmanager +def expect_base_exception_error(testcase): + """Expect a BaseExceptionError raised by detect_exceptions(). + + Unlike assertRaises, this does not roll back to a savepoint: in tests the + exceptions are written with the test cursor, and a rollback would remove + them before they can be checked. + """ + try: + yield + except BaseExceptionError: + return + testcase.fail("BaseExceptionError not raised") @contextmanager diff --git a/base_exception/tests/test_base_exception.py b/base_exception/tests/test_base_exception.py index cbc9bed14ac..de15ab09449 100644 --- a/base_exception/tests/test_base_exception.py +++ b/base_exception/tests/test_base_exception.py @@ -8,14 +8,14 @@ from odoo import SUPERUSER_ID from odoo.api import Environment -from odoo.exceptions import UserError, ValidationError +from odoo.exceptions import UserError from odoo.tests import TransactionCase from ..exceptions import BaseExceptionError from .common import ( + expect_base_exception_error, mock_base_exception_method_env, patch_base_exception_method_env, - swallow_base_exception_error, ) @@ -74,15 +74,14 @@ def test_exception_rule_confirm(self): self.assertFalse(self.exception_rule_confirm.exception_ids) @patch_base_exception_method_env - @swallow_base_exception_error def test_fail_by_py(self): - with self.assertRaises(ValidationError): + with expect_base_exception_error(self): self.po.button_confirm() - self.po.with_context(raise_exception=False).button_confirm() + with expect_base_exception_error(self): + self.po.with_context(raise_exception=False).button_confirm() self.assertTrue(self.po.exception_ids) @patch_base_exception_method_env - @swallow_base_exception_error def test_fail_by_domain(self): self.exception_rule.write( { @@ -90,13 +89,13 @@ def test_fail_by_domain(self): "exception_type": "by_domain", } ) - with self.assertRaises(ValidationError): + with expect_base_exception_error(self): self.po.button_confirm() - self.po.with_context(raise_exception=False).button_confirm() + with expect_base_exception_error(self): + self.po.with_context(raise_exception=False).button_confirm() self.assertTrue(self.po.exception_ids) @patch_base_exception_method_env - @swallow_base_exception_error def test_fail_by_method(self): self.exception_rule.write( { @@ -104,18 +103,19 @@ def test_fail_by_method(self): "exception_type": "by_method", } ) - with self.assertRaises(ValidationError): + with expect_base_exception_error(self): self.po.button_confirm() - self.po.with_context(raise_exception=False).button_confirm() + with expect_base_exception_error(self): + self.po.with_context(raise_exception=False).button_confirm() self.assertTrue(self.po.exception_ids) @patch_base_exception_method_env - @swallow_base_exception_error def test_ignorable_exception(self): # Block because of exception during validation - with self.assertRaises(ValidationError): + with expect_base_exception_error(self): self.po.button_confirm() - self.po.with_context(raise_exception=False).button_confirm() + with expect_base_exception_error(self): + self.po.with_context(raise_exception=False).button_confirm() # Test that we have linked exceptions self.assertTrue(self.po.exception_ids) # Test ignore exeception make possible for the po to validate @@ -147,28 +147,30 @@ def test_purchase_check_button_cancel(self): self.assertEqual(self.po.state, "cancel") @patch_base_exception_method_env - @swallow_base_exception_error def test_detect_exceptions(self): - self.po.detect_exceptions() + with expect_base_exception_error(self): + self.po.detect_exceptions() + self.assertEqual(self.po.exception_ids, self.exception_rule) @patch_base_exception_method_env - @swallow_base_exception_error def test_blocking_exception(self): self.exception_rule.is_blocking = True # Block because of exception during validation - with self.assertRaises(ValidationError): + with expect_base_exception_error(self): self.po.button_confirm() # Test that we have linked exceptions - self.po.with_context(raise_exception=False).button_confirm() + with expect_base_exception_error(self): + self.po.with_context(raise_exception=False).button_confirm() self.assertTrue(self.po.exception_ids) self.assertTrue(self.po.exceptions_summary) # Test cannot ignore blocked exception with self.assertRaises(UserError): self.po.action_ignore_exceptions() self.assertFalse(self.po.ignore_exception) - with self.assertRaises(ValidationError): + with expect_base_exception_error(self): self.po.button_confirm() - self.po.with_context(raise_exception=False).button_confirm() + with expect_base_exception_error(self): + self.po.with_context(raise_exception=False).button_confirm() self.assertTrue(self.po.exception_ids) self.assertTrue(self.po.exceptions_summary)