Skip to content

Commit

Permalink
Merge pull request #102 from nationalarchives/v16-api-client-ready
Browse files Browse the repository at this point in the history
Add annotations and rename functions to make v16 compatible
  • Loading branch information
jacksonj04 authored Oct 13, 2023
2 parents 8abb04d + 06b3bc0 commit c7ab19b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
7 changes: 5 additions & 2 deletions ds-caselaw-ingester/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
MarklogicApiClient,
MarklogicResourceNotFoundError,
)
from caselawclient.client_helpers import VersionAnnotation, VersionType
from dotenv import load_dotenv
from notifications_python_client.notifications import NotificationsAPIClient

Expand Down Expand Up @@ -375,16 +376,18 @@ def parse_xml(xml) -> ET.Element:


def update_judgment_xml(uri, xml) -> bool:
annotation = VersionAnnotation(VersionType.SUBMISSION, "updated by ingester")
try:
api_client.get_judgment_xml(uri, show_unpublished=True)
api_client.save_judgment_xml(uri, xml)
api_client.update_document_xml(uri, xml, annotation)
return True
except MarklogicResourceNotFoundError:
return False


def insert_document_xml(uri, xml) -> bool:
api_client.insert_document_xml(uri, xml)
annotation = VersionAnnotation(VersionType.SUBMISSION, "inserted by ingester")
api_client.insert_document_xml(uri, xml, annotation)
return True


Expand Down
28 changes: 14 additions & 14 deletions ds-caselaw-ingester/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def create_fake_tdr_file(*args, **kwargs):


class TestHandler:
@patch("lambda_function.api_client")
@patch("lambda_function.extract_metadata")
@patch("lambda_function.api_client", autospec=True)
@patch("lambda_function.extract_metadata", autospec=True)
@patch("lambda_function.tarfile")
@patch("lambda_function.boto3.session.Session")
@patch("lambda_function.urllib3.PoolManager")
Expand Down Expand Up @@ -82,8 +82,8 @@ def test_handler_messages_v1(
assert "Upload Successful" in log
assert "Ingestion complete" in log

@patch("lambda_function.api_client")
@patch("lambda_function.extract_metadata")
@patch("lambda_function.api_client", autospec=True)
@patch("lambda_function.extract_metadata", autospec=True)
@patch("lambda_function.tarfile")
@patch("lambda_function.boto3.session.Session")
@patch("lambda_function.urllib3.PoolManager")
Expand Down Expand Up @@ -238,7 +238,7 @@ def test_extract_docx_filename_failure(self):
with pytest.raises(lambda_function.DocxFilenameNotFoundException):
lambda_function.extract_docx_filename(metadata, "anything")

@patch("lambda_function.api_client")
@patch("lambda_function.api_client", autospec=True)
def test_store_metadata(self, api_client):
metadata = {
"parameters": {
Expand Down Expand Up @@ -645,42 +645,42 @@ def test_malformed_message(self):
with pytest.raises(lambda_function.InvalidMessageException):
lambda_function.get_consignment_reference(message)

@patch("lambda_function.api_client")
@patch("lambda_function.api_client", autospec=True)
def test_update_judgment_xml_success(self, api_client):
xml = ET.XML("<xml>Here's some xml</xml>")
api_client.get_judgment_xml = MagicMock(return_value=True)
api_client.save_judgment_xml = MagicMock(return_value=True)
api_client.update_document_xml = MagicMock(return_value=True)
result = lambda_function.update_judgment_xml("a/fake/uri", xml)
assert result is True

@patch("lambda_function.api_client")
@patch("lambda_function.api_client", autospec=True)
def test_update_judgment_xml_judgment_does_not_exist(self, api_client):
xml = ET.XML("<xml>Here's some xml</xml>")
api_client.get_judgment_xml = MagicMock(
side_effect=MarklogicResourceNotFoundError("error")
)
api_client.save_judgment_xml = MagicMock(return_value=True)
api_client.update_document_xml = MagicMock(return_value=True)
result = lambda_function.update_judgment_xml("a/fake/uri", xml)
assert result is False

@patch("lambda_function.api_client")
@patch("lambda_function.api_client", autospec=True)
def test_update_judgment_xml_judgment_does_not_save(self, api_client):
xml = ET.XML("<xml>Here's some xml</xml>")
api_client.get_judgment_xml = MagicMock(return_value=True)
api_client.save_judgment_xml = MagicMock(
api_client.update_document_xml = MagicMock(
side_effect=MarklogicCommunicationError("error")
)
with pytest.raises(MarklogicCommunicationError):
lambda_function.update_judgment_xml("a/fake/uri", xml)

@patch("lambda_function.api_client")
@patch("lambda_function.api_client", autospec=True)
def test_insert_document_xml_success(self, api_client):
xml = ET.XML("<xml>Here's some xml</xml>")
api_client.insert_document_xml = MagicMock(return_value=True)
result = lambda_function.insert_document_xml("a/fake/uri", xml)
assert result is True

@patch("lambda_function.api_client")
@patch("lambda_function.api_client", autospec=True)
def test_insert_document_xml_failure(self, api_client):
xml = ET.XML("<xml>Here's some xml</xml>")
api_client.insert_document_xml = MagicMock(
Expand Down Expand Up @@ -764,7 +764,7 @@ def test_get_best_xml_with_no_xml_file(self):
assert result.__class__ == ET.Element
assert result.tag == "error"

@patch("lambda_function.api_client")
@patch("lambda_function.api_client", autospec=True)
def test_unpublish_updated_judgment(self, api_client):
uri = "a/fake/uri"
api_client.set_published = MagicMock()
Expand Down
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
django-environ~=0.10
ds-caselaw-marklogic-api-client==14.1.0
ds-caselaw-marklogic-api-client==16.0.0
requests-toolbelt~=1.0
urllib3~=1.26
boto3
Expand Down

0 comments on commit c7ab19b

Please sign in to comment.