Skip to content
Snippets Groups Projects
Commit 8176aeb6 authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

DEV,TST: added entity checks

Added some utitlity functions and corresponding tests:
assure_entity_is_in_list for checking whether an entity has another
entity in one of its properties and possibly sets it
assure_has_parent checks whether an entity has a parent and possibly
adds it
insert_id_based_on_name and get_ids_for_entities_with_names for adding
ids to entities in a container such that they can be updated
parent 54d2b7a9
Branches
Tags
No related merge requests found
......@@ -145,3 +145,79 @@ class AbstractCFood(object):
entity.add_property(prop, value, datatype=datatype)
else:
entity.add_property(prop, value)
def assure_entity_is_in_list(entity, containing_object, property_name,
to_be_updated):
"""
Checks whether `entity` 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
`containing_object` should be updated.
If the property is missing, it is added first and then the entity is added.
"""
if containing_object.get_property(property_name) is None:
containing_object.add_property(property_name, value=[],
datatype=db.LIST(property_name))
current_list = containing_object.get_property(property_name).value
contained = False
for el in current_list:
if el.id == entity.id:
contained = True
break
if contained:
return
current_list.append(entity)
to_be_updated.append(containing_object)
def assure_has_parent(entity, parent, to_be_updated):
"""
Checks whether `entity` has a parent with name `parent`.
If this is the case this function returns. Otherwise the entity is assigned
a new parent and is added to the supplied list to_be_updated in order to
indicate, that the entity `entity` should be updated.
"""
parents = entity.get_parents()
contained = False
for el in parents:
if el.name == parent:
contained = True
break
if contained:
return
entity.add_parent(parent)
to_be_updated.append(entity)
def insert_id_based_on_name(entity):
if entity.name is not None and (entity.id is None or entity.id < 0):
entity.id = get_entity(entity.name).id
def get_ids_for_entities_with_names(entities):
for ent in entities:
insert_id_based_on_name(ent)
for prop in ent.get_properties():
insert_id_based_on_name(prop)
for parent in ent.get_parents():
insert_id_based_on_name(parent)
insert_id_based_on_name(ent)
......@@ -22,12 +22,11 @@
#
# ** end header
import unittest
from tempfile import NamedTemporaryFile
import caosdb as db
from caosadvancedtools.cfood import AbstractCFood
from caosadvancedtools.cfood import (AbstractCFood, assure_entity_is_in_list,
assure_has_parent)
PATTERN = "h.*"
......@@ -47,3 +46,30 @@ class CFoodReTest(unittest.TestCase):
self.assertIsNotNone(TestCFood._pattern)
self.assertIsNotNone(TestCFood.match("hallo"))
self.assertIsNone(TestCFood.match("allo"))
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,
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)
to_be_updated = []
assure_entity_is_in_list(contained_entity, entity_with_list, prop_name,
to_be_updated)
assert len(to_be_updated) == 0
def test_parent(self):
entity = db.Record()
to_be_updated = []
assure_has_parent(entity, "parent", to_be_updated)
assert to_be_updated[0] is entity
assert (to_be_updated[0].get_parents()[0].name == "parent")
to_be_updated = []
assure_has_parent(entity, "parent", to_be_updated)
assert len(to_be_updated) == 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment