diff --git a/CHANGELOG.md b/CHANGELOG.md index e780ce250923e9217af507e27be7bd2acb0318e3..616794d12aea9945dfbad3ee28f2b77e936a3ee9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed ### +* [#134](https://gitlab.com/linkahead/linkahead-pylib/-/issues/134) + Setting special attributes using add_property now updates the attribute + instead of adding a new property with the same name + ### Deprecated ### ### Removed ### @@ -60,7 +64,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#87](https://gitlab.com/linkahead/linkahead-pylib/-/issues/87) `XMLSyntaxError` messages when parsing (incomplete) responses in case of certain connection timeouts. - The diff returned by compare_entities now uses id instead of name as key if either property does not have a name * [#127](https://gitlab.com/linkahead/linkahead-pylib/-/issues/127) pylinkahead.ini now supports None and tuples as values for the `timeout` keyword diff --git a/src/linkahead/common/models.py b/src/linkahead/common/models.py index 75b03b70907abdde50fb383c54a02e94cad115ad..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 -------- @@ -749,6 +756,21 @@ class Entity: raise UserWarning( "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 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 + else: + raise ValueError(f"'{name}' is a special attribute and does not " + f"support multi-property. It is already set " + f"and cannot be overwritten using this method. " + f"Please use direct assignment for setting this " + f"property after the first time.") + # ToDo: Implement the same behaviour for special attribute ids, + # https://gitlab.indiscale.com/caosdb/src/caosdb-pylib/-/issues/219 + new_property = Property(name=name, id=id, description=description, datatype=datatype, value=value, unit=unit) diff --git a/unittests/test_apiutils.py b/unittests/test_apiutils.py index 6667089abc2d16e59bd97d16f7d0fe75d07afe1b..4b1bbf239235386684f6ffa5b5118f700931df43 100644 --- a/unittests/test_apiutils.py +++ b/unittests/test_apiutils.py @@ -115,8 +115,8 @@ def test_compare_entities(): r2.add_property("tester", ) r1.add_property("tests_234234", value=45) r2.add_property("tests_TT", value=45) - r1.add_property("datatype", value=45, datatype=db.INTEGER) - r2.add_property("datatype", value=45) + r1.add_property("datatype_prop", value=45, datatype=db.INTEGER) + r2.add_property("datatype_prop", value=45) r1.add_property("entity_id", value=2) r2.add_property("entity_id", value=24) r1.add_property("entity_mix_e", value=2) @@ -152,10 +152,10 @@ def test_compare_entities(): assert "tests_234234" in diff_r1["properties"] assert "tests_TT" in diff_r2["properties"] - assert "datatype" in diff_r1["properties"] - assert "datatype" in diff_r1["properties"]["datatype"] - assert "datatype" in diff_r2["properties"] - assert "datatype" in diff_r2["properties"]["datatype"] + assert "datatype_prop" in diff_r1["properties"] + assert "datatype" in diff_r1["properties"]["datatype_prop"] + assert "datatype_prop" in diff_r2["properties"] + assert "datatype" in diff_r2["properties"]["datatype_prop"] assert "entity_id" in diff_r1["properties"] assert "entity_id" in diff_r2["properties"] diff --git a/unittests/test_issues.py b/unittests/test_issues.py index 3b0117b28c1300ea1eb0919fce02e3881c2ab025..31c09ed1f8309d4d7c4562db8222ff0cf2527228 100644 --- a/unittests/test_issues.py +++ b/unittests/test_issues.py @@ -128,3 +128,24 @@ def test_issue_73(): assert "datatype=" in xml_str assert "Recursive reference" in xml_str assert len(xml_str) < 500 + + +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), + ("value", "Val"), ("unit", "°C")]: + rec = db.Record() + assert rec.__getattribute__(attr.lower()) is None + rec.add_property(name=attr, value=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.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)