-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathtest_base_exception.py
More file actions
189 lines (163 loc) · 6.74 KB
/
test_base_exception.py
File metadata and controls
189 lines (163 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Copyright 2016 Akretion Mourad EL HADJ MIMOUNE
# Copyright 2020 Hibou Corp.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
try:
from decorator import decoratorx as decorator
except ImportError:
from decorator import decorator
from unittest.mock import patch
from odoo_test_helper import FakeModelLoader
from odoo.exceptions import UserError, ValidationError
from odoo.tests import TransactionCase
from ..exceptions import BaseExceptionError
class TestBaseException(TransactionCase):
def setUp(self):
# FakeModelLoader must be used in setUp, not setUpClass
super().setUp()
self.loader = FakeModelLoader(self.env, self.__module__)
self.loader.backup_registry()
from .purchase_test import ExceptionRule, LineTest, PurchaseTest, WizardTest
self.loader.update_registry((ExceptionRule, LineTest, PurchaseTest, WizardTest))
self.partner = self.env["res.partner"].create({"name": "Foo"})
self.po = self.env["base.exception.test.purchase"].create(
{
"name": "Test base exception to basic purchase",
"partner_id": self.partner.id,
"line_ids": [
(0, 0, {"name": "line test", "amount": 120.0, "qty": 1.5})
],
}
)
self.exception_rule = self.env["exception.rule"].create(
{
"name": "No ZIP code on destination",
"sequence": 10,
"model": "base.exception.test.purchase",
"code": "if not self.partner_id.zip: failed=True",
"exception_type": "by_py_code",
}
)
exception_rule_confirm_obj = self.env["exception.rule.confirm.test.purchase"]
self.exception_rule_confirm = exception_rule_confirm_obj.with_context(
active_model="base.exception.test.purchase", active_ids=self.po.ids
).create(
{
"related_model_id": self.po.id,
"ignore": False,
}
)
@decorator
def swallow_base_exception_error(func, self):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except BaseExceptionError:
return None
return wrapper
@decorator
def patch_base_exception_method_env(func, self):
with patch(
"odoo.addons.base_exception.models.base_exception_method.Environment"
) as mocked_env:
mocked_env.return_value = self.env
return func(self)
def tearDown(self):
self.loader.restore_registry()
return super().tearDown()
@patch_base_exception_method_env
def test_valid(self):
self.partner.write({"zip": "00000"})
self.exception_rule.active = False
self.po.button_confirm()
self.assertFalse(self.po.exception_ids)
def test_exception_rule_confirm(self):
self.exception_rule_confirm.action_confirm()
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):
self.po.button_confirm()
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):
self.po.button_confirm()
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):
self.po.button_confirm()
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):
self.po.button_confirm()
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
self.po.action_ignore_exceptions()
self.assertTrue(self.po.ignore_exception)
self.assertFalse(self.po.exceptions_summary)
self.po.button_confirm()
self.assertEqual(self.po.state, "purchase")
def test_purchase_check_exception(self):
self.po.test_purchase_check_exception()
def test_purchase_check_button_approve(self):
self.po.button_approve()
self.assertEqual(self.po.state, "to approve")
def test_purchase_check_button_draft(self):
self.po.button_draft()
self.assertEqual(self.po.state, "draft")
@patch_base_exception_method_env
def test_purchase_check_button_confirm(self):
self.partner.write({"zip": "00000"})
self.po.button_confirm()
self.assertEqual(self.po.state, "purchase")
def test_purchase_check_button_cancel(self):
self.po.button_cancel()
self.assertEqual(self.po.state, "cancel")
@patch_base_exception_method_env
@swallow_base_exception_error
def test_detect_exceptions(self):
self.po.detect_exceptions()
@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):
self.po.button_confirm()
# Test that we have linked exceptions
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):
self.po.button_confirm()
self.po.with_context(raise_exception=False).button_confirm()
self.assertTrue(self.po.exception_ids)
self.assertTrue(self.po.exceptions_summary)