Skip to content

Commit f756f51

Browse files
committed
Unit tests
1 parent b3c0f4a commit f756f51

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

filenameprocessor/src/file_name_processor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ def handle_record(record) -> dict:
6767
# Get message_id if the file is not new, else assign one
6868
message_id = record.get("message_id", str(uuid4()))
6969

70-
vaccine_type, supplier = validate_file_key(file_key)
71-
7270
created_at_formatted_string = get_created_at_formatted_string(bucket_name, file_key)
7371

72+
vaccine_type, supplier = validate_file_key(file_key)
73+
7474
permissions = validate_vaccine_type_permissions(vaccine_type=vaccine_type, supplier=supplier)
7575
if not is_existing_file:
7676
ensure_file_is_not_a_duplicate(file_key, created_at_formatted_string)
@@ -136,6 +136,8 @@ def handle_record(record) -> dict:
136136
"file_key": file_key,
137137
"message_id": message_id,
138138
"error": str(error),
139+
"vaccine_type": vaccine_type,
140+
"supplier": supplier
139141
}
140142

141143
elif "config" in bucket_name:

filenameprocessor/tests/test_lambda_handler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ def test_successful_processing_from_configs(self):
496496
"file_key": ravs_rsv_file_details_1.file_key,
497497
"message_id": ravs_rsv_file_details_1.message_id,
498498
"vaccine_type": ravs_rsv_file_details_1.vaccine_type,
499-
"supplier": ravs_rsv_file_details_1.supplier,
499+
"supplier": ravs_rsv_file_details_1.supplier
500500
}
501501
self.assertEqual(result, expected_result)
502502

@@ -524,5 +524,7 @@ def test_successful_processing_from_configs(self):
524524
"file_key": ravs_rsv_file_details_2.file_key,
525525
"message_id": ravs_rsv_file_details_2.message_id,
526526
"error": "Initial file validation failed: RAVS does not have permissions for RSV",
527+
"vaccine_type": ravs_rsv_file_details_2.vaccine_type,
528+
"supplier": ravs_rsv_file_details_2.supplier
527529
}
528530
self.assertEqual(result, expected_result)

filenameprocessor/tests/test_logging_decorator.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from copy import deepcopy
77
from contextlib import ExitStack
88
from boto3 import client as boto3_client
9+
from botocore.exceptions import ClientError
910
from moto import mock_s3, mock_firehose, mock_sqs, mock_dynamodb
1011

1112
from tests.utils_for_tests.generic_setup_and_teardown import GenericSetUp, GenericTearDown
@@ -192,13 +193,43 @@ def test_logging_failed_validation(self):
192193
"file_key": FILE_DETAILS.file_key,
193194
"message_id": FILE_DETAILS.message_id,
194195
"error": "Initial file validation failed: EMIS does not have permissions for FLU",
196+
"vaccine_type": "FLU",
197+
"supplier": "EMIS"
195198
}
196199

197200
log_data = json.loads(mock_logger.info.call_args[0][0])
198201
self.assertEqual(log_data, expected_log_data)
199202

200203
mock_send_log_to_firehose.assert_called_once_with(log_data)
201204

205+
def test_logging_throws_exception(self):
206+
"""Tests that the correct logs are sent to CloudWatch and Splunk when firehose throws an exception"""
207+
permissions_config_content = generate_permissions_config_content({"EMIS": ["COVID19_FULL"]})
208+
209+
firehose_exception = ClientError(
210+
error_response={"Error": {"Code": "ServiceUnavailable", "Message": "Service down"}},
211+
operation_name="PutRecord"
212+
)
213+
214+
with (
215+
patch("file_name_processor.uuid4", return_value=FILE_DETAILS.message_id),
216+
patch("elasticache.redis_client.get", return_value=permissions_config_content),
217+
patch("logging_decorator.firehose_client.put_record", side_effect=firehose_exception),
218+
patch("logging_decorator.logger") as mock_logger,
219+
):
220+
lambda_handler(MOCK_VACCINATION_EVENT, context=None)
221+
222+
# Assert logger.exception was called once
223+
mock_logger.exception.assert_called_once()
224+
225+
# Extract the call arguments
226+
exception_message = mock_logger.exception.call_args[0][0]
227+
exception_obj = mock_logger.exception.call_args[0][1]
228+
229+
# Check that the message format is correct
230+
self.assertIn("Error sending log to Firehose", exception_message)
231+
self.assertEqual(exception_obj, firehose_exception)
232+
202233
def test_logging_successful_config_upload(self):
203234
"""
204235
Tests that the correct logs are sent to cloudwatch and splunk when the config cache is successfully updated

0 commit comments

Comments
 (0)