changeset 701dd4767588 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=701dd4767588
description:
        Test relation domain with context

        issue10386
        review336171002
diffstat:

 trytond/tests/modelstorage.py      |  31 +++++++++++++++++++++++
 trytond/tests/test_cache.py        |  25 +++++++++++++++++-
 trytond/tests/test_modelstorage.py |  51 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 106 insertions(+), 1 deletions(-)

diffs (150 lines):

diff -r 3f6ad2b8a631 -r 701dd4767588 trytond/tests/modelstorage.py
--- a/trytond/tests/modelstorage.py     Sun Jun 06 09:27:28 2021 +0200
+++ b/trytond/tests/modelstorage.py     Sun Jun 06 09:28:24 2021 +0200
@@ -78,12 +78,43 @@
                 ('value', '!=', 'valid')),
             ],
         depends=['relation_valid'])
+    relation_context = fields.Many2One(
+        'test.modelstorage.relation_domain.target', "Value",
+        domain=[
+            ('valid', '=', True),
+            ],
+        context={
+            'valid': Eval('relation_valid', True),
+            })
+    relation_pyson_context = fields.Many2One(
+        'test.modelstorage.relation_domain.target', "Value",
+        domain=[
+            If(Eval('relation_valid', True),
+                ('valid', '=', True),
+                ('valid', '=', None)),
+            ],
+        context={
+            'valid': Eval('relation_valid', True),
+            },
+        depends=['relation_valid'])
 
 
 class ModelStorageRelationDomainTarget(ModelSQL):
     "Target of Model stored containing a relation field with a domain"
     __name__ = 'test.modelstorage.relation_domain.target'
     value = fields.Char("Value")
+    valid = fields.Function(
+        fields.Boolean("Valid"), 'get_valid', searcher='search_valid')
+
+    def get_valid(self, name):
+        return Transaction().context.get('valid')
+
+    @classmethod
+    def search_valid(cls, name, domain):
+        if Transaction().context.get('valid') == domain[2]:
+            return []
+        else:
+            return [('id', '=', -1)]
 
 
 class ModelStorageRelationDomain2(ModelSQL):
diff -r 3f6ad2b8a631 -r 701dd4767588 trytond/tests/test_cache.py
--- a/trytond/tests/test_cache.py       Sun Jun 06 09:27:28 2021 +0200
+++ b/trytond/tests/test_cache.py       Sun Jun 06 09:28:24 2021 +0200
@@ -5,7 +5,8 @@
 import unittest
 
 from trytond import backend, cache as cache_mod
-from trytond.cache import freeze, MemoryCache, LRUDict, LRUDictTransaction
+from trytond.cache import (
+    freeze, unfreeze, MemoryCache, LRUDict, LRUDictTransaction)
 from trytond.tests.test_tryton import with_transaction, activate_module
 from trytond.tests.test_tryton import DB_NAME, USER
 from trytond.transaction import Transaction
@@ -40,6 +41,28 @@
                                             ('string', 'test'),
                                             ]))]))]))
 
+    def testUnfreeze(self):
+        "Test unfreeze"
+        for value, result in [
+                (freeze([1, 2, 3]), (1, 2, 3)),
+                (freeze({'dict': {
+                                'inner dict': {
+                                    'list': [1, 2, 3],
+                                    'string': 'test',
+                                    },
+                                },
+                            }),
+                    {'dict': {
+                            'inner dict': {
+                                'list': (1, 2, 3),
+                                'string': 'test',
+                                },
+                            },
+                        }),
+                ]:
+            with self.subTest(value=value):
+                self.assertEqual(unfreeze(value), result)
+
 
 class MemoryCacheTestCase(unittest.TestCase):
     "Test Cache"
diff -r 3f6ad2b8a631 -r 701dd4767588 trytond/tests/test_modelstorage.py
--- a/trytond/tests/test_modelstorage.py        Sun Jun 06 09:27:28 2021 +0200
+++ b/trytond/tests/test_modelstorage.py        Sun Jun 06 09:28:24 2021 +0200
@@ -389,6 +389,57 @@
         self.assertTrue(cm.exception.domain[1], 'value')
 
     @with_transaction()
+    def test_relation_domain_context(self):
+        "Test valid relation with context"
+        pool = Pool()
+        Model = pool.get('test.modelstorage.relation_domain')
+        Target = pool.get('test.modelstorage.relation_domain.target')
+
+        target, = Target.create([{}])
+
+        record, = Model.create(
+            [{'relation_context': target.id, 'relation_valid': True}])
+
+    @with_transaction()
+    def test_relation_domain_context_invalid(self):
+        "Test invalid relation with context"
+        pool = Pool()
+        Model = pool.get('test.modelstorage.relation_domain')
+        Target = pool.get('test.modelstorage.relation_domain.target')
+
+        target, = Target.create([{}])
+
+        with self.assertRaises(DomainValidationError):
+            record, = Model.create(
+                [{'relation_context': target.id, 'relation_valid': False}])
+
+    @with_transaction()
+    def test_relation_pyson_domain_context(self):
+        "Test valid relation with PYSON and context"
+        pool = Pool()
+        Model = pool.get('test.modelstorage.relation_domain')
+        Target = pool.get('test.modelstorage.relation_domain.target')
+
+        target, = Target.create([{}])
+
+        record, = Model.create(
+            [{'relation_pyson_context': target.id, 'relation_valid': True}])
+
+    @with_transaction()
+    def test_relation_pyson_domain_context_invalid(self):
+        "Test invalid relation with PYSON and context"
+        pool = Pool()
+        Model = pool.get('test.modelstorage.relation_domain')
+        Target = pool.get('test.modelstorage.relation_domain.target')
+
+        target, = Target.create([{}])
+
+        with self.assertRaises(DomainValidationError):
+            record, = Model.create([{
+                        'relation_pyson_context': target.id,
+                        'relation_valid': False}])
+
+    @with_transaction()
     def test_relation2_domain_invalid(self):
         "Test invalid relation domain with 2 level"
         pool = Pool()

Reply via email to