|
13 | 13 | from odoo.addons.component.tests.common import new_rollbacked_env |
14 | 14 | from odoo.addons.rest_log import exceptions as log_exceptions # pylint: disable=W7950 |
15 | 15 |
|
16 | | -from .common import TestDBLoggingMixin |
| 16 | +from .common import FakeConcurrentUpdateError, TestDBLoggingMixin |
17 | 17 |
|
18 | 18 |
|
19 | 19 | class TestDBLogging(TransactionRestServiceRegistryCase, TestDBLoggingMixin): |
@@ -374,3 +374,66 @@ def test_log_exception_value(self): |
374 | 374 | self._test_exception( |
375 | 375 | "value", log_exceptions.RESTServiceDispatchException, "ValueError", "severe" |
376 | 376 | ) |
| 377 | + |
| 378 | + |
| 379 | +class TestDBLoggingRetryableError( |
| 380 | + TransactionRestServiceRegistryCase, TestDBLoggingMixin |
| 381 | +): |
| 382 | + @classmethod |
| 383 | + def setUpClass(cls): |
| 384 | + super().setUpClass() |
| 385 | + cls._setup_registry(cls) |
| 386 | + |
| 387 | + @classmethod |
| 388 | + def tearDownClass(cls): |
| 389 | + # pylint: disable=W8110 |
| 390 | + cls._teardown_registry(cls) |
| 391 | + super().tearDownClass() |
| 392 | + |
| 393 | + def _test_exception(self, test_type, wrapping_exc, exc_name, severity): |
| 394 | + log_model = self.env["rest.log"].sudo() |
| 395 | + initial_entries = log_model.search([]) |
| 396 | + # Context: we are running in a transaction case which uses savepoints. |
| 397 | + # The log machinery is going to rollback the transation when catching errors. |
| 398 | + # Hence we need a completely separated env for the service. |
| 399 | + with new_rollbacked_env() as new_env: |
| 400 | + # Init fake collection w/ new env |
| 401 | + collection = _PseudoCollection(self._collection_name, new_env) |
| 402 | + service = self._get_service(self, collection=collection) |
| 403 | + with self._get_mocked_request(env=new_env): |
| 404 | + try: |
| 405 | + service.dispatch("fail", test_type) |
| 406 | + except Exception as err: |
| 407 | + # Not using `assertRaises` to inspect the exception directly |
| 408 | + self.assertTrue(isinstance(err, wrapping_exc)) |
| 409 | + self.assertEqual( |
| 410 | + service._get_exception_message(err), "Failed as you wanted!" |
| 411 | + ) |
| 412 | + |
| 413 | + with new_rollbacked_env() as new_env: |
| 414 | + log_model = new_env["rest.log"].sudo() |
| 415 | + entry = log_model.search([]) - initial_entries |
| 416 | + expected = { |
| 417 | + "collection": service._collection, |
| 418 | + "state": "failed", |
| 419 | + "result": "null", |
| 420 | + "exception_name": exc_name, |
| 421 | + "exception_message": "Failed as you wanted!", |
| 422 | + "severity": severity, |
| 423 | + } |
| 424 | + self.assertRecordValues(entry, [expected]) |
| 425 | + |
| 426 | + @staticmethod |
| 427 | + def _get_test_controller(class_or_instance, root_path=None): |
| 428 | + return super()._get_test_controller( |
| 429 | + class_or_instance, root_path="/test_log_exception_retryable/" |
| 430 | + ) |
| 431 | + |
| 432 | + def test_log_exception_retryable(self): |
| 433 | + # retryable error must bubble up to the retrying mechanism |
| 434 | + self._test_exception( |
| 435 | + "retryable", |
| 436 | + FakeConcurrentUpdateError, |
| 437 | + "odoo.addons.rest_log.tests.common.FakeConcurrentUpdateError", |
| 438 | + "warning", |
| 439 | + ) |
0 commit comments