From 9dc5ab5fa6f1aee0bf59f45c1a94b29ac600956b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <henrik@trineo.org> Date: Mon, 5 Aug 2019 13:54:16 +0200 Subject: [PATCH] API: assure_object_is_in_list: this function does now check whether an object (not an entity) is in the list --- src/caosadvancedtools/cfood.py | 10 ++++------ unittests/test_cfood.py | 12 +++++------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/caosadvancedtools/cfood.py b/src/caosadvancedtools/cfood.py index 44ba253e..76f7bce4 100644 --- a/src/caosadvancedtools/cfood.py +++ b/src/caosadvancedtools/cfood.py @@ -156,14 +156,12 @@ class AbstractCFood(object): entity.add_property(prop, value) -def assure_entity_is_in_list(entity, containing_object, property_name, +def assure_object_is_in_list(obj, containing_object, property_name, to_be_updated): """ - Checks whether `entity` is one of the values in the list property + Checks whether `obj` is one of the values in the list property `property_name` of the supplied entity containing_object`. - This check is done based on ids. - If this is the case this function returns. Otherwise the entity is added to the property `property_name` and the entity `containing_object` is added to the supplied list to_be_updated in order to indicate, that the entity @@ -179,7 +177,7 @@ def assure_entity_is_in_list(entity, containing_object, property_name, contained = False for el in current_list: - if el.id == entity.id: + if el == obj: contained = True break @@ -187,7 +185,7 @@ def assure_entity_is_in_list(entity, containing_object, property_name, if contained: return - current_list.append(entity) + current_list.append(obj) to_be_updated.append(containing_object) diff --git a/unittests/test_cfood.py b/unittests/test_cfood.py index a7d584a9..2f00c1c1 100644 --- a/unittests/test_cfood.py +++ b/unittests/test_cfood.py @@ -25,8 +25,8 @@ import unittest import caosdb as db -from caosadvancedtools.cfood import (AbstractCFood, assure_entity_is_in_list, - assure_has_parent) +from caosadvancedtools.cfood import (AbstractCFood, assure_has_parent, + assure_object_is_in_list) PATTERN = "h.*" @@ -52,15 +52,13 @@ class InsertionTest(unittest.TestCase): def test_contained_in_list(self): entity_with_list = db.Record() prop_name = "list_prop" - contained_entity = db.Record(id=1005) to_be_updated = [] - assure_entity_is_in_list(contained_entity, entity_with_list, prop_name, + assure_object_is_in_list(1005, entity_with_list, prop_name, to_be_updated) assert to_be_updated[0] is entity_with_list - assert (to_be_updated[0].get_property(prop_name).value[0] is - contained_entity) + assert (to_be_updated[0].get_property(prop_name).value[0] == 1005) to_be_updated = [] - assure_entity_is_in_list(contained_entity, entity_with_list, prop_name, + assure_object_is_in_list(1005, entity_with_list, prop_name, to_be_updated) assert len(to_be_updated) == 0 -- GitLab