Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions base_exception/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 23 additions & 21 deletions base_exception/tests/test_base_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down Expand Up @@ -74,48 +74,48 @@ 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(
{
"domain": "[('partner_id.zip', '=', False)]",
"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(
{
"method": "exception_method_no_zip",
"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
Expand Down Expand Up @@ -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)

Expand Down
Loading