From 2e69dca0c1d0f2844e27945d6a92b0193557b875 Mon Sep 17 00:00:00 2001 From: "i.nueske" <i.nueske@indiscale.com> Date: Tue, 14 Jan 2025 11:50:04 +0100 Subject: [PATCH] ENH: Update add_property docstring, make special attribute case-insensitive --- src/linkahead/common/models.py | 10 +++++++++- unittests/test_issues.py | 11 ++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/linkahead/common/models.py b/src/linkahead/common/models.py index 35db9f6..5f6f7ec 100644 --- a/src/linkahead/common/models.py +++ b/src/linkahead/common/models.py @@ -622,6 +622,10 @@ class Entity: If you want to add a property to an already existing entity, the property ``id`` of that property needs to be specified before you send the updated entity to the server. + If the specified name matches the name of a special attribute in + lowercase, such as 'name' or 'description', the attribute is set + instead. If the attribute already has a value, this fails with a + ValueError. Parameters ---------- @@ -671,6 +675,9 @@ class Entity: If you try to add an ``Entity`` object with File or Record role (or, equivalently, a ``File`` or ``Record`` object) as a property, a ``ValueError`` is raised. + ValueError: + If the property to be added is a special attribute and already has + a value. Examples -------- @@ -750,7 +757,8 @@ class Entity: "This method expects you to pass at least an entity, a name or an id.") # If the name is a special attribute, set the attribute instead - if name in SPECIAL_ATTRIBUTES and name in dir(self): + if name and name.lower() in SPECIAL_ATTRIBUTES and name.lower() in dir(self): + name = name.lower() if getattr(self, name) is None: setattr(self, name, value) return self diff --git a/unittests/test_issues.py b/unittests/test_issues.py index 33fdc9e..31c09ed 100644 --- a/unittests/test_issues.py +++ b/unittests/test_issues.py @@ -135,16 +135,17 @@ def test_issue_134(): Test setting special attributes using add_property. https://gitlab.com/linkahead/linkahead-pylib/-/issues/134 """ - for attr, val in [("name", "TestRecord"), ("datatype", db.TEXT), - ("description", "desc"), ("id", 1000), + for attr, val in [("nAme", "TestRecord"), ("datatype", db.TEXT), + ("descriptIon", "desc"), ("id", 1000), ("value", "Val"), ("unit", "°C")]: rec = db.Record() - assert rec.__getattribute__(attr) is None + assert rec.__getattribute__(attr.lower()) is None rec.add_property(name=attr, value=val) - assert rec.__getattribute__(attr) == val + assert rec.__getattribute__(attr.lower()) == val assert rec.get_property(attr) is None + assert rec.get_property(attr.lower()) is None - exp_str = f"'{attr}' is a special attribute and does not support" + exp_str = f"'{attr.lower()}' is a special attribute and does not" with raises(ValueError) as e: rec.add_property(name=attr, value=val) assert exp_str in str(e.value) -- GitLab