Skip to content

Commit

Permalink
Validate UUIDs make sense on save
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-dxw committed Dec 3, 2024
1 parent ae7b583 commit 084e566
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/caselawclient/models/identifiers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class InvalidIdentifierXMLRepresentationException(Exception):
pass


class UUIDMismatchError(Exception):
pass


class IdentifierSchema(ABC):
"""
A base class which describes what an identifier schema should look like.
Expand Down Expand Up @@ -99,6 +103,12 @@ def url_slug(self) -> str:


class Identifiers(dict[str, Identifier]):
def validate(self) -> None:
for uuid, identifier in self.items():
if uuid != identifier.uuid:
msg = "Key of {identifier} in Identifiers is {uuid} not {identifier.uuid}"
raise UUIDMismatchError(msg)

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

Expand All @@ -120,5 +130,5 @@ def as_etree(self) -> etree._Element:

def save(self, document) -> None: # type: ignore[no-untyped-def, unused-ignore]
"""Save the current state of this Document's identifiers to MarkLogic."""

self.validate()
document.api_client.set_property_as_node(document.uri, "identifiers", self.as_etree)
7 changes: 7 additions & 0 deletions tests/models/documents/test_document_identifiers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest
from lxml import etree

from caselawclient.factories import DocumentFactory
from caselawclient.models.identifiers import Identifiers, UUIDMismatchError
from tests.models.identifiers.test_identifiers import TestIdentifier


Expand All @@ -18,6 +20,11 @@ def test_add_identifiers(self):
"id-2": identifier_2,
}

def test_validate(self):
identifiers = Identifiers({"id-1": TestIdentifier(uuid="id-2", value="TEST-123")})
with pytest.raises(UUIDMismatchError):
identifiers.validate()

def test_identifiers_as_etree(self):
document = DocumentFactory.build()

Expand Down

0 comments on commit 084e566

Please sign in to comment.