From b88320c1b71d86b14ce3caaf75fe655788c999ec Mon Sep 17 00:00:00 2001 From: Dragon Dave McKee Date: Wed, 31 Jan 2024 15:38:50 +0000 Subject: [PATCH] Refactor log checking out --- ds-caselaw-ingester/lambda_function.py | 9 +++++---- ds-caselaw-ingester/tests.py | 28 +++++++++++--------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/ds-caselaw-ingester/lambda_function.py b/ds-caselaw-ingester/lambda_function.py index 9c8e03a..cd08bb1 100644 --- a/ds-caselaw-ingester/lambda_function.py +++ b/ds-caselaw-ingester/lambda_function.py @@ -160,6 +160,7 @@ class InvalidMessageException(ReportableException): class DocumentInsertionError(ReportableException): pass + class ErrorLogWouldOverwritePublishedDocument(ReportableException): pass @@ -499,11 +500,11 @@ def process_message(message): except DocumentNotFoundError: # the target does not exist target_published = False - allow_publish = ( - is_akoma # should we forbid publication because it's a parser error log? - ) + # forbid publication if it's a parser error log + # allow publication if it's looks like an akoma ntosa document + allow_publish = is_akoma - if target_published and not is_akoma: + if target_published and not allow_publish: """Do not publish: there is an existing published document, and we do not have a real document""" raise ErrorLogWouldOverwritePublishedDocument( f"XML for {uri} is a {xml.tag}; a published document already exists there." diff --git a/ds-caselaw-ingester/tests.py b/ds-caselaw-ingester/tests.py index 876e46b..33074eb 100644 --- a/ds-caselaw-ingester/tests.py +++ b/ds-caselaw-ingester/tests.py @@ -75,6 +75,17 @@ def create_fake_bulk_file(*args, **kwargs): shutil.copyfile(BULK_TARBALL_PATH, "/tmp/BULK-0.tar.gz") +def assert_log_sensible(log): + assert "Ingester Start: Consignment reference" in log + assert "tar.gz saved locally as" in log + assert "Ingesting document" in log + assert "Updated judgment xml" in log + assert "Upload Successful" in log + assert "Ingestion complete" in log + assert "Invalid XML file" not in log + assert "No XML file found" not in log + + class TestHandler: @patch("lambda_function.api_client", autospec=True) @patch("lambda_function.boto3.session.Session") @@ -107,15 +118,8 @@ def test_handler_messages_v2( lambda_function.handler(event=event, context=None) log = capsys.readouterr().out - assert "Ingester Start: Consignment reference TDR-2022-DNWR" in log - assert "tar.gz saved locally as /tmp/TDR-2022-DNWR.tar.gz" in log - assert "Ingesting document" in log - assert "Updated judgment xml" in log - assert "Upload Successful" in log - assert "Ingestion complete" in log + assert_log_sensible(log) assert "auto_publish" not in log - assert "Invalid XML file" not in log - assert "No XML file found" not in log assert "image1.png" in log notify_update.assert_called() assert notify_update.call_count == 2 @@ -161,15 +165,7 @@ def test_handler_messages_s3( lambda_function.handler(event=event, context=None) log = capsys.readouterr().out - assert "Ingester Start: Consignment reference BULK-0" in log - assert "tar.gz saved locally as /tmp/BULK-0.tar.gz" in log - assert "Ingesting document" in log - assert "Updated judgment xml" in log - assert "Upload Successful" in log - assert "Ingestion complete" in log assert "auto_publish" in log - assert "Invalid XML file" not in log - assert "No XML file found" not in log apiclient.set_published.assert_called_with("ukut/iac/2012/82", True) assert apiclient.set_published.call_count == 2 notify_new.assert_not_called()