From 4ed54e9bbc84bd36b2055f98023b0fad5891ccea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 3 May 2018 11:57:10 +0200 Subject: [PATCH] Support child_of/parent_of with None in the value list This was a implicit behaviour before the changeset 6fefd00369c1. As a lot of code rely on this behaviour, we make it explicit by adding a test case. issue7405 review36251002 --- trytond/model/fields/many2one.py | 5 ++--- trytond/tests/test_field_many2one.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/trytond/model/fields/many2one.py b/trytond/model/fields/many2one.py index 2bd87fa89d..3d322d5bbe 100644 --- a/trytond/model/fields/many2one.py +++ b/trytond/model/fields/many2one.py @@ -116,7 +116,7 @@ class Many2One(Field): cursor = Transaction().connection.cursor() table, _ = tables[None] name, operator, ids = domain - red_sql = reduce_ids(table.id, ids) + red_sql = reduce_ids(table.id, (i for i in ids if i is not None)) Target = self.get_target() left = getattr(Target, self.left).sql_column(table) right = getattr(Target, self.right).sql_column(table) @@ -138,8 +138,7 @@ class Many2One(Field): target = Target.__table__() table, _ = tables[None] name, operator, ids = domain - ids = set(ids) # Ensure it is a set for concatenation - red_sql = reduce_ids(target.id, ids) + red_sql = reduce_ids(target.id, (i for i in ids if i is not None)) if operator.endswith('child_of'): tree = With('id', recursive=True) diff --git a/trytond/tests/test_field_many2one.py b/trytond/tests/test_field_many2one.py index fa5dad5b0e..ebbfeb1368 100644 --- a/trytond/tests/test_field_many2one.py +++ b/trytond/tests/test_field_many2one.py @@ -320,6 +320,25 @@ class FieldMany2OneTestCase(unittest.TestCase): "Test search many2one mptt not child of empty" self._test_search_not_child_of_empty('test.many2one_mptt') + @with_transaction() + def _test_search_child_of_none(self, model_name): + Many2One = Pool().get(model_name) + self.create_tree(Many2One) + + result = Many2One.search([ + ('many2one', 'child_of', [None]), + ]) + + self.assertListEqual(result, []) + + def test_search_tree_child_of_none(self): + "Test search many2one tree child of None" + self._test_search_child_of_none('test.many2one_tree') + + def test_search_mptt_child_of_none(self): + "Test search many2one mptt child of None" + self._test_search_child_of_none('test.many2one_mptt') + @with_transaction() def _test_search_parent_of_root1(self, model_name): Many2One = Pool().get(model_name)