Skip to content

Commit 3558237

Browse files
tfokittiu
tfo
authored andcommitted
[MIG v13.0] base_custom_info: fix tests
1 parent aaae035 commit 3558237

6 files changed

+45
-19
lines changed

base_custom_info/models/custom_info_category.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ class CustomInfoCategory(models.Model):
2121
def check_access_rule(self, operation):
2222
"""You access a category if you access at least one property."""
2323
last_error = None
24-
for prop in self.mapped("property_ids"):
25-
try:
26-
prop.check_access_rule(operation)
27-
return
28-
except Exception as err:
29-
last_error = err
30-
pass
24+
if not self.env.context.get("check_access_rule_once"):
25+
for prop in self.with_context(check_access_rule_once=True).mapped(
26+
"property_ids"
27+
):
28+
try:
29+
prop.check_access_rule(operation)
30+
return
31+
except Exception as err:
32+
last_error = err
33+
pass
3134
if last_error:
3235
raise last_error
3336
return super().check_access_rule(operation)

base_custom_info/models/custom_info_option.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,18 @@ class CustomInfoOption(models.Model):
3131

3232
def check_access_rule(self, operation):
3333
"""You access an option if you access at least one property."""
34-
last_error = None
35-
for prop in self.mapped("property_ids"):
36-
try:
37-
prop.check_access_rule(operation)
38-
return
39-
except Exception as err:
40-
last_error = err
41-
pass
42-
if last_error:
43-
raise last_error
34+
# Avoid to enter in infinite loop
35+
if not self.env.context.get("check_access_rule_once"):
36+
last_error = None
37+
for prop in self.with_context(check_access_rule_once=True).mapped(
38+
"property_ids"
39+
):
40+
try:
41+
prop.check_access_rule(operation)
42+
return
43+
except Exception as err:
44+
last_error = err
45+
pass
46+
if last_error:
47+
raise last_error
4448
return super().check_access_rule(operation)

base_custom_info/models/custom_info_template.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Copyright 2017 Pedro M. Baeza <pedro.baeza@tecnativa.com>
33
# License LGPL-3 - See http://www.gnu.org/licenses/lgpl-3.0.html
44

5+
from psycopg2 import IntegrityError
6+
57
from odoo import _, api, fields, models
68
from odoo.exceptions import ValidationError
79

@@ -47,7 +49,10 @@ def _compute_model(self):
4749

4850
def _inverse_model(self):
4951
for r in self:
50-
r.model_id = self.env["ir.model"].search([("model", "=", r.model)])
52+
model = self.env["ir.model"].search([("model", "=", r.model)])
53+
if r.model and not model:
54+
raise IntegrityError(_("You must set a existing model."))
55+
r.model_id = model
5156

5257
@api.model
5358
def _search_model(self, operator, value):

base_custom_info/models/custom_info_value.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class CustomInfoValue(models.Model):
3737
help="Record that owns this custom value.",
3838
)
3939
res_id = fields.Integer(
40-
string="Resource ID", required=True, index=True, store=True, ondelete="cascade",
40+
string="Resource ID", required=True, index=True, ondelete="cascade",
4141
)
4242
property_id = fields.Many2one(
4343
comodel_name="custom.info.property",

base_custom_info/tests/test_partner.py

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def set_custom_info_for_agrolait(self):
1919
"""Used when you need to use some created custom info."""
2020
self.agrolait.custom_info_template_id = self.tpl
2121
self.agrolait._onchange_custom_info_template_id()
22+
# need to invalidate cache here: o2m with an integer (res_id) as inverse_name
23+
self.agrolait.custom_info_ids.invalidate_cache()
2224
self.agrolait.get_custom_info_value(
2325
self.env.ref("base_custom_info.prop_haters")
2426
).value_int = 5
@@ -29,6 +31,8 @@ def test_access_granted(self):
2931
agrolait = self.agrolait.with_user(self.demouser)
3032
agrolait.custom_info_template_id = self.tpl
3133
agrolait._onchange_custom_info_template_id()
34+
# need to invalidate cache here: o2m with an integer (res_id) as inverse_name
35+
self.agrolait.custom_info_ids.invalidate_cache()
3236
prop_weaknesses = agrolait.env.ref("base_custom_info.prop_weaknesses")
3337
val_weaknesses = agrolait.get_custom_info_value(prop_weaknesses)
3438
opt_food = agrolait.env.ref("base_custom_info.opt_food")
@@ -68,6 +72,8 @@ def test_apply_unapply_template(self):
6872
# Applying a template autofills the values
6973
self.agrolait.custom_info_template_id = self.tpl
7074
self.agrolait._onchange_custom_info_template_id()
75+
# need to invalidate cache here: o2m with an integer (res_id) as inverse_name
76+
self.agrolait.custom_info_ids.invalidate_cache()
7177
self.assertEqual(len(self.agrolait.custom_info_ids), len(self.tpl.property_ids))
7278
self.assertEqual(
7379
self.agrolait.custom_info_ids.mapped("property_id"), self.tpl.property_ids
@@ -76,6 +82,8 @@ def test_apply_unapply_template(self):
7682
# Unapplying a template empties the values
7783
self.agrolait.custom_info_template_id = False
7884
self.agrolait._onchange_custom_info_template_id()
85+
# need to invalidate cache here: o2m with an integer (res_id) as inverse_name
86+
self.agrolait.custom_info_ids.invalidate_cache()
7987
self.assertFalse(self.agrolait.custom_info_template_id)
8088
self.assertFalse(self.agrolait.custom_info_ids)
8189

@@ -128,6 +136,8 @@ def test_default_values(self):
128136
"""Default values get applied."""
129137
self.agrolait.custom_info_template_id = self.tpl
130138
self.agrolait._onchange_custom_info_template_id()
139+
# need to invalidate cache here: o2m with an integer (res_id) as inverse_name
140+
self.agrolait.custom_info_ids.invalidate_cache()
131141
val_weaknesses = self.agrolait.get_custom_info_value(
132142
self.env.ref("base_custom_info.prop_weaknesses")
133143
)
@@ -145,6 +155,8 @@ def test_recursive_templates(self):
145155
self.agrolait.invalidate_cache()
146156
self.assertIn(tpl_gamer, self.agrolait.all_custom_info_templates())
147157
self.agrolait._onchange_custom_info_template_id()
158+
# need to invalidate cache here: o2m with an integer (res_id) as inverse_name
159+
self.agrolait.custom_info_ids.invalidate_cache()
148160
self.assertTrue(
149161
tpl_gamer.property_ids < self.agrolait.mapped("custom_info_ids.property_id")
150162
)

base_custom_info/tests/test_value_conversion.py

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def fill_value(self, prop, value, field="value"):
2727
value = str(value)
2828
self.value = self.agrolait.get_custom_info_value(prop)
2929
self.value[field] = value
30+
# need to invalidate cache here: o2m with an integer (res_id) as inverse_name
31+
self.agrolait.custom_info_ids.invalidate_cache()
3032

3133
def creation_found(self, value):
3234
"""Ensure you can search what you just created."""

0 commit comments

Comments
 (0)