From 8e0119962bbf333712fe0bcc829b9fe4732b1511 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz <vlorentz@softwareheritage.org> Date: Fri, 26 Feb 2021 14:59:43 +0100 Subject: [PATCH] Add CoreSWHID.to_extended() This is a useful shorthand when generating SWHIDs in tests. --- swh/model/identifiers.py | 11 +++++++++++ swh/model/tests/test_identifiers.py | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/swh/model/identifiers.py b/swh/model/identifiers.py index e4598eb3..44daee52 100644 --- a/swh/model/identifiers.py +++ b/swh/model/identifiers.py @@ -857,6 +857,17 @@ class CoreSWHID(_BaseSWHID[ObjectType]): ) """the type of object the identifier points to""" + def to_extended(self) -> ExtendedSWHID: + """Converts this CoreSWHID into an ExtendedSWHID. + + As ExtendedSWHID is a superset of CoreSWHID, this is lossless.""" + return ExtendedSWHID( + namespace=self.namespace, + scheme_version=self.scheme_version, + object_type=ExtendedObjectType(self.object_type.value), + object_id=self.object_id, + ) + def _parse_core_swhid(swhid: Union[str, CoreSWHID, None]) -> Optional[CoreSWHID]: if swhid is None or isinstance(swhid, CoreSWHID): diff --git a/swh/model/tests/test_identifiers.py b/swh/model/tests/test_identifiers.py index 5fad92ba..a3a5295f 100644 --- a/swh/model/tests/test_identifiers.py +++ b/swh/model/tests/test_identifiers.py @@ -1368,6 +1368,18 @@ def test_parse_unparse_swhids(string, core, qualified, extended): assert string == str(parsed_swhid) +@pytest.mark.parametrize( + "core,extended", + [ + pytest.param(core, extended, id=string) + for (string, core, qualified, extended) in VALID_SWHIDS + if core is not None + ], +) +def test_core_to_extended(core, extended): + assert core.to_extended() == extended + + @pytest.mark.parametrize( "ns,version,type,id,qualifiers", [ -- GitLab