Test context on target instance of One2One
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
from trytond.model import ModelSQL, Unique, fields
|
||||
from trytond.pool import Pool
|
||||
from trytond.transaction import Transaction
|
||||
|
||||
|
||||
class One2One(ModelSQL):
|
||||
@@ -80,6 +81,39 @@ class One2OneDomainRelation(ModelSQL):
|
||||
]
|
||||
|
||||
|
||||
class One2OneContext(ModelSQL):
|
||||
__name__ = 'test.one2one_context'
|
||||
one2one = fields.One2One(
|
||||
'test.one2one_context.relation', 'origin', 'target', "One2One",
|
||||
context={'test': 'foo'})
|
||||
|
||||
|
||||
class One2OneContextTarget(ModelSQL):
|
||||
__name__ = 'test.one2one_context.target'
|
||||
context = fields.Function(fields.Char("context"), 'get_context')
|
||||
|
||||
def get_context(self, name):
|
||||
context = Transaction().context
|
||||
return context.get('test')
|
||||
|
||||
|
||||
class One2OneContextRelation(ModelSQL):
|
||||
__name__ = 'test.one2one_context.relation'
|
||||
origin = fields.Many2One('test.one2one_context', 'Origin')
|
||||
target = fields.Many2One('test.one2one_context.target', 'Target')
|
||||
|
||||
@classmethod
|
||||
def __setup__(cls):
|
||||
super().__setup__()
|
||||
table = cls.__table__()
|
||||
cls._sql_constraints += [
|
||||
('origin_unique', Unique(table, table.origin),
|
||||
'tests.msg_one2one_relation_origin_unique'),
|
||||
('target_unique', Unique(table, table.target),
|
||||
'tests.msg_one2one_relation_target_unique'),
|
||||
]
|
||||
|
||||
|
||||
def register(module):
|
||||
Pool.register(
|
||||
One2One,
|
||||
@@ -89,4 +123,7 @@ def register(module):
|
||||
One2OneRequiredRelation,
|
||||
One2OneDomain,
|
||||
One2OneDomainRelation,
|
||||
One2OneContext,
|
||||
One2OneContextTarget,
|
||||
One2OneContextRelation,
|
||||
module=module, type_='model')
|
||||
|
||||
@@ -361,3 +361,44 @@ class FieldOne2OneTestCase(TestCase):
|
||||
|
||||
self.assertIsInstance(record.one2one, Target)
|
||||
self.assertEqual(record.one2one.name, "Test")
|
||||
|
||||
@with_transaction()
|
||||
def test_context_attribute(self):
|
||||
"Test context on one2one attribute"
|
||||
pool = Pool()
|
||||
One2One = pool.get('test.one2one_context')
|
||||
Target = pool.get('test.one2one_context.target')
|
||||
|
||||
target, = Target.create([{}])
|
||||
record, = One2One.create([{
|
||||
'one2one': target.id,
|
||||
}])
|
||||
|
||||
self.assertEqual(record.one2one.context, 'foo')
|
||||
|
||||
@with_transaction()
|
||||
def test_context_read(self):
|
||||
"Test context on one2one read"
|
||||
pool = Pool()
|
||||
One2One = pool.get('test.one2one_context')
|
||||
Target = pool.get('test.one2one_context.target')
|
||||
|
||||
target, = Target.create([{}])
|
||||
record, = One2One.create([{
|
||||
'one2one': target.id,
|
||||
}])
|
||||
data, = One2One.read([record.id], ['one2one.context'])
|
||||
|
||||
self.assertEqual(data['one2one.']['context'], 'foo')
|
||||
|
||||
@with_transaction()
|
||||
def test_context_set(self):
|
||||
"Test context on one2one set"
|
||||
pool = Pool()
|
||||
One2One = pool.get('test.one2one_context')
|
||||
Target = pool.get('test.one2one_context.target')
|
||||
|
||||
target, = Target.create([{}])
|
||||
record = One2One(one2one=target.id)
|
||||
|
||||
self.assertEqual(record.one2one.context, 'foo')
|
||||
|
||||
Reference in New Issue
Block a user