From c52be307c758bc8a22f52b75785664cca8df1e33 Mon Sep 17 00:00:00 2001 From: "i.nueske" <i.nueske@indiscale.com> Date: Sat, 11 Jan 2025 14:57:44 +0100 Subject: [PATCH] ENH: Updated add_property to support special attributes: - if given name is a special attributes, update corresponding attribute instead of creating a new property - Add test for https://gitlab.com/linkahead/linkahead-pylib/-/issues/134 - Update changelog --- CHANGELOG.md | 4 +++- src/linkahead/common/models.py | 5 +++++ unittests/test_apiutils.py | 12 ++++++------ unittests/test_issues.py | 15 +++++++++++++++ 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a32e16d..e64b496 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 8bd8eac..38a485f 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 6667089..4b1bbf2 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 3b0117b..5f44a90 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 -- GitLab