Add test for fmany2one field
This commit is contained in:
1 parent
f147c94118
commit
68ae551752
3 files changed
+254
-6
No files matched your search
@@ -11,12 +11,12 @@ def register():
|
||||
from . import (
|
||||
access, copy_, export_data, field_binary, field_boolean, field_char,
|
||||
field_context, field_date, field_datetime, field_dict, field_float,
|
||||
field_function, field_integer, field_many2many, field_many2one,
|
||||
field_multiselection, field_numeric, field_one2many, field_one2one,
|
||||
field_reference, field_selection, field_text, field_time,
|
||||
field_timedelta, history, import_data, mixin, model, model_log,
|
||||
modelsql, modelstorage, modelview, mptt, multivalue, path, resource,
|
||||
rule, tree, trigger, wizard, workflow)
|
||||
field_fmany2one, field_function, field_integer, field_many2many,
|
||||
field_many2one, field_multiselection, field_numeric, field_one2many,
|
||||
field_one2one, field_reference, field_selection, field_text,
|
||||
field_time, field_timedelta, history, import_data, mixin, model,
|
||||
model_log, modelsql, modelstorage, modelview, mptt, multivalue, path,
|
||||
resource, rule, tree, trigger, wizard, workflow)
|
||||
|
||||
access.register('tests')
|
||||
copy_.register('tests')
|
||||
@@ -33,6 +33,7 @@ def register():
|
||||
field_integer.register('tests')
|
||||
field_many2many.register('tests')
|
||||
field_many2one.register('tests')
|
||||
field_fmany2one.register('tests')
|
||||
field_multiselection.register('tests')
|
||||
field_numeric.register('tests')
|
||||
field_one2many.register('tests')
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
# This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||
# this repository contains the full copyright notices and license terms.
|
||||
|
||||
from trytond.model import ModelSQL, Unique, fields
|
||||
from trytond.pool import Pool
|
||||
|
||||
|
||||
class FMany2OneTarget(ModelSQL):
|
||||
"FMany2One Target"
|
||||
__name__ = 'test.fmany2one_target'
|
||||
|
||||
name = fields.Char("Name")
|
||||
children = fields.One2Many(
|
||||
'test.fmany2one_target.child', 'parent', "Children")
|
||||
|
||||
@classmethod
|
||||
def __setup__(cls):
|
||||
super().__setup__()
|
||||
table = cls.__table__()
|
||||
cls._sql_constraints += [
|
||||
('name_unique', Unique(table, table.name),
|
||||
"The name must be unique."),
|
||||
]
|
||||
|
||||
|
||||
class FMany2OneTargetChild(
|
||||
fields.fmany2one(
|
||||
'parent', 'parent_name', 'test.fmany2one_target,name', "Parent"),
|
||||
ModelSQL):
|
||||
"FMany2One Target Child"
|
||||
__name__ = 'test.fmany2one_target.child'
|
||||
|
||||
name = fields.Char("Name")
|
||||
parent_name = fields.Char("Parent Name")
|
||||
|
||||
@classmethod
|
||||
def __setup__(cls):
|
||||
super().__setup__()
|
||||
table = cls.__table__()
|
||||
cls._sql_constraints += [
|
||||
('name_parent_unique',
|
||||
Unique(table, table.name, table.parent_name),
|
||||
"The name and parent must be unique."),
|
||||
]
|
||||
|
||||
|
||||
class FMany2One(
|
||||
fields.fmany2one(
|
||||
'target', 'target_name', 'test.fmany2one_target,name', "Target"),
|
||||
fields.fmany2one(
|
||||
'child', 'child_name,target_name',
|
||||
'test.fmany2one_target.child,name,parent_name', "Child"),
|
||||
ModelSQL):
|
||||
"FMany2One"
|
||||
__name__ = 'test.fmany2one'
|
||||
|
||||
target_name = fields.Char("Target Name")
|
||||
child_name = fields.Char("Child Name")
|
||||
|
||||
|
||||
class FMany2OneRequired(
|
||||
fields.fmany2one(
|
||||
'target', 'target_name', 'test.fmany2one_target,name', "Target",
|
||||
required=True),
|
||||
ModelSQL):
|
||||
"FMany2One required"
|
||||
__name__ = 'test.fmany2one_required'
|
||||
|
||||
target_name = fields.Char("Target Name", required=True)
|
||||
|
||||
|
||||
def register(module):
|
||||
Pool.register(
|
||||
FMany2OneTarget,
|
||||
FMany2OneTargetChild,
|
||||
FMany2One,
|
||||
FMany2OneRequired,
|
||||
module=module, type_='model')
|
||||
@@ -0,0 +1,169 @@
|
||||
# This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||
# this repository contains the full copyright notices and license terms.
|
||||
|
||||
from trytond.pool import Pool
|
||||
from trytond.tests.test_tryton import (
|
||||
TestCase, activate_module, with_transaction)
|
||||
|
||||
|
||||
class FieldFMany2OneTestCase(TestCase):
|
||||
"Test Field fmany2one"
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
activate_module('tests')
|
||||
|
||||
@with_transaction()
|
||||
def test_create(self):
|
||||
"Test create fmany2one"
|
||||
pool = Pool()
|
||||
Target = pool.get('test.fmany2one_target')
|
||||
FMany2One = pool.get('test.fmany2one')
|
||||
target, = Target.create([{'name': "Foo"}])
|
||||
|
||||
fmany2one, = FMany2One.create([{
|
||||
'target': target.id,
|
||||
}])
|
||||
|
||||
self.assertEqual(fmany2one.target, target)
|
||||
self.assertEqual(fmany2one.target_name, "Foo")
|
||||
|
||||
@with_transaction()
|
||||
def test_create_composed_key(self):
|
||||
"Test create fmany2one with composed key"
|
||||
pool = Pool()
|
||||
Target = pool.get('test.fmany2one_target')
|
||||
TargetChild = pool.get('test.fmany2one_target.child')
|
||||
FMany2One = pool.get('test.fmany2one')
|
||||
target, = Target.create([{'name': "Foo"}])
|
||||
target_child, = TargetChild.create(
|
||||
[{'name': "Bar", 'parent': target.id}])
|
||||
|
||||
fmany2one, = FMany2One.create([{
|
||||
'target': target.id,
|
||||
'child': target_child.id,
|
||||
}])
|
||||
|
||||
self.assertEqual(fmany2one.target, target)
|
||||
self.assertEqual(fmany2one.target_name, "Foo")
|
||||
self.assertEqual(fmany2one.child, target_child)
|
||||
self.assertEqual(fmany2one.child_name, "Bar")
|
||||
|
||||
@with_transaction()
|
||||
def test_create_required(self):
|
||||
"Test create fmany2one required"
|
||||
pool = Pool()
|
||||
Target = pool.get('test.fmany2one_target')
|
||||
FMany2One = pool.get('test.fmany2one_required')
|
||||
target, = Target.create([{'name': "Foo"}])
|
||||
|
||||
fmany2one, = FMany2One.create([{
|
||||
'target': target.id,
|
||||
}])
|
||||
|
||||
self.assertEqual(fmany2one.target, target)
|
||||
self.assertEqual(fmany2one.target_name, "Foo")
|
||||
|
||||
@with_transaction()
|
||||
def test_write(self):
|
||||
"Test write fmany2one"
|
||||
pool = Pool()
|
||||
Target = pool.get('test.fmany2one_target')
|
||||
FMany2One = pool.get('test.fmany2one')
|
||||
target, = Target.create([{'name': "Foo"}])
|
||||
|
||||
fmany2one, = FMany2One.create([{}])
|
||||
FMany2One.write([fmany2one], {
|
||||
'target': target.id,
|
||||
})
|
||||
|
||||
self.assertEqual(fmany2one.target, target)
|
||||
self.assertEqual(fmany2one.target_name, "Foo")
|
||||
|
||||
@with_transaction()
|
||||
def test_write_composed_key(self):
|
||||
"Test create fmany2one with composed key"
|
||||
pool = Pool()
|
||||
Target = pool.get('test.fmany2one_target')
|
||||
TargetChild = pool.get('test.fmany2one_target.child')
|
||||
FMany2One = pool.get('test.fmany2one')
|
||||
target, _ = Target.create([{'name': "Foo"}, {'name': "Baz"}])
|
||||
target_child, = TargetChild.create(
|
||||
[{'name': "Bar", 'parent': target.id}])
|
||||
|
||||
fmany2one, = FMany2One.create([{'target_name': "Baz"}])
|
||||
FMany2One.write([fmany2one], {
|
||||
'child': target_child.id,
|
||||
'target': target.id,
|
||||
})
|
||||
|
||||
self.assertEqual(fmany2one.target, target)
|
||||
self.assertEqual(fmany2one.target_name, "Foo")
|
||||
self.assertEqual(fmany2one.child, target_child)
|
||||
self.assertEqual(fmany2one.child_name, "Bar")
|
||||
|
||||
@with_transaction()
|
||||
def test_search_id(self):
|
||||
"Test search on fmany2one with id"
|
||||
pool = Pool()
|
||||
Target = pool.get('test.fmany2one_target')
|
||||
FMany2One = pool.get('test.fmany2one')
|
||||
target, = Target.create([{'name': "Foo"}])
|
||||
fmany2one, = FMany2One.create([{
|
||||
'target': target.id,
|
||||
}])
|
||||
|
||||
result = FMany2One.search([('target', '=', target.id)])
|
||||
|
||||
self.assertEqual(result, [fmany2one])
|
||||
|
||||
@with_transaction()
|
||||
def test_search_string(self):
|
||||
"Test search on fmany2one with string"
|
||||
pool = Pool()
|
||||
Target = pool.get('test.fmany2one_target')
|
||||
FMany2One = pool.get('test.fmany2one')
|
||||
target, = Target.create([{'name': "Foo"}])
|
||||
fmany2one, = FMany2One.create([{
|
||||
'target': target.id,
|
||||
}])
|
||||
|
||||
result = FMany2One.search([('target', '=', "Foo")])
|
||||
|
||||
self.assertEqual(result, [fmany2one])
|
||||
|
||||
@with_transaction()
|
||||
def test_search_nested(self):
|
||||
"Test search on fmany2one with nested clause"
|
||||
pool = Pool()
|
||||
Target = pool.get('test.fmany2one_target')
|
||||
FMany2One = pool.get('test.fmany2one')
|
||||
target, = Target.create([{'name': "Foo"}])
|
||||
fmany2one, = FMany2One.create([{
|
||||
'target': target.id,
|
||||
}])
|
||||
|
||||
result = FMany2One.search([('target.name', '=', "Foo")])
|
||||
|
||||
self.assertEqual(result, [fmany2one])
|
||||
|
||||
@with_transaction()
|
||||
def test_search_composed_key(self):
|
||||
"Test search on fmany2one with composed key"
|
||||
pool = Pool()
|
||||
Target = pool.get('test.fmany2one_target')
|
||||
TargetChild = pool.get('test.fmany2one_target.child')
|
||||
FMany2One = pool.get('test.fmany2one')
|
||||
target, = Target.create([{'name': "Foo"}])
|
||||
target_child, = TargetChild.create(
|
||||
[{'name': "Bar", 'parent': target.id}])
|
||||
fmany2one, = FMany2One.create([{
|
||||
'target': target.id,
|
||||
'child': target_child.id,
|
||||
}])
|
||||
|
||||
result = FMany2One.search([
|
||||
('child', '=', target_child.id),
|
||||
])
|
||||
|
||||
self.assertEqual(result, [fmany2one])
|
||||
Reference in new issue
Block a user