diff --git a/CHANGELOG.md b/CHANGELOG.md index a32e16deba8103a3e037c7bedfeb8fc0e4c55a6b..e64b4960473c4fc9564e1bd7dae8797ca76553cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,9 +37,11 @@ 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 +* [#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 ### Security ### diff --git a/src/linkahead/common/models.py b/src/linkahead/common/models.py index 8bd8eacda1035761bbc7b9aff4134fc45f682da0..38a485fce53fb7c80e1fabe85785bfff0bafb970 100644 --- a/src/linkahead/common/models.py +++ b/src/linkahead/common/models.py @@ -749,6 +749,11 @@ 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 in SPECIAL_ATTRIBUTES and name in dir(self): + setattr(self, name, value) + return self + 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..5f44a90434f1de82dcd5be3959e2799f2f683fde 100644 --- a/unittests/test_issues.py +++ b/unittests/test_issues.py @@ -128,3 +128,18 @@ 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) is None + rec.add_property(name=attr, value=val) + assert rec.__getattribute__(attr) == val + assert rec.get_property(attr) is None