Skip to content

Commit 2929db1

Browse files
committed
mock redis in exceptions
1 parent 82ee2db commit 2929db1

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

infrastructure/instance/s3_config.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,18 @@ resource "aws_s3_bucket_lifecycle_configuration" "datasources_lifecycle" {
8484
filter {
8585
prefix = "archive/"
8686
}
87+
expiration {
88+
days = 7
89+
}
90+
}
91+
92+
rule {
93+
id = "DeleteFinalFilesAfter7Days"
94+
status = "Enabled"
8795

96+
filter {
97+
prefix = "ea-archive/"
98+
}
8899
expiration {
89100
days = 7
90101
}

lambdas/filenameprocessor/src/file_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def validate_extended_attributes_file_key(file_key: str) -> str:
6464
):
6565
raise InvalidFileKeyError("Initial file validation failed: invalid file key")
6666

67-
return f"{organization_code}_COVID"
67+
return organization_code
6868

6969

7070
def validate_batch_file_key(file_key: str) -> tuple[str, str]:

lambdas/filenameprocessor/tests/test_lambda_handler.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ def test_lambda_handler_non_root_file(self):
245245
self.assert_no_sqs_message()
246246
self.assert_no_ack_file(file_details)
247247

248-
def test_lambda_handler_extended_attributes_success(self):
248+
@patch("elasticache.get_redis_client")
249+
def test_lambda_handler_extended_attributes_success(self, mock_get_redis_client):
249250
"""
250251
Tests that for an extended attributes file (prefix starts with 'Vaccination_Extended_Attributes'):
251252
* The file is added to the audit table with a status of 'Processed'
@@ -263,7 +264,11 @@ def test_lambda_handler_extended_attributes_success(self):
263264
Body=MOCK_EXTENDED_ATTRIBUTES_FILE_CONTENT,
264265
)
265266

266-
# TODO: rewrite the bucket patches to use moto
267+
# Mock Redis so EA validation passes: supplier present and COVID valid
268+
mock_redis = fakeredis.FakeStrictRedis()
269+
mock_redis.hget = Mock(side_effect=create_mock_hget({"X8E5B": "RAVS"}, {}))
270+
mock_redis.hkeys = Mock(return_value=["COVID", *all_vaccine_types_in_this_test_file])
271+
mock_get_redis_client.return_value = mock_redis
267272

268273
# Patch uuid4 (message id), and prevent external copy issues by simulating move
269274
with (
@@ -312,7 +317,8 @@ def test_lambda_handler_extended_attributes_success(self):
312317
self.assert_no_sqs_message()
313318
self.assert_no_ack_file(test_cases[0])
314319

315-
def test_lambda_handler_extended_attributes_failure(self):
320+
@patch("elasticache.get_redis_client")
321+
def test_lambda_handler_extended_attributes_failure(self, mock_get_redis_client):
316322
"""
317323
Tests that for an extended attributes file (prefix starts with 'Vaccination_Extended_Attributes'):
318324
Where the file has not been copied to the destination bucket
@@ -334,7 +340,11 @@ def test_lambda_handler_extended_attributes_failure(self):
334340
Body=MOCK_EXTENDED_ATTRIBUTES_FILE_CONTENT,
335341
)
336342

337-
# TODO: rewrite the bucket patches to use moto
343+
# Mock Redis so EA validation passes: supplier present and COVID valid
344+
mock_redis = fakeredis.FakeStrictRedis()
345+
mock_redis.hget = Mock(side_effect=create_mock_hget({"X8E5B": "RAVS"}, {}))
346+
mock_redis.hkeys = Mock(return_value=["COVID", *all_vaccine_types_in_this_test_file])
347+
mock_get_redis_client.return_value = mock_redis
338348

339349
# Patch uuid4 (message id), and raise an exception instead of moving the file.
340350
with (
@@ -612,7 +622,8 @@ def test_unexpected_bucket_name(self, mock_get_redis_client):
612622
self.assertIn(ravs_record.file_key, args)
613623
self.assertIn("unknown-bucket", args)
614624

615-
def test_unexpected_bucket_name_with_extended_attributes_file(self):
625+
@patch("elasticache.get_redis_client")
626+
def test_unexpected_bucket_name_with_extended_attributes_file(self, mock_get_redis_client):
616627
"""Tests if extended attributes file is handled when bucket name is incorrect"""
617628
valid_file_key = "Vaccination_Extended_Attributes_V1_5_X8E5B_20000101T00000001.csv"
618629
record = {
@@ -622,6 +633,12 @@ def test_unexpected_bucket_name_with_extended_attributes_file(self):
622633
}
623634
}
624635

636+
# Mock Redis so EA filename validation can succeed (supplier present and COVID is a valid vaccine type)
637+
mock_redis = Mock()
638+
mock_redis.hget.side_effect = create_mock_hget({"X8E5B": "RAVS"}, {})
639+
mock_redis.hkeys.return_value = ["COVID", *all_vaccine_types_in_this_test_file]
640+
mock_get_redis_client.return_value = mock_redis
641+
625642
with patch("file_name_processor.logger") as mock_logger:
626643
result = handle_record(record)
627644

lambdas/filenameprocessor/tests/utils_for_tests/values_for_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class MockFileDetails:
9393
emis_rsv = FileDetails("EMIS", "RSV", "YGM41")
9494
ravs_flu = FileDetails("RAVS", "FLU", "X8E5B")
9595
extended_attributes_file = FileDetails(
96-
vaccine_type="Vaccination_Extended_Attributes", file_number=1, organization_code="YGM41"
96+
vaccine_type="Vaccination_Extended_Attributes", file_number=1, organization_code="X8E5B"
9797
)
9898

9999

0 commit comments

Comments
 (0)