From 6fcaab09964703a87e0a299cc34a98b68e830653 Mon Sep 17 00:00:00 2001 From: Nick Jackson Date: Mon, 16 Dec 2024 17:57:00 +0000 Subject: [PATCH] feat(FCL-532): assign FCLIDs on document publication When documents are published they will now be assigned a new FCLID if one isn't already present. --- .../models/documents/__init__.py | 7 ++++ tests/conftest.py | 1 + tests/models/documents/test_document_verbs.py | 38 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/src/caselawclient/models/documents/__init__.py b/src/caselawclient/models/documents/__init__.py index 445698a8..6af19c3e 100644 --- a/src/caselawclient/models/documents/__init__.py +++ b/src/caselawclient/models/documents/__init__.py @@ -15,6 +15,7 @@ NotSupportedOnVersion, OnlySupportedOnVersion, ) +from caselawclient.models.identifiers.fclid import FindCaseLawIdentifier, FindCaseLawIdentifierSchema from caselawclient.models.identifiers.unpacker import unpack_all_identifiers_from_etree from caselawclient.models.utilities import VersionsDict, extract_version, render_versions from caselawclient.models.utilities.aws import ( @@ -432,6 +433,12 @@ def publish(self) -> None: if not self.is_publishable: raise CannotPublishUnpublishableDocument + ## If it doesn't already have one, get a new FCLID for this document and assign + if len(self.identifiers.of_type(FindCaseLawIdentifier)) < 1: + document_fclid = FindCaseLawIdentifierSchema.mint(self.api_client) + self.identifiers.add(document_fclid) + self.save_identifiers() + publish_documents(uri_for_s3(self.uri)) self.api_client.set_published(self.uri, True) announce_document_event( diff --git a/tests/conftest.py b/tests/conftest.py index bb88e3ef..1d3e954d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -130,5 +130,6 @@ def mock_api_client(): mock_client = Mock(spec=MarklogicApiClient) mock_client.get_judgment_xml_bytestring.return_value = b"content" mock_client.get_property_as_node.return_value = None + mock_client.get_next_document_sequence_number.return_value = 1 return mock_client diff --git a/tests/models/documents/test_document_verbs.py b/tests/models/documents/test_document_verbs.py index 5db45635..0b18ed1b 100644 --- a/tests/models/documents/test_document_verbs.py +++ b/tests/models/documents/test_document_verbs.py @@ -14,6 +14,7 @@ DocumentURIString, ) from caselawclient.models.identifiers import Identifiers +from caselawclient.models.identifiers.fclid import FindCaseLawIdentifier from caselawclient.models.judgments import Judgment from caselawclient.models.neutral_citation_mixin import NeutralCitationString @@ -64,6 +65,43 @@ def test_publish( ) mock_enrich.assert_called_once() + @patch("caselawclient.models.documents.announce_document_event") + @patch("caselawclient.models.documents.publish_documents") + @patch("caselawclient.models.documents.Document.enrich") + def test_publish_assigns_new_fclid( + self, + mock_enrich, + mock_publish_documents, + mock_announce_document_event, + mock_api_client, + ): + document = Document(DocumentURIString("test/1234"), mock_api_client) + document.is_publishable = True + mock_api_client.get_next_document_sequence_number.return_value = 123 + document.publish() + + assert len(document.identifiers.of_type(FindCaseLawIdentifier)) == 1 + assert [identifier.value for identifier in document.identifiers.of_type(FindCaseLawIdentifier)][0] == "z27xcnhr" + + @patch("caselawclient.models.documents.announce_document_event") + @patch("caselawclient.models.documents.publish_documents") + @patch("caselawclient.models.documents.Document.enrich") + def test_publish_only_assigns_new_fclid_if_none_present( + self, + mock_enrich, + mock_publish_documents, + mock_announce_document_event, + mock_api_client, + ): + document = Document(DocumentURIString("test/1234"), mock_api_client) + document.is_publishable = True + document.identifiers.add(FindCaseLawIdentifier(value="abcd1234")) + mock_api_client.get_next_document_sequence_number.return_value = 123 + document.publish() + + assert len(document.identifiers.of_type(FindCaseLawIdentifier)) == 1 + assert [identifier.value for identifier in document.identifiers.of_type(FindCaseLawIdentifier)][0] == "abcd1234" + class TestDocumentUnpublish: @patch("caselawclient.models.documents.announce_document_event")