From ff5717ff14a7ccc4d0347c24bfa7793fbf58650e Mon Sep 17 00:00:00 2001 From: Timm Fitschen <t.fitschen@indiscale.com> Date: Fri, 26 Jun 2020 00:22:18 +0200 Subject: [PATCH] EHN: get_property_values - support attributes --- src/caosdb/common/models.py | 19 +++++++++++++++---- unittests/test_container.py | 23 ++++++++++++++++------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py index 184be277..e05cadcf 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 f2b73b0a..906295ce 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,)] - - -- GitLab