Skip to content

Commit

Permalink
feat(FCL-532): add ability to retrieve identifiers by type
Browse files Browse the repository at this point in the history
This is needed to test for the existence of an FCLID before assigning a new one.
  • Loading branch information
jacksonj04 committed Dec 16, 2024
1 parent 77e6e62 commit 73c2914
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/caselawclient/models/identifiers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions tests/models/identifiers/test_identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 73c2914

Please sign in to comment.