Skip to content

Commit 043de2c

Browse files
committed
Add info log and fix minor edge case
1 parent 82cf6fe commit 043de2c

File tree

4 files changed

+47
-21
lines changed

4 files changed

+47
-21
lines changed

batch_processor_filter/src/batch_processor_filter_service.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def apply_filter(self, batch_file_created_event: BatchFileCreatedEvent) -> None:
3232
supplier = batch_file_created_event["supplier"]
3333
vaccine_type = batch_file_created_event["vaccine_type"]
3434

35+
logger.info("Received batch file event for filename: %s with message id: %s", filename, message_id)
36+
3537
if self._is_duplicate_file(filename):
3638
# Mark as processed and return without error so next event will be picked up from queue
3739
logger.error("A duplicate file has already been processed. Filename: %s", filename)
@@ -52,12 +54,12 @@ def apply_filter(self, batch_file_created_event: BatchFileCreatedEvent) -> None:
5254
raise EventAlreadyProcessingForSupplierAndVaccTypeError(f"Batch event already processing for supplier: "
5355
f"{supplier} and vacc type: {vaccine_type}")
5456

57+
self._batch_audit_repository.update_status(message_id, FileStatus.PROCESSING)
5558
self._queue_client.send_message(
5659
QueueUrl=QUEUE_URL,
5760
MessageBody=json.dumps(batch_file_created_event),
5861
MessageGroupId=f"{supplier}_{vaccine_type}"
5962
)
60-
self._batch_audit_repository.update_status(message_id, FileStatus.PROCESSING)
6163

6264
successful_log_message = f"File forwarded for processing by ECS. Filename: {filename}"
6365
logger.info(successful_log_message)

batch_processor_filter/tests/test_lambda_handler.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import boto3
55
import copy
66
from unittest import TestCase
7-
from unittest.mock import patch, Mock, ANY
7+
from unittest.mock import patch, Mock, ANY, call
88

99
import botocore
1010
from moto import mock_aws
@@ -206,10 +206,17 @@ def test_lambda_handler_raises_error_when_event_already_processing_for_supplier_
206206
sqs_messages = sqs_client.receive_message(QueueUrl=self.mock_queue_url)
207207
self.assertEqual(sqs_messages.get("Messages", []), [])
208208

209-
self.mock_logger.info.assert_called_once_with(
210-
"Batch event already processing for supplier and vacc type. Filename: %s",
211-
"Menacwy_Vaccinations_v5_TEST_20250826T15003000.csv"
212-
)
209+
self.mock_logger.info.assert_has_calls([
210+
call(
211+
"Received batch file event for filename: %s with message id: %s",
212+
"Menacwy_Vaccinations_v5_TEST_20250826T15003000.csv",
213+
"3b60c4f7-ef67-43c7-8f0d-4faee04d7d0e"
214+
),
215+
call(
216+
"Batch event already processing for supplier and vacc type. Filename: %s",
217+
"Menacwy_Vaccinations_v5_TEST_20250826T15003000.csv"
218+
)
219+
])
213220

214221
def test_lambda_handler_processes_event_successfully(self):
215222
"""Should update the audit entry status to Processing and forward to SQS"""
@@ -225,11 +232,20 @@ def test_lambda_handler_processes_event_successfully(self):
225232
self.assertEqual(len(sqs_messages.get("Messages", [])), 1)
226233
self.assertDictEqual(json.loads(sqs_messages["Messages"][0]["Body"]), dict(self.default_batch_file_event))
227234

228-
expected_log_message = (f"File forwarded for processing by ECS. Filename: "
229-
f"{self.default_batch_file_event['filename']}")
230-
self.mock_logger.info.assert_called_once_with(expected_log_message)
235+
expected_success_log_message = (f"File forwarded for processing by ECS. Filename: "
236+
f"{self.default_batch_file_event['filename']}")
237+
self.mock_logger.info.assert_has_calls([
238+
call(
239+
"Received batch file event for filename: %s with message id: %s",
240+
"Menacwy_Vaccinations_v5_TEST_20250820T10210000.csv",
241+
"df0b745c-b8cb-492c-ba84-8ea28d9f51d5"
242+
),
243+
call(
244+
expected_success_log_message
245+
)
246+
])
231247
self.mock_firehose_send_log.assert_called_once_with(
232-
{**self.default_batch_file_event, "message": expected_log_message}
248+
{**self.default_batch_file_event, "message": expected_success_log_message}
233249
)
234250

