1
1
# Copyright (C) 2014-Today GRAP (http://www.grap.coop)
2
2
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
3
3
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
4
-
5
- from odoo .exceptions import ValidationError
4
+ from odoo import Command
5
+ from odoo .exceptions import AccessError , ValidationError
6
6
from odoo .tests .common import TransactionCase
7
7
8
8
9
9
class Tests (TransactionCase ):
10
- def setUp ( self ):
11
- super (). setUp ()
12
- self . ProductTemplate = self . env [ "product.template" ]
13
- self . ResCompany = self .env ["res.company " ]
14
- self . FiscalClassification = self .env ["account.product.fiscal.classification " ]
15
- self . WizardChange = self .env ["wizard.change .fiscal.classification" ]
16
-
17
- self . main_company = self . env . ref ( "base.main_company" )
18
- self . group_accountant = self .env .ref ("account.group_account_manager " )
19
- self . group_system = self .env .ref ("base.group_system " )
20
- self . company_2 = self .env .ref ("account_product_fiscal_classification.company_2 " )
21
- self . user_demo = self .env .ref ("base.user_demo " )
22
-
23
- self . fiscal_classification_A_company_1 = self .env .ref (
10
+ @ classmethod
11
+ def setUpClass ( cls ):
12
+ super (). setUpClass ()
13
+ cls . ProductTemplate = cls .env ["product.template " ]
14
+ cls . ResCompany = cls .env ["res.company " ]
15
+ cls . FiscalClassification = cls .env ["account.product .fiscal.classification" ]
16
+ cls . WizardChange = cls . env [ "wizard.change.fiscal.classification" ]
17
+
18
+ cls . main_company = cls .env .ref ("base.main_company " )
19
+ cls . group_accountant = cls .env .ref ("account.group_account_manager " )
20
+ cls . group_system = cls .env .ref ("base.group_system " )
21
+ cls . company_2 = cls .env .ref ("account_product_fiscal_classification.company_2 " )
22
+
23
+ cls . classification_A_company_1 = cls .env .ref (
24
24
"account_product_fiscal_classification.fiscal_classification_A_company_1"
25
25
)
26
- self . fiscal_classification_B_company_1 = self .env .ref (
26
+ cls . classification_B_company_1 = cls .env .ref (
27
27
"account_product_fiscal_classification.fiscal_classification_B_company_1"
28
28
)
29
- self . fiscal_classification_D_global = self .env .ref (
29
+ cls . classification_D_global = cls .env .ref (
30
30
"account_product_fiscal_classification.fiscal_classification_D_global"
31
31
)
32
- self .product_template_A_company_1 = self .env .ref (
32
+ cls .product_template_A_company_1 = cls .env .ref (
33
33
"account_product_fiscal_classification.product_template_A_company_1"
34
34
)
35
- self .account_tax_purchase_20_company_1 = self .env .ref (
35
+ cls .account_tax_purchase_20_company_1 = cls .env .ref (
36
36
"account_product_fiscal_classification.account_tax_purchase_20_company_1"
37
37
)
38
- self .account_tax_sale_20_company_1 = self .env .ref (
38
+ cls .account_tax_sale_5_company_1 = cls .env .ref (
39
+ "account_product_fiscal_classification.account_tax_sale_5_company_1"
40
+ )
41
+ cls .account_tax_sale_20_company_1 = cls .env .ref (
39
42
"account_product_fiscal_classification.account_tax_sale_20_company_1"
40
43
)
41
- self .account_tax_purchase_7_company_2 = self .env .ref (
44
+ cls .account_tax_purchase_7_company_2 = cls .env .ref (
42
45
"account_product_fiscal_classification.account_tax_purchase_7_company_2"
43
46
)
44
- self .chart_template = self .env .ref (
47
+ cls .chart_template = cls .env .ref (
45
48
"account_product_fiscal_classification.chart_template"
46
49
)
47
- # self.sale_tax_2 = self.env.ref(
48
- # "account_product_fiscal_classification.account_tax_sale_5_company_1"
49
- # )
50
-
51
- self .category_all = self .env .ref ("product.product_category_all" )
52
- self .category_wine = self .env .ref (
50
+ cls .category_all = cls .env .ref ("product.product_category_all" )
51
+ cls .category_wine = cls .env .ref (
53
52
"account_product_fiscal_classification.category_wine"
54
53
)
55
54
56
- # # Group to create product
57
- # self.product_group = self.env.ref("account.group_account_manager")
58
- # self.restricted_group = self.env.ref("base.group_system")
55
+ cls .initial_classif_count = cls .FiscalClassification .search_count ([])
59
56
60
- # Test Section
57
+ vals = {
58
+ "company_id" : cls .env .ref ("base.main_company" ).id ,
59
+ "name" : "New User" ,
60
+ "login" : "new_user" ,
61
+
62
+ "groups_id" : [Command .set (cls .env .ref ("base.group_system" ).ids )],
63
+ }
64
+ cls .user_demo = cls .env ["res.users" ].create (vals )
65
+
66
+ def _create_product (self , extra_vals , user = False ):
67
+ if not user :
68
+ user = self .env .user
69
+ vals = {
70
+ "name" : "Test Product" ,
71
+ "company_id" : self .main_company .id ,
72
+ "categ_id" : self .category_all .id ,
73
+ }
74
+ vals .update (extra_vals )
75
+ return self .ProductTemplate .with_user (user ).create (vals )
76
+
77
+ # # Test Section
61
78
def test_01_change_classification (self ):
62
79
"""Test the behaviour when we change Fiscal Classification for
63
80
products."""
64
81
wizard = self .WizardChange .create (
65
82
{
66
- "old_fiscal_classification_id" : self .fiscal_classification_A_company_1 .id ,
67
- "new_fiscal_classification_id" : self .fiscal_classification_B_company_1 .id ,
83
+ "old_fiscal_classification_id" : self .classification_A_company_1 .id ,
84
+ "new_fiscal_classification_id" : self .classification_B_company_1 .id ,
68
85
}
69
86
)
70
87
wizard .button_change_fiscal_classification ()
71
88
self .assertEqual (
72
89
self .product_template_A_company_1 .fiscal_classification_id ,
73
- self .fiscal_classification_B_company_1 ,
90
+ self .classification_B_company_1 ,
74
91
"Fiscal Classification change has failed for products via Wizard." ,
75
92
)
76
93
77
94
def test_02_create_product (self ):
78
95
"""Test if creating a product with fiscal classification set correct taxes"""
79
- vals = {
80
- "name" : "Product Product Name" ,
81
- "company_id" : self .main_company .id ,
82
- "fiscal_classification_id" : self .fiscal_classification_D_global .id ,
83
- }
84
- newTemplate = self .ProductTemplate .create (vals )
96
+ newTemplate = self ._create_product (
97
+ {"fiscal_classification_id" : self .classification_D_global .id }
98
+ )
85
99
# Test that all taxes are set (in sudo mode)
86
100
self .assertEqual (
87
101
set (newTemplate .sudo ().taxes_id .ids ),
88
- set (self .fiscal_classification_D_global .sudo ().sale_tax_ids .ids ),
102
+ set (self .classification_D_global .sudo ().sale_tax_ids .ids ),
89
103
)
90
104
self .assertEqual (
91
105
set (newTemplate .sudo ().supplier_taxes_id .ids ),
92
- set (self .fiscal_classification_D_global .sudo ().purchase_tax_ids .ids ),
106
+ set (self .classification_D_global .sudo ().purchase_tax_ids .ids ),
93
107
)
94
108
95
109
def test_03_update_fiscal_classification (self ):
96
110
"""Test if changing a Configuration of a Fiscal Classification changes
97
111
the product."""
98
- self .fiscal_classification_A_company_1 .write (
112
+ self .classification_A_company_1 .write (
99
113
{"sale_tax_ids" : [(6 , 0 , [self .account_tax_sale_20_company_1 .id ])]}
100
114
)
101
115
self .assertEqual (
@@ -107,7 +121,7 @@ def test_03_update_fiscal_classification(self):
107
121
def test_05_unlink_fiscal_classification (self ):
108
122
"""Test if unlinking a Fiscal Classification with products fails."""
109
123
with self .assertRaises (ValidationError ):
110
- self .fiscal_classification_A_company_1 .unlink ()
124
+ self .classification_A_company_1 .unlink ()
111
125
112
126
def test_10_chart_template (self ):
113
127
"""Test if installing new CoA creates correct classification"""
@@ -148,29 +162,85 @@ def test_30_rules(self):
148
162
149
163
# Create a product without rules should success
150
164
self ._create_product (
151
- self . env . user , self .category_all , self . fiscal_classification_B_company_1
165
+ { "fiscal_classification_id" : self .classification_B_company_1 . id }
152
166
)
153
167
self ._create_product (
154
- self .user_demo , self .category_all , self .fiscal_classification_B_company_1
168
+ {"fiscal_classification_id" : self .classification_B_company_1 .id },
169
+ user = self .user_demo ,
155
170
)
156
171
172
+ vals = {
173
+ "categ_id" : self .category_wine .id ,
174
+ "fiscal_classification_id" : self .classification_B_company_1 .id ,
175
+ }
157
176
# create a product not respecting rules should succeed with accountant perfil
158
- self ._create_product (
159
- self .env .user , self .category_wine , self .fiscal_classification_B_company_1
160
- )
177
+ self ._create_product (vals )
161
178
162
179
# create a product not respecting rules should fail without accountant perfil
163
180
with self .assertRaises (ValidationError ):
164
- self ._create_product (
165
- self .user_demo ,
166
- self .category_wine ,
167
- self .fiscal_classification_B_company_1 ,
168
- )
181
+ self ._create_product (vals , user = self .user_demo )
169
182
170
- def _create_product (self , user , category , classification ):
183
+ def test_no_classification_and_find_one (self ):
171
184
vals = {
172
- "name" : "Test Product" ,
173
- "categ_id" : category .id ,
174
- "fiscal_classification_id" : classification .id ,
185
+ "taxes_id" : self .classification_B_company_1 .sale_tax_ids .ids ,
186
+ "supplier_taxes_id" : self .classification_B_company_1 .purchase_tax_ids .ids ,
187
+ }
188
+ product = self ._create_product (vals , user = self .user_demo )
189
+
190
+ # no new classification is created
191
+ classif_count_after = self .FiscalClassification .search_count ([])
192
+ self .assertEqual (classif_count_after , self .initial_classif_count )
193
+ # product is linked to the correct classification
194
+ self .assertEqual (
195
+ product .fiscal_classification_id , self .classification_B_company_1
196
+ )
197
+
198
+ def test_no_classification_one_more_tax_and_create_one (self ):
199
+ """Create a product with fiscal settings that looks like
200
+ classification_B_company_1 but with an additional supplier tax.
201
+ """
202
+
203
+ vals = {
204
+ "taxes_id" : self .classification_B_company_1 .sale_tax_ids .ids ,
205
+ "supplier_taxes_id" : self .account_tax_purchase_20_company_1 .ids ,
206
+ }
207
+
208
+ # create a product with fiscal settings that doesn't match
209
+ # existing classification should fail with non accountant user
210
+ with self .assertRaises (AccessError ):
211
+ self ._create_product (vals , user = self .user_demo )
212
+
213
+ # create a product with fiscal settings that doesn't match
214
+ # existing classification should success with accountant user
215
+ product = self ._create_product (vals )
216
+ self .assertNotEqual (product .fiscal_classification_id , False )
217
+ classif_count_after = self .FiscalClassification .search_count ([])
218
+ self .assertEqual (classif_count_after , self .initial_classif_count + 1 )
219
+
220
+ def test_no_classification_one_less_tax_and_create_one (self ):
221
+ """Create a product with fiscal settings that looks like
222
+ classification_B_company_1 but with one less tax
223
+ """
224
+
225
+ vals = {
226
+ "taxes_id" : self .account_tax_sale_5_company_1 .ids ,
227
+ "supplier_taxes_id" : [],
175
228
}
176
- self .ProductTemplate .with_user (user ).create (vals )
229
+ # create a product with fiscal settings that doesn't match
230
+ # existing classification should fail with non accountant user
231
+ with self .assertRaises (AccessError ):
232
+ self ._create_product (vals , user = self .user_demo )
233
+
234
+ # create a product with fiscal settings that doesn't match
235
+ # existing classification should success with accountant user
236
+ product = self ._create_product (vals )
237
+
238
+ self .assertNotEqual (product .fiscal_classification_id , False )
239
+ classif_count_after = self .FiscalClassification .search_count ([])
240
+ self .assertEqual (classif_count_after , self .initial_classif_count + 1 )
241
+
242
+ def test_no_tax_nor_classification (self ):
243
+ product = self ._create_product ({"taxes_id" : [], "supplier_taxes_id" : []})
244
+ classif = product .fiscal_classification_id
245
+ self .assertEqual (classif .purchase_tax_ids .ids , [])
246
+ self .assertEqual (classif .sale_tax_ids .ids , [])
0 commit comments