Skip to content

Commit fff884d

Browse files
committed
[PRMP-990] update tests for stitched reference method to include S3 version id
1 parent b91caee commit fff884d

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

lambdas/services/pdf_stitching_service.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self):
4545
self.document_service = DocumentService()
4646
self.sqs_service = SQSService()
4747
self.multipart_references: list[DocumentReference] = []
48-
self.stitched_reference: DocumentReference = None
48+
self.stitched_reference: DocumentReference | None = None
4949

5050
def retrieve_multipart_references(
5151
self, nhs_number: str, doc_type: SupportedDocumentTypes
@@ -101,7 +101,7 @@ def process_message(self, stitching_message: PdfStitchingSqsMessage):
101101
stitch_file_size=sys.getsizeof(stitching_data_stream),
102102
)
103103
self.upload_stitched_file(stitching_data_stream=stitching_data_stream)
104-
self.update_stitched_reference()
104+
self.update_stitched_reference_with_version_id()
105105
self.migrate_multipart_references()
106106
self.write_stitching_reference()
107107
self.publish_nrl_message(
@@ -130,10 +130,9 @@ def create_stitched_reference(
130130
deep=True,
131131
)
132132

133-
def update_stitched_reference(self):
133+
def update_stitched_reference_with_version_id(self):
134134
self.stitched_reference.s3_version_id = self.s3_service.get_head_object(
135-
self.target_bucket,
136-
self.stitched_reference.s3_file_key
135+
self.target_bucket, self.stitched_reference.s3_file_key
137136
).get("VersionId")
138137

139138
def process_stitching(self, s3_object_keys: list[str]) -> BytesIO:
@@ -377,6 +376,6 @@ def process_manual_trigger(self, ods_code: str, queue_url):
377376
except (InvalidMessageException, Exception) as e:
378377
logger.error(f"Error sending batch to SQS: {str(e)}")
379378
# 1 batch is 10 messages
380-
# we can send up to 300 messages a second
381-
# a 0.1s delay means 10 batches, so 100 messages
379+
# we can send up to 300 messages a second.
380+
# a 0.1 s delay means 10 batches, so 100 messages
382381
time.sleep(0.1)

lambdas/tests/unit/services/test_pdf_stitching_service.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ def mock_upload_stitched_file(mocker, mock_service):
7171
return mocker.patch.object(mock_service, "upload_stitched_file")
7272

7373

74+
@pytest.fixture
75+
def mock_update_stitched_reference_with_version_id(mocker, mock_service):
76+
return mocker.patch.object(
77+
mock_service, "update_stitched_reference_with_version_id"
78+
)
79+
80+
7481
@pytest.fixture
7582
def mock_migrate_multipart_references(mocker, mock_service):
7683
return mocker.patch.object(mock_service, "migrate_multipart_references")
@@ -164,6 +171,7 @@ def test_process_message(
164171
mock_sort_multipart_object_keys,
165172
mock_process_stitching,
166173
mock_upload_stitched_file,
174+
mock_update_stitched_reference_with_version_id,
167175
mock_migrate_multipart_references,
168176
mock_write_stitching_reference,
169177
mock_publish_nrl_message,
@@ -193,6 +201,7 @@ def set_stitched_reference(document_reference, stitch_file_size, *args, **kwargs
193201
mock_sort_multipart_object_keys.assert_called_once_with()
194202
mock_process_stitching.assert_called_once_with(s3_object_keys=test_sorted_keys)
195203
mock_upload_stitched_file.assert_called_once_with(stitching_data_stream=test_stream)
204+
mock_update_stitched_reference_with_version_id.assert_called_once()
196205
mock_migrate_multipart_references.assert_called_once()
197206
mock_write_stitching_reference.assert_called_once()
198207
mock_publish_nrl_message.assert_called_once()
@@ -671,6 +680,24 @@ def test_rollback_reference_migration_handles_exception(mock_service):
671680
mock_service.rollback_reference_migration()
672681

673682

683+
def test_update_stitched_reference_with_version_id(mock_service):
684+
test_version_id = "test-version-id-12345"
685+
mock_service.stitched_reference = TEST_1_OF_1_DOCUMENT_REFERENCE
686+
687+
mock_service.s3_service.get_head_object.return_value = {
688+
"VersionId": test_version_id,
689+
"ContentType": "application/pdf",
690+
"ContentLength": 1234,
691+
}
692+
693+
mock_service.update_stitched_reference_with_version_id()
694+
695+
mock_service.s3_service.get_head_object.assert_called_once_with(
696+
MOCK_LG_BUCKET, TEST_1_OF_1_DOCUMENT_REFERENCE.s3_file_key
697+
)
698+
assert mock_service.stitched_reference.s3_version_id == test_version_id
699+
700+
674701
def test_process_manual_trigger_calls_process_message_for_each_nhs_number(
675702
mocker, mock_service
676703
):

0 commit comments

Comments
 (0)