diff --git a/src/linkahead/common/models.py b/src/linkahead/common/models.py index 35db9f62faf0db773d7547f119d43b5710803fea..5f6f7ec3ac63ae20413a151c36efde03db9944e5 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 33fdc9e6bfd5e8034f6e54220f158ffe065b6225..31c09ed1f8309d4d7c4562db8222ff0cf2527228 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)