Skip to content

Commit

Permalink
Merge pull request #801 from nationalarchives/FCL-496-save-identifier…
Browse files Browse the repository at this point in the history
…-on-document

Use save_identifiers on document not identifiers.save()
  • Loading branch information
dragon-dxw authored Dec 12, 2024
2 parents d378fd8 + 94cf9ee commit bca1e2a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/caselawclient/models/documents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,11 @@ def can_reparse(self) -> bool:
"""
return self.docx_exists()

def save_identifiers(self) -> None:
"""Save the current state of this Document's identifiers to MarkLogic."""
self.identifiers.validate()
self.api_client.set_property_as_node(self.uri, "identifiers", self.identifiers.as_etree)

def __getattr__(self, name: str) -> Any:
warnings.warn(f"{name} no longer exists on Document, using Document.body instead", DeprecationWarning)
try:
Expand Down
5 changes: 0 additions & 5 deletions src/caselawclient/models/identifiers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,3 @@ def as_etree(self) -> etree._Element:
identifiers_root.append(identifier.as_xml_tree)

return identifiers_root

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)
20 changes: 19 additions & 1 deletion tests/models/documents/test_document_verbs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import json
import os
from unittest.mock import PropertyMock, patch
from unittest.mock import Mock, PropertyMock, patch

import pytest
import time_machine
Expand All @@ -13,10 +13,28 @@
DocumentNotSafeForDeletion,
DocumentURIString,
)
from caselawclient.models.identifiers import Identifiers
from caselawclient.models.judgments import Judgment
from caselawclient.models.neutral_citation_mixin import NeutralCitationString


class TestDocumentSaveIdentifiers:
def test_document_save_identifiers(self, mock_api_client):
"""
given a particular Document with a known value of Identifiers (probably mock this out tbh)
when I call document.save_identifiers() it
calls identifiers.validate and
calls set_property_as_node with expected values (edited)
"""
document = Document(DocumentURIString("test/1234"), mock_api_client)
document.identifiers = Mock(autospec=Identifiers)
document.identifiers.as_etree = "fake_node"

document.save_identifiers()
document.identifiers.validate.assert_called_once()
mock_api_client.set_property_as_node.assert_called_with("test/1234", "identifiers", "fake_node")


class TestDocumentPublish:
def test_publish_fails_if_not_publishable(self, mock_api_client):
with pytest.raises(CannotPublishUnpublishableDocument):
Expand Down

0 comments on commit bca1e2a

Please sign in to comment.