Test relation domain with context
issue10386 review336171002
This commit is contained in:
1 parent
04b66919f7
commit
c6e42ec4cf
3 files changed
+106
-1
No files matched your search
@@ -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):
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in new issue
Block a user