Skip to content

Commit

Permalink
Add s3 test
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-dxw committed Nov 2, 2023
1 parent ad22ebc commit 6e4ba62
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ send-message-v2:
@awslocal sns publish --topic-arn arn:aws:sns:us-east-1:000000000000:judgments --message file://aws_examples/sns/parsed-judgment-v2.json

send-message-s3:
@awslocal s3 cp aws_examples/s3/te-editorial-out-int/test2.tar.gz s3://inbound-bucket/QX/e31b117f-ff09-49b6-a697-7952c7a67384/QX.tar.gz
@awslocal s3 cp aws_examples/s3/te-editorial-out-int/test3.tar.gz s3://inbound-bucket/QX/e31b117f-ff09-49b6-a697-7952c7a67384/QX.tar.gz


delete-document:
Expand Down
Binary file removed aws_examples/s3/te-editorial-out-int/test2.tar.gz
Binary file not shown.
Binary file added aws_examples/s3/te-editorial-out-int/test3.tar.gz
Binary file not shown.
76 changes: 76 additions & 0 deletions ds-caselaw-ingester/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,23 @@
}
"""

s3_message = {
"Records": [
{
"eventSource": "aws:s3",
"s3": {
"bucket": {
"name": "staging-tre-court-document-pack-out",
},
"object": {
"key": "QX/e31b117f-ff09-49b6-a697-7952c7a67384/FCL-12345.tar.gz",
},
},
}
]
}
v2_message = json.loads(v2_message_raw)
s3_message_raw = json.dumps(s3_message)


def create_fake_tdr_file(*args, **kwargs):
Expand Down Expand Up @@ -142,6 +158,66 @@ def test_handler_messages_v2(
assert "Upload Successful" in log
assert "Ingestion complete" in log

@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")
def test_handler_messages_s3(
self, urllib_pool, boto_session, tarfile, metadata, apiclient, capsys
):
"""Mostly intended as a very sketchy test of the primary function"""
urllib_pool.return_value.request.return_value.status = 200
urllib_pool.return_value.request.return_value.data = b"data"
tarfile.open.return_value.getmembers().return_value.name.extractfile.return_value = (
b"3"
)
boto_session.return_value.client.return_value.download_file = (
create_fake_tdr_file
)
metadata.return_value = {
"parameters": {
"TRE": {
"reference": "TDR-2020-FAR",
"payload": {
"xml": "",
"filename": "temp.docx",
"images": [],
},
},
"TDR": {
"Source-Organization": "",
"Contact-Name": "",
"Contact-Email": "",
"Internal-Sender-Identifier": "",
"Consignment-Completed-Datetime": "",
},
"PARSER": {"uri": ""},
}
}

# metadata.return_value = {
# "uri": "https://caselaw.nationalarchives.gov.uk/id/eat/2022/1",
# "court": "EAT",
# "cite": "[2022] EAT 1",
# "date": "2021-09-28",
# "name": "SECRETARY OF STATE FOR JUSTICE v MR ALAN JOHNSON",
# "attachments": [],
# }

message = s3_message_raw
event = {"Records": [{"Sns": {"Message": message}}]}
lambda_function.handler(event=event, context=None)

log = capsys.readouterr().out
assert "Ingester Start: Consignment reference FCL-12345" in log
assert "v1: False" in log
assert "tar.gz saved locally as /tmp/FCL-12345.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


class TestLambda:
TDR_TARBALL_PATH = os.path.join(
Expand Down

0 comments on commit 6e4ba62

Please sign in to comment.