diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py index 184be2776fd3aa63d281950bdee64d52619c6194..e05cadcf07f43bbddc420df940c4b8d359f06924 100644 --- a/src/caosdb/common/models.py +++ b/src/caosdb/common/models.py @@ -636,15 +636,26 @@ class Entity(object): val = prop.value elif isinstance(sel, (tuple, list)) and len(sel) > 0: + val = None ref = self - while len(sel) > 0 and isinstance(ref, Entity): + + # the while loop walks through the re + while len(sel) > 1 and isinstance(ref, Entity): prop = ref.get_property(sel[0]) - if prop is not None: + if prop is not None and isinstance(prop.value, Entity): ref = prop.value else: - ref = None + # non-entity value - + ref = prop sel = sel[1:] - val = ref if len(sel) == 0 else None + if len(sel) == 1 and isinstance(ref, Entity): + if (hasattr(sel[0], "lower") + and hasattr(ref, sel[0].lower())): + val = getattr(ref, sel[0].lower()) + else: + prop = ref.get_property(sel[0]) + if prop is not None: + val = prop.value else: raise TypeError("The elements of the parameter `properties` " "must contain str or non-empty tuples of str." diff --git a/unittests/test_container.py b/unittests/test_container.py index f2b73b0abf0740f706c51cf46a30fa8bf5ca8bdf..906295ceaf5b7b8b5c2ca74e69f50b132e91cf29 100644 --- a/unittests/test_container.py +++ b/unittests/test_container.py @@ -32,9 +32,10 @@ def test_get_property_values(): rt_window = c.RecordType("Window") rt_owner = c.RecordType("Owner") p_height = c.Property("Height", datatype=c.DOUBLE) + window = c.Record().add_parent(rt_window) window.id = 1001 - window.add_property(p_height, 20.5) + window.add_property(p_height, 20.5, unit="m") owner = c.Record("The Queen").add_parent(rt_owner) @@ -42,7 +43,7 @@ def test_get_property_values(): house.add_parent(rt_house) house.add_property(rt_owner, owner) house.add_property(rt_window, window) - house.add_property(p_height, 40.2) + house.add_property(p_height, 40.2, unit="ft") container = c.Container() container.extend([ @@ -50,20 +51,28 @@ def test_get_property_values(): owner ]) + + assert getattr(house.get_property(p_height), "unit") == "ft" + assert getattr(window.get_property(p_height), "unit") == "m" + table = container.get_property_values("naME", "height", + ("height", "unit"), "window", + ("window", "non-existing"), + ("window", "non-existing", "unit"), + ("window", "unit"), ("window", "heiGHT"), + ("window", "heiGHT", "value"), + ("window", "heiGHT", "unit"), "owner") assert len(table) == 2 house_row = table[0] - assert house_row == (house.name, 40.2, window.id, 20.5, owner.name) + assert house_row == (house.name, 40.2, "ft", window.id, None, None, None, 20.5, 20.5, "m", owner.name) owner_row = table[1] - assert owner_row == (owner.name, None, None, None, None) + assert owner_row == (owner.name, None, None, None, None, None, None, None, None, None, None) - assert container.get_property_values("sdfg") == [(None,), (None,)] + assert container.get_property_values("non-existing") == [(None,), (None,)] assert container.get_property_values("name") == [(house.name,), (owner.name,)] - -