Skip to content

Commit 29a06dc

Browse files
author
François Degrave
committed
[ADD] datamodel: add test to check if datamodel env equals the env of the current request
1 parent 6af16dc commit 29a06dc

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

datamodel/tests/test_build_datamodel.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import mock
66
from marshmallow_objects.models import Model as MarshmallowModel
77

8+
from odoo import SUPERUSER_ID, api
9+
10+
from odoo.addons.website.tools import MockRequest
11+
812
from .. import fields
913
from ..core import Datamodel
1014
from .common import DatamodelRegistryCase, TransactionDatamodelCase
@@ -40,7 +44,7 @@ class Datamodel2(Datamodel):
4044
self.assertIsInstance(Datamodel2(), MarshmallowModel)
4145

4246
def test_no_name(self):
43-
""" Ensure that a datamodel has a _name """
47+
"""Ensure that a datamodel has a _name"""
4448

4549
class Datamodel1(Datamodel):
4650
pass
@@ -50,7 +54,7 @@ class Datamodel1(Datamodel):
5054
Datamodel1._build_datamodel(self.datamodel_registry)
5155

5256
def test_register(self):
53-
""" Able to register datamodels in datamodels registry """
57+
"""Able to register datamodels in datamodels registry"""
5458

5559
class Datamodel1(Datamodel):
5660
_name = "datamodel1"
@@ -67,7 +71,7 @@ class Datamodel2(Datamodel):
6771
)
6872

6973
def test_inherit_bases(self):
70-
""" Check __bases__ of Datamodel with _inherit """
74+
"""Check __bases__ of Datamodel with _inherit"""
7175

7276
class Datamodel1(Datamodel):
7377
_name = "datamodel1"
@@ -90,7 +94,7 @@ class Datamodel3(Datamodel):
9094
)
9195

9296
def test_prototype_inherit_bases(self):
93-
""" Check __bases__ of Datamodel with _inherit and different _name """
97+
"""Check __bases__ of Datamodel with _inherit and different _name"""
9498

9599
class Datamodel1(Datamodel):
96100
_name = "datamodel1"
@@ -188,7 +192,7 @@ class Datamodel4(Datamodel):
188192
)
189193

190194
def test_custom_build(self):
191-
""" Check that we can hook at the end of a Datamodel build """
195+
"""Check that we can hook at the end of a Datamodel build"""
192196

193197
class Datamodel1(Datamodel):
194198
_name = "datamodel1"
@@ -204,7 +208,7 @@ def _complete_datamodel_build(cls):
204208
self.assertTrue(self.env.datamodels["datamodel1"]._build_done)
205209

206210
def test_inherit_attrs(self):
207-
""" Check attributes inheritance of Datamodels with _inherit """
211+
"""Check attributes inheritance of Datamodels with _inherit"""
208212

209213
class Datamodel1(Datamodel):
210214
_name = "datamodel1"
@@ -236,7 +240,7 @@ def say(self):
236240
self.assertEqual("foo bar", datamodel2.say())
237241

238242
def test_duplicate_datamodel(self):
239-
""" Check that we can't have 2 datamodels with the same name """
243+
"""Check that we can't have 2 datamodels with the same name"""
240244

241245
class Datamodel1(Datamodel):
242246
_name = "datamodel1"
@@ -250,7 +254,7 @@ class Datamodel2(Datamodel):
250254
Datamodel2._build_datamodel(self.datamodel_registry)
251255

252256
def test_no_parent(self):
253-
""" Ensure we can't _inherit a non-existent datamodel """
257+
"""Ensure we can't _inherit a non-existent datamodel"""
254258

255259
class Datamodel1(Datamodel):
256260
_name = "datamodel1"
@@ -261,7 +265,7 @@ class Datamodel1(Datamodel):
261265
Datamodel1._build_datamodel(self.datamodel_registry)
262266

263267
def test_no_parent2(self):
264-
""" Ensure we can't _inherit by prototype a non-existent datamodel """
268+
"""Ensure we can't _inherit by prototype a non-existent datamodel"""
265269

266270
class Datamodel1(Datamodel):
267271
_name = "datamodel1"
@@ -276,7 +280,7 @@ class Datamodel2(Datamodel):
276280
Datamodel2._build_datamodel(self.datamodel_registry)
277281

278282
def test_add_inheritance(self):
279-
""" Ensure we can add a new inheritance """
283+
"""Ensure we can add a new inheritance"""
280284

281285
class Datamodel1(Datamodel):
282286
_name = "datamodel1"
@@ -339,7 +343,7 @@ class Datamodel1(Datamodel):
339343
self.env.datamodels["datamodel1"](field_str="1234")
340344

341345
def test_nested_model(self):
342-
""" Test nested model serialization/deserialization"""
346+
"""Test nested model serialization/deserialization"""
343347

344348
class Parent(Datamodel):
345349
_name = "parent"
@@ -366,7 +370,7 @@ class Child(Datamodel):
366370
self.assertEqual(new_instance.child.field_str, instance.child.field_str)
367371

368372
def test_list_nested_model(self):
369-
""" Test list model of nested model serialization/deserialization"""
373+
"""Test list model of nested model serialization/deserialization"""
370374

371375
class Parent(Datamodel):
372376
_name = "parent"
@@ -469,6 +473,10 @@ class Item(Datamodel):
469473
self.assertEqual(instance.items[0].env, self.env)
470474
schema = instance.items[0].get_schema()
471475
self.assertEqual(schema._env, self.env)
476+
another_env = api.Environment(self.env.registry.cursor(), SUPERUSER_ID, {})
477+
with MockRequest(another_env):
478+
self.assertNotEqual(p.env, self.env)
479+
self.assertEqual(p.env, another_env)
472480

473481

474482
class TestRegistryAccess(TransactionDatamodelCase):

0 commit comments

Comments
 (0)