235251
def test_lambda_handler_processes_event_successfully_when_event_for_same_supplier_and_vacc_already_processed(self):
@@ -256,8 +272,17 @@ def test_lambda_handler_processes_event_successfully_when_event_for_same_supplie
256272
self.assertEqual(len(sqs_messages.get("Messages", [])), 1)
257273
self.assertDictEqual(json.loads(sqs_messages["Messages"][0]["Body"]), dict(test_event))
258274

259-
expected_log_message = f"File forwarded for processing by ECS. Filename: {test_event['filename']}"
260-
self.mock_logger.info.assert_called_once_with(expected_log_message)
275+
expected_success_log_message = f"File forwarded for processing by ECS. Filename: {test_event['filename']}"
276+
self.mock_logger.info.assert_has_calls([
277+
call(
278+
"Received batch file event for filename: %s with message id: %s",
279+
"Menacwy_Vaccinations_v5_TEST_20250826T15003000.csv",
280+
"3b60c4f7-ef67-43c7-8f0d-4faee04d7d0e"
281+
),
282+
call(
283+
expected_success_log_message
284+
)
285+
])
261286
self.mock_firehose_send_log.assert_called_once_with(
262-
{**test_event, "message": expected_log_message}
287+
{**test_event, "message": expected_success_log_message}
263288
)

filenameprocessor/src/file_name_processor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ def handle_record(record) -> dict:
7070
permissions = validate_vaccine_type_permissions(vaccine_type=vaccine_type, supplier=supplier)
7171

7272
queue_name = f"{supplier}_{vaccine_type}"
73-
make_and_send_sqs_message(
74-
file_key, message_id, permissions, vaccine_type, supplier, created_at_formatted_string
75-
)
7673
upsert_audit_table(
7774
message_id, file_key, created_at_formatted_string, expiry_timestamp, queue_name, FileStatus.QUEUED
7875
)
76+
make_and_send_sqs_message(
77+
file_key, message_id, permissions, vaccine_type, supplier, created_at_formatted_string
78+
)
7979

8080
logger.info("Lambda invocation successful for file '%s'", file_key)
8181

filenameprocessor/tests/test_lambda_handler.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from boto3 import client as boto3_client
1111
from moto import mock_s3, mock_sqs, mock_firehose, mock_dynamodb
1212

13-
from errors import UnhandledSqsError
1413
from tests.utils_for_tests.generic_setup_and_teardown import GenericSetUp, GenericTearDown
1514
from tests.utils_for_tests.utils_for_filenameprocessor_tests import (
1615
assert_audit_table_entry,
@@ -288,7 +287,7 @@ def test_lambda_invalid_permissions(self):
288287

289288
def test_lambda_adds_event_to_audit_table_as_failed_when_unexpected_exception_is_caught(self):
290289
"""
291-
Tests that when an unexpected error occurs e.g. sending to SQS (maybe in case of bad deployment):
290+
Tests that when an unexpected error occurs e.g. an unexpected exception when validating permissions:
292291
* The file is added to the audit table with a status of 'Failed' and the reason
293292
* The message is not sent to SQS
294293
* The failure inf_ack file is created
@@ -298,8 +297,8 @@ def test_lambda_adds_event_to_audit_table_as_failed_when_unexpected_exception_is
298297

299298
with ( # noqa: E999
300299
patch("file_name_processor.uuid4", return_value=test_file_details.message_id), # noqa: E999
301-
patch("file_name_processor.make_and_send_sqs_message", side_effect=UnhandledSqsError(
302-
"Some client error with SQS"
300+
patch("file_name_processor.validate_vaccine_type_permissions", side_effect=Exception(
301+
"Some unexpected exception"
303302
))
304303
): # noqa: E999
305304
lambda_handler(self.make_event([self.make_record(test_file_details.file_key)]), None)
@@ -310,7 +309,7 @@ def test_lambda_adds_event_to_audit_table_as_failed_when_unexpected_exception_is
310309
"filename": {"S": test_file_details.file_key},
311310
"queue_name": {"S": "EMIS_FLU"},
312311
"status": {"S": "Failed"},
313-
"error_details": {"S": "Some client error with SQS"},
312+
"error_details": {"S": "Some unexpected exception"},
314313
"timestamp": {"S": test_file_details.created_at_formatted_string},
315314
"expires_at": {"N": str(test_file_details.expires_at)},
316315
}

0 commit comments

Comments
 (0)