Skip to content

Commit

Permalink
Compare two identifiers, only add if not present
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-dxw committed Dec 5, 2024
1 parent 629ae4a commit c5b6f9e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/caselawclient/models/identifiers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def as_xml_tree(self) -> etree._Element:
def url_slug(self) -> str:
return self.schema.compile_identifier_url_slug(self.value)

def similar(self, other: "Identifier") -> bool:
"Is this the same as another identifier (in value and schema)?"
return self.value == other.value and self.schema == other.schema


class Identifiers(dict[str, Identifier]):
def validate(self) -> None:
Expand All @@ -109,8 +113,13 @@ def validate(self) -> None:
msg = "Key of {identifier} in Identifiers is {uuid} not {identifier.uuid}"
raise UUIDMismatchError(msg)

def contains_similar(self, other_identifier: Identifier) -> bool:
"Do the identifier's value and namespace already exist in this group?"
return any(other_identifier.similar(identifier) for identifier in self.values())

def add(self, identifier: Identifier) -> None:
self[identifier.uuid] = identifier
if not self.contains_similar(identifier):
self[identifier.uuid] = identifier

def __delitem__(self, key: Union[Identifier, str]) -> None:
if isinstance(key, Identifier):
Expand Down
25 changes: 23 additions & 2 deletions tests/models/identifiers/test_identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
from lxml import etree

from caselawclient.models.identifiers import Identifier, Identifiers, IdentifierSchema
from caselawclient.models.identifiers.neutral_citation import NeutralCitationNumber


@pytest.fixture
def identifiers():
return Identifiers(
{"id-1": Identifier(uuid="id-1", value="TEST-111"), "id-2": Identifier(uuid="id-2", value="TEST-222")}
{"id-1": TestIdentifier(uuid="id-1", value="TEST-111"), "id-2": TestIdentifier(uuid="id-2", value="TEST-222")}
)


@pytest.fixture
def id3():
return Identifier(uuid="id-3", value="TEST-333")
return TestIdentifier(uuid="id-3", value="TEST-333")


class TestIdentifierSchema(IdentifierSchema):
Expand Down Expand Up @@ -55,6 +56,21 @@ def test_xml_representation(self):
etree.fromstring(expected_xml), strip_text=True
)

def test_similar_different_types(self):
id_a = TestIdentifier("X")
id_b = NeutralCitationNumber("X")
assert not id_a.similar(id_b)

def test_similar_same_type_and_value(self):
id_a = NeutralCitationNumber("X")
id_b = NeutralCitationNumber("X")
assert id_a.similar(id_b)

def test_similar_different_values(self):
id_a = NeutralCitationNumber("Y")
id_b = NeutralCitationNumber("X")
assert not id_a.similar(id_b)


class TestIdentifiersCRUD:
def test_delete(self, identifiers):
Expand All @@ -72,3 +88,8 @@ def test_add_identifier(self, identifiers, id3):
identifiers.add(id3)
assert identifiers["id-3"] == id3
assert len(identifiers) == 3

def test_contains(self, identifiers):
assert identifiers.contains_similar(TestIdentifier(value="TEST-111"))
assert not identifiers.contains_similar(TestIdentifier(value="TEST-333"))
assert not identifiers.contains_similar(NeutralCitationNumber(value="TEST-111"))

0 comments on commit c5b6f9e

Please sign in to comment.