From 73c29143f2df87519e0d996d8fc9a3365880e498 Mon Sep 17 00:00:00 2001 From: Nick Jackson Date: Mon, 16 Dec 2024 17:39:58 +0000 Subject: [PATCH] feat(FCL-532): add ability to retrieve identifiers by type This is needed to test for the existence of an FCLID before assigning a new one. --- src/caselawclient/models/identifiers/__init__.py | 5 +++++ tests/models/identifiers/test_identifiers.py | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/src/caselawclient/models/identifiers/__init__.py b/src/caselawclient/models/identifiers/__init__.py index 641f6f2c..d87a3015 100644 --- a/src/caselawclient/models/identifiers/__init__.py +++ b/src/caselawclient/models/identifiers/__init__.py @@ -127,6 +127,11 @@ def __delitem__(self, key: Union[Identifier, str]) -> None: else: super().__delitem__(key) + def of_type(self, identifier_type: type[Identifier]) -> list[Identifier]: + """Return a list of all identifiers of a given type.""" + uuids = self.keys() + return [self[uuid] for uuid in list(uuids) if isinstance(self[uuid], identifier_type)] + def delete_type(self, deleted_identifier_type: type[Identifier]) -> None: "For when we want an identifier to be the only valid identifier of that type, delete the others first" uuids = self.keys() diff --git a/tests/models/identifiers/test_identifiers.py b/tests/models/identifiers/test_identifiers.py index 6f630b0c..d317eae8 100644 --- a/tests/models/identifiers/test_identifiers.py +++ b/tests/models/identifiers/test_identifiers.py @@ -105,6 +105,12 @@ def test_contains(self, identifiers): assert not identifiers.contains(TestIdentifier(value="TEST-333")) assert not identifiers.contains(NeutralCitationNumber(value="TEST-111")) + def test_of_type(self, mixed_identifiers): + only_ncns = mixed_identifiers.of_type(NeutralCitationNumber) + assert "TEST-999" not in str(only_ncns) + assert "[1701] UKSC 999" in str(only_ncns) + assert "[1234] UKSC 999" in str(only_ncns) + def test_delete_type(self, mixed_identifiers): mixed_identifiers.delete_type(NeutralCitationNumber) assert "TEST-999" in str(mixed_identifiers)