Skip to content

Commit 002ecd2

Browse files
committed
[IMP] rest_log: split exception tests
1 parent 92835f8 commit 002ecd2

3 files changed

Lines changed: 173 additions & 164 deletions

File tree

rest_log/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from . import test_db_logging
2+
from . import test_db_logging_exception

rest_log/tests/test_db_logging.py

Lines changed: 1 addition & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
from odoo.http import Response
99
from odoo.tools import mute_logger
1010

11-
from odoo.addons.base_rest.controllers.main import _PseudoCollection
1211
from odoo.addons.base_rest.tests.common import TransactionRestServiceRegistryCase
13-
from odoo.addons.component.tests.common import new_rollbacked_env
14-
from odoo.addons.rest_log import exceptions as log_exceptions # pylint: disable=W7950
1512

16-
from .common import FakeConcurrentUpdateError, TestDBLoggingMixin
13+
from .common import TestDBLoggingMixin
1714

1815

1916
class TestDBLogging(TransactionRestServiceRegistryCase, TestDBLoggingMixin):
@@ -277,163 +274,3 @@ def test_log_entry_values_failure_with_response(self):
277274
"status": 418,
278275
},
279276
)
280-
281-
282-
class TestDBLoggingExceptionBase(
283-
TransactionRestServiceRegistryCase, TestDBLoggingMixin
284-
):
285-
@classmethod
286-
def setUpClass(cls):
287-
super().setUpClass()
288-
cls._setup_registry(cls)
289-
290-
@classmethod
291-
def tearDownClass(cls):
292-
# pylint: disable=W8110
293-
cls._teardown_registry(cls)
294-
super().tearDownClass()
295-
296-
def _test_exception(self, test_type, wrapping_exc, exc_name, severity):
297-
log_model = self.env["rest.log"].sudo()
298-
initial_entries = log_model.search([])
299-
entry_url_from_exc = None
300-
# Context: we are running in a transaction case which uses savepoints.
301-
# The log machinery is going to rollback the transation when catching errors.
302-
# Hence we need a completely separated env for the service.
303-
with new_rollbacked_env() as new_env:
304-
# Init fake collection w/ new env
305-
collection = _PseudoCollection(self._collection_name, new_env)
306-
service = self._get_service(self, collection=collection)
307-
with self._get_mocked_request(env=new_env):
308-
try:
309-
service.dispatch("fail", test_type)
310-
except Exception as err:
311-
# Not using `assertRaises` to inspect the exception directly
312-
self.assertTrue(isinstance(err, wrapping_exc))
313-
self.assertEqual(
314-
service._get_exception_message(err), "Failed as you wanted!"
315-
)
316-
entry_url_from_exc = err.rest_json_info["log_entry_url"]
317-
318-
with new_rollbacked_env() as new_env:
319-
log_model = new_env["rest.log"].sudo()
320-
entry = log_model.search([]) - initial_entries
321-
expected = {
322-
"collection": service._collection,
323-
"state": "failed",
324-
"result": "null",
325-
"exception_name": exc_name,
326-
"exception_message": "Failed as you wanted!",
327-
"severity": severity,
328-
}
329-
self.assertRecordValues(entry, [expected])
330-
self.assertEqual(entry_url_from_exc, service._get_log_entry_url(entry))
331-
332-
333-
class TestDBLoggingExceptionUserError(TestDBLoggingExceptionBase):
334-
@staticmethod
335-
def _get_test_controller(class_or_instance, root_path=None):
336-
# Override to avoid registering twice the same controller route.
337-
return super()._get_test_controller(
338-
class_or_instance, root_path="/test_log_exception_user/"
339-
)
340-
341-
def test_log_exception_user(self):
342-
self._test_exception(
343-
"user",
344-
log_exceptions.RESTServiceUserErrorException,
345-
"odoo.exceptions.UserError",
346-
"functional",
347-
)
348-
349-
350-
class TestDBLoggingExceptionValidationError(TestDBLoggingExceptionBase):
351-
@staticmethod
352-
def _get_test_controller(class_or_instance, root_path=None):
353-
return super()._get_test_controller(
354-
class_or_instance, root_path="/test_log_exception_validation/"
355-
)
356-
357-
def test_log_exception_validation(self):
358-
self._test_exception(
359-
"validation",
360-
log_exceptions.RESTServiceValidationErrorException,
361-
"odoo.exceptions.ValidationError",
362-
"functional",
363-
)
364-
365-
366-
class TestDBLoggingExceptionValueError(TestDBLoggingExceptionBase):
367-
@staticmethod
368-
def _get_test_controller(class_or_instance, root_path=None):
369-
return super()._get_test_controller(
370-
class_or_instance, root_path="/test_log_exception_value/"
371-
)
372-
373-
def test_log_exception_value(self):
374-
self._test_exception(
375-
"value", log_exceptions.RESTServiceDispatchException, "ValueError", "severe"
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-
)
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# Copyright 2020 Camptocamp SA (http://www.camptocamp.com)
2+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
3+
# from urllib.parse import urlparse
4+
5+
6+
from odoo.addons.base_rest.controllers.main import _PseudoCollection
7+
from odoo.addons.base_rest.tests.common import TransactionRestServiceRegistryCase
8+
from odoo.addons.component.tests.common import new_rollbacked_env
9+
from odoo.addons.rest_log import exceptions as log_exceptions # pylint: disable=W7950
10+
11+
from .common import FakeConcurrentUpdateError, TestDBLoggingMixin
12+
13+
14+
class TestDBLoggingExceptionBase(
15+
TransactionRestServiceRegistryCase, TestDBLoggingMixin
16+
):
17+
@classmethod
18+
def setUpClass(cls):
19+
super().setUpClass()
20+
cls._setup_registry(cls)
21+
22+
@classmethod
23+
def tearDownClass(cls):
24+
# pylint: disable=W8110
25+
cls._teardown_registry(cls)
26+
super().tearDownClass()
27+
28+
def _test_exception(self, test_type, wrapping_exc, exc_name, severity):
29+
log_model = self.env["rest.log"].sudo()
30+
initial_entries = log_model.search([])
31+
entry_url_from_exc = None
32+
# Context: we are running in a transaction case which uses savepoints.
33+
# The log machinery is going to rollback the transation when catching errors.
34+
# Hence we need a completely separated env for the service.
35+
with new_rollbacked_env() as new_env:
36+
# Init fake collection w/ new env
37+
collection = _PseudoCollection(self._collection_name, new_env)
38+
service = self._get_service(self, collection=collection)
39+
with self._get_mocked_request(env=new_env):
40+
try:
41+
service.dispatch("fail", test_type)
42+
except Exception as err:
43+
# Not using `assertRaises` to inspect the exception directly
44+
self.assertTrue(isinstance(err, wrapping_exc))
45+
self.assertEqual(
46+
service._get_exception_message(err), "Failed as you wanted!"
47+
)
48+
entry_url_from_exc = err.rest_json_info["log_entry_url"]
49+
50+
with new_rollbacked_env() as new_env:
51+
log_model = new_env["rest.log"].sudo()
52+
entry = log_model.search([]) - initial_entries
53+
expected = {
54+
"collection": service._collection,
55+
"state": "failed",
56+
"result": "null",
57+
"exception_name": exc_name,
58+
"exception_message": "Failed as you wanted!",
59+
"severity": severity,
60+
}
61+
self.assertRecordValues(entry, [expected])
62+
self.assertEqual(entry_url_from_exc, service._get_log_entry_url(entry))
63+
64+
65+
class TestDBLoggingExceptionUserError(TestDBLoggingExceptionBase):
66+
@staticmethod
67+
def _get_test_controller(class_or_instance, root_path=None):
68+
# Override to avoid registering twice the same controller route.
69+
return super()._get_test_controller(
70+
class_or_instance, root_path="/test_log_exception_user/"
71+
)
72+
73+
def test_log_exception_user(self):
74+
self._test_exception(
75+
"user",
76+
log_exceptions.RESTServiceUserErrorException,
77+
"odoo.exceptions.UserError",
78+
"functional",
79+
)
80+
81+
82+
class TestDBLoggingExceptionValidationError(TestDBLoggingExceptionBase):
83+
@staticmethod
84+
def _get_test_controller(class_or_instance, root_path=None):
85+
return super()._get_test_controller(
86+
class_or_instance, root_path="/test_log_exception_validation/"
87+
)
88+
89+
def test_log_exception_validation(self):
90+
self._test_exception(
91+
"validation",
92+
log_exceptions.RESTServiceValidationErrorException,
93+
"odoo.exceptions.ValidationError",
94+
"functional",
95+
)
96+
97+
98+
class TestDBLoggingExceptionValueError(TestDBLoggingExceptionBase):
99+
@staticmethod
100+
def _get_test_controller(class_or_instance, root_path=None):
101+
return super()._get_test_controller(
102+
class_or_instance, root_path="/test_log_exception_value/"
103+
)
104+
105+
def test_log_exception_value(self):
106+
self._test_exception(
107+
"value", log_exceptions.RESTServiceDispatchException, "ValueError", "severe"
108+
)
109+
110+
111+
class TestDBLoggingRetryableError(
112+
TransactionRestServiceRegistryCase, TestDBLoggingMixin
113+
):
114+
@classmethod
115+
def setUpClass(cls):
116+
super().setUpClass()
117+
cls._setup_registry(cls)
118+
119+
@classmethod
120+
def tearDownClass(cls):
121+
# pylint: disable=W8110
122+
cls._teardown_registry(cls)
123+
super().tearDownClass()
124+
125+
def _test_exception(self, test_type, wrapping_exc, exc_name, severity):
126+
log_model = self.env["rest.log"].sudo()
127+
initial_entries = log_model.search([])
128+
# Context: we are running in a transaction case which uses savepoints.
129+
# The log machinery is going to rollback the transation when catching errors.
130+
# Hence we need a completely separated env for the service.
131+
with new_rollbacked_env() as new_env:
132+
# Init fake collection w/ new env
133+
collection = _PseudoCollection(self._collection_name, new_env)
134+
service = self._get_service(self, collection=collection)
135+
with self._get_mocked_request(env=new_env):
136+
try:
137+
service.dispatch("fail", test_type)
138+
except Exception as err:
139+
# Not using `assertRaises` to inspect the exception directly
140+
self.assertTrue(isinstance(err, wrapping_exc))
141+
self.assertEqual(
142+
service._get_exception_message(err), "Failed as you wanted!"
143+
)
144+
145+
with new_rollbacked_env() as new_env:
146+
log_model = new_env["rest.log"].sudo()
147+
entry = log_model.search([]) - initial_entries
148+
expected = {
149+
"collection": service._collection,
150+
"state": "failed",
151+
"result": "null",
152+
"exception_name": exc_name,
153+
"exception_message": "Failed as you wanted!",
154+
"severity": severity,
155+
}
156+
self.assertRecordValues(entry, [expected])
157+
158+
@staticmethod
159+
def _get_test_controller(class_or_instance, root_path=None):
160+
return super()._get_test_controller(
161+
class_or_instance, root_path="/test_log_exception_retryable/"
162+
)
163+
164+
def test_log_exception_retryable(self):
165+
# retryable error must bubble up to the retrying mechanism
166+
self._test_exception(
167+
"retryable",
168+
FakeConcurrentUpdateError,
169+
"odoo.addons.rest_log.tests.common.FakeConcurrentUpdateError",
170+
"warning",
171+
)

0 commit comments

Comments
 (0)