From c6e42ec4cf6500c652e0c3cb15d6254fdfc1decf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 6 Jun 2021 09:28:24 +0200 Subject: [PATCH] Test relation domain with context issue10386 review336171002 --- trytond/tests/modelstorage.py | 31 ++++++++++++++++++ trytond/tests/test_cache.py | 25 ++++++++++++++- trytond/tests/test_modelstorage.py | 51 ++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) diff --git a/trytond/tests/modelstorage.py b/trytond/tests/modelstorage.py index a42eff73e7..998848b2ab 100644 --- a/trytond/tests/modelstorage.py +++ b/trytond/tests/modelstorage.py @@ -78,12 +78,43 @@ class ModelStorageRelationDomain(ModelSQL): ('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 --git a/trytond/tests/test_cache.py b/trytond/tests/test_cache.py index 87b0351f66..9c274372e0 100644 --- a/trytond/tests/test_cache.py +++ b/trytond/tests/test_cache.py @@ -5,7 +5,8 @@ import time 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 @@ class CacheTestCase(unittest.TestCase): ('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 --git a/trytond/tests/test_modelstorage.py b/trytond/tests/test_modelstorage.py index c22b043eed..ede4c2eb68 100644 --- a/trytond/tests/test_modelstorage.py +++ b/trytond/tests/test_modelstorage.py @@ -388,6 +388,57 @@ class ModelStorageTestCase(unittest.TestCase): self.assertEqual(cm.exception.domain[0], [['value', '!=', 'valid']]) 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"