|
8 | 8 | from odoo.http import Response |
9 | 9 | from odoo.tools import mute_logger |
10 | 10 |
|
11 | | -from odoo.addons.base_rest.controllers.main import _PseudoCollection |
12 | 11 | 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 |
15 | 12 |
|
16 | | -from .common import FakeConcurrentUpdateError, TestDBLoggingMixin |
| 13 | +from .common import TestDBLoggingMixin |
17 | 14 |
|
18 | 15 |
|
19 | 16 | class TestDBLogging(TransactionRestServiceRegistryCase, TestDBLoggingMixin): |
@@ -277,163 +274,3 @@ def test_log_entry_values_failure_with_response(self): |
277 | 274 | "status": 418, |
278 | 275 | }, |
279 | 276 | ) |
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 | | - ) |
0 commit comments