From f6c3fba01eaad2f2e282a423e775ea60b53477c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <h.tomwoerden@indiscale.com> Date: Fri, 24 Jan 2025 10:52:05 +0100 Subject: [PATCH 1/3] ENH: add convenience functions `value_matches_versionid`, `get_id_from_versionid` and `get_versionid` --- CHANGELOG.md | 1 + src/linkahead/common/models.py | 21 +++++++++++++++++++++ unittests/test_entity.py | 28 +++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23ddf86..20f2498 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ## ### Added ### +- convenience functions `value_matches_versionid`, `get_id_from_versionid` and `get_versionid` ### Changed ### diff --git a/src/linkahead/common/models.py b/src/linkahead/common/models.py index 75b03b7..0912647 100644 --- a/src/linkahead/common/models.py +++ b/src/linkahead/common/models.py @@ -505,6 +505,9 @@ class Entity: return self + def get_versionid(self): + return str(self.id)+"@"+str(self.version.id) + def get_importance(self, property): # @ReservedAssignment """Get the importance of a given property regarding this entity.""" @@ -1954,6 +1957,7 @@ class QueryTemplate(): return len(self.get_errors()) > 0 + class Parent(Entity): """The parent entities.""" @@ -2126,6 +2130,12 @@ class Property(Entity): return is_reference(self.datatype) + def value_matches_versionid(self): + return value_matches_versionid(self.value) + + def get_id_from_versionid_value(self): + return get_id_from_versionid(self.value) + class Message(object): def __init__( @@ -5670,3 +5680,14 @@ def _filter_entity_list_by_identity(listobject: list[Entity], if pid_none and name_match: matches.append(candidate) return matches + +def value_matches_versionid(value: Union[int, str]): + if isinstance(value, int): + return False + if not isinstance(value, str): + raise ValueError(f"A reference value needs to be int or str. It was {type(value)}. " + "Did you call value_matches_versionid on a non reference value?") + return "@" in value + +def get_id_from_versionid(versionid: str): + return versionid.split("@")[0] diff --git a/unittests/test_entity.py b/unittests/test_entity.py index 855e5a3..722930b 100644 --- a/unittests/test_entity.py +++ b/unittests/test_entity.py @@ -30,7 +30,9 @@ import linkahead from linkahead import (INTEGER, Entity, Parent, Property, Record, RecordType, configure_connection) import warnings -from linkahead.common.models import SPECIAL_ATTRIBUTES +from linkahead.common.models import (SPECIAL_ATTRIBUTES, get_id_from_versionid, +value_matches_versionid) +from linkahead.common.versioning import Version from linkahead.connection.mockup import MockUpServerConnection from lxml import etree from pytest import raises @@ -295,3 +297,27 @@ def test_filter_by_identity(): t.parents.filter(pid=234) assert issubclass(w[-1].category, DeprecationWarning) assert "This function was renamed" in str(w[-1].message) + + +def test_value_matches_versionid(): + assert value_matches_versionid(234) is False, "integer is no version id" + assert value_matches_versionid("234") is False, ("string that only contains an integer is no " + "version id") + assert value_matches_versionid("234@bfe1a42cb37aae8ac625a757715d38814c274158") is True, ( + "integer is no version id") is True + with raises(ValueError): + value_matches_versionid(234.0) + p = Property(value=234) + assert p.value_matches_versionid() is False + p = Property(value="234@bfe1a42cb37aae8ac625a757715d38814c274158") + assert p.value_matches_versionid() is True + +def test_get_id_from_versionid(): + assert get_id_from_versionid("234@bfe1a42cb37aae8ac625a757715d38814c274158") == "234" + p = Property(value="234@bfe1a42cb37aae8ac625a757715d38814c274158") + assert p.get_id_from_versionid_value() == "234" + +def test_get_versionid(): + e = Entity(id=234) + e.version = Version(id="bfe1a42cb37aae8ac625a757715d38814c274158") + assert e.get_versionid() =="234@bfe1a42cb37aae8ac625a757715d38814c274158" -- GitLab From 85d807d332542485f12a3cdfd235a6b587f54c85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <h.tomwoerden@indiscale.com> Date: Fri, 24 Jan 2025 11:05:06 +0100 Subject: [PATCH 2/3] DOC: add docstrings --- src/linkahead/common/models.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/linkahead/common/models.py b/src/linkahead/common/models.py index 0912647..12d3617 100644 --- a/src/linkahead/common/models.py +++ b/src/linkahead/common/models.py @@ -506,6 +506,7 @@ class Entity: return self def get_versionid(self): + """Returns the concatenation of ID and version""" return str(self.id)+"@"+str(self.version.id) def get_importance(self, property): # @ReservedAssignment @@ -2131,9 +2132,11 @@ class Property(Entity): def value_matches_versionid(self): + """Returns True if the value matches the pattern <id>@<version>""" return value_matches_versionid(self.value) def get_id_from_versionid_value(self): + """Returns the ID part of the versionid with the pattern <id>@<version>""" return get_id_from_versionid(self.value) class Message(object): @@ -5682,6 +5685,7 @@ def _filter_entity_list_by_identity(listobject: list[Entity], return matches def value_matches_versionid(value: Union[int, str]): + """Returns True if the value matches the pattern <id>@<version>""" if isinstance(value, int): return False if not isinstance(value, str): @@ -5690,4 +5694,5 @@ def value_matches_versionid(value: Union[int, str]): return "@" in value def get_id_from_versionid(versionid: str): + """Returns the ID part of the versionid with the pattern <id>@<version>""" return versionid.split("@")[0] -- GitLab From 1a8c63724874076b633cce93e7d636c33ff0c125 Mon Sep 17 00:00:00 2001 From: Florian Spreckelsen <f.spreckelsen@indiscale.com> Date: Fri, 24 Jan 2025 14:08:53 +0100 Subject: [PATCH 3/3] STY: autopep8'd --- src/linkahead/common/models.py | 5 +++-- unittests/test_entity.py | 8 +++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/linkahead/common/models.py b/src/linkahead/common/models.py index 12d3617..bfe43b0 100644 --- a/src/linkahead/common/models.py +++ b/src/linkahead/common/models.py @@ -1958,7 +1958,6 @@ class QueryTemplate(): return len(self.get_errors()) > 0 - class Parent(Entity): """The parent entities.""" @@ -2130,7 +2129,6 @@ class Property(Entity): else: return is_reference(self.datatype) - def value_matches_versionid(self): """Returns True if the value matches the pattern <id>@<version>""" return value_matches_versionid(self.value) @@ -2139,6 +2137,7 @@ class Property(Entity): """Returns the ID part of the versionid with the pattern <id>@<version>""" return get_id_from_versionid(self.value) + class Message(object): def __init__( @@ -5684,6 +5683,7 @@ def _filter_entity_list_by_identity(listobject: list[Entity], matches.append(candidate) return matches + def value_matches_versionid(value: Union[int, str]): """Returns True if the value matches the pattern <id>@<version>""" if isinstance(value, int): @@ -5693,6 +5693,7 @@ def value_matches_versionid(value: Union[int, str]): "Did you call value_matches_versionid on a non reference value?") return "@" in value + def get_id_from_versionid(versionid: str): """Returns the ID part of the versionid with the pattern <id>@<version>""" return versionid.split("@")[0] diff --git a/unittests/test_entity.py b/unittests/test_entity.py index 722930b..2f41371 100644 --- a/unittests/test_entity.py +++ b/unittests/test_entity.py @@ -31,7 +31,7 @@ from linkahead import (INTEGER, Entity, Parent, Property, Record, RecordType, configure_connection) import warnings from linkahead.common.models import (SPECIAL_ATTRIBUTES, get_id_from_versionid, -value_matches_versionid) + value_matches_versionid) from linkahead.common.versioning import Version from linkahead.connection.mockup import MockUpServerConnection from lxml import etree @@ -302,7 +302,7 @@ def test_filter_by_identity(): def test_value_matches_versionid(): assert value_matches_versionid(234) is False, "integer is no version id" assert value_matches_versionid("234") is False, ("string that only contains an integer is no " - "version id") + "version id") assert value_matches_versionid("234@bfe1a42cb37aae8ac625a757715d38814c274158") is True, ( "integer is no version id") is True with raises(ValueError): @@ -312,12 +312,14 @@ def test_value_matches_versionid(): p = Property(value="234@bfe1a42cb37aae8ac625a757715d38814c274158") assert p.value_matches_versionid() is True + def test_get_id_from_versionid(): assert get_id_from_versionid("234@bfe1a42cb37aae8ac625a757715d38814c274158") == "234" p = Property(value="234@bfe1a42cb37aae8ac625a757715d38814c274158") assert p.get_id_from_versionid_value() == "234" + def test_get_versionid(): e = Entity(id=234) e.version = Version(id="bfe1a42cb37aae8ac625a757715d38814c274158") - assert e.get_versionid() =="234@bfe1a42cb37aae8ac625a757715d38814c274158" + assert e.get_versionid() == "234@bfe1a42cb37aae8ac625a757715d38814c274158" -- GitLab