From a17386e9706172f5f5dea8c8a13fd5a38bf9708d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 6 Oct 2017 11:00:41 +0200 Subject: [PATCH] Let default value of copy converted for the type of field The default value of copy is expected to be the same format as for the default methods. So the value for xxx2Many fields must be converted. issue6818 review37751002 --- trytond/model/modelstorage.py | 2 +- trytond/tests/test_copy.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/trytond/model/modelstorage.py b/trytond/model/modelstorage.py index 787aff4d06..aef02cd40c 100644 --- a/trytond/model/modelstorage.py +++ b/trytond/model/modelstorage.py @@ -285,7 +285,7 @@ class ModelStorage(Model): if field_name in default: data[field_name] = default[field_name] - elif (isinstance(cls._fields[field_name], fields.Function) + if (isinstance(cls._fields[field_name], fields.Function) and not isinstance( cls._fields[field_name], fields.MultiValue)): del data[field_name] diff --git a/trytond/tests/test_copy.py b/trytond/tests/test_copy.py index a4c1636043..35f98d565c 100644 --- a/trytond/tests/test_copy.py +++ b/trytond/tests/test_copy.py @@ -44,6 +44,24 @@ class CopyTestCase(unittest.TestCase): self.assertEqual([x.name for x in one2many.one2many], [x.name for x in one2many_copy.one2many]) + @with_transaction() + def test_one2many_default(self): + "Test copy one2many with default" + pool = Pool() + One2many = pool.get('test.copy.one2many') + Target = pool.get('test.copy.one2many.target') + + record = One2many(name="Test") + record.save() + target = Target(name="Target") + target.save() + + record_copy, = One2many.copy( + [record], default={'one2many': [target.id]}) + + self.assertListEqual( + [x.name for x in record_copy.one2many], [target.name]) + @with_transaction() def test_many2many(self): 'Test copy many2many' @@ -74,6 +92,23 @@ class CopyTestCase(unittest.TestCase): self.assertEqual([x.name for x in many2many.many2many], [x.name for x in many2many_copy.many2many]) + @with_transaction() + def test_many2many_default(self): + "Test copy many2many with default" + pool = Pool() + Many2many = pool.get('test.copy.many2many') + Target = pool.get('test.copy.many2many.target') + + record = Many2many(name="Test") + record.save() + target = Target(name="Target") + target.save() + + record_copy, = Many2many.copy( + [record], default={'many2many': [target.id]}) + + self.assertSequenceEqual(record_copy.many2many, [target]) + def suite(): return unittest.TestLoader().loadTestsFromTestCase(CopyTestCase)