Skip to content

Commit 0719a0d

Browse files
Merge pull request #338 from NHSDigital/AMB-2382_TEST_Audit_table_file_movement_file_name_processor
Amb 2382 test audit table file movement file name processor
2 parents 53866ca + 4e185ac commit 0719a0d

12 files changed

+748
-257
lines changed

filenameprocessor/src/audit_table.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ def upsert_audit_table(
5757
Key={AuditTableKeys.MESSAGE_ID: {"S": message_id}},
5858
UpdateExpression="SET #status = :status",
5959
ExpressionAttributeNames={"#status": "status"},
60-
# TODO: Should this be set to file_status? The status may be 'processed' due to an exception occuring
61-
ExpressionAttributeValues={":status": {"S": FileStatus.PROCESSING}},
60+
ExpressionAttributeValues={":status": {"S": file_status}},
6261
ConditionExpression="attribute_exists(message_id)",
6362
)
6463
logger.info("%s file set for processing, and the status successfully updated in audit table", file_key)

filenameprocessor/src/file_name_processor.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ def handle_record(record) -> dict:
4444
logger.error("Error obtaining file_key: %s", error)
4545
return {"statusCode": 500, "message": "Failed to download file key", "error": str(error)}
4646

47-
# The lambda is unintentionally invoked when a file is moved into a different folder in the source bucket.
48-
# Excluding file keys containing a "/" is a workaround to prevent the lambda from processing files that
49-
# are not in the root of the source bucket.
50-
if "data-sources" in bucket_name and "/" not in file_key:
47+
if "data-sources" in bucket_name:
48+
49+
# The lambda is unintentionally invoked when a file is moved into a different folder in the source bucket.
50+
# Excluding file keys containing a "/" is a workaround to prevent the lambda from processing files that
51+
# are not in the root of the source bucket.
52+
if "/" in file_key:
53+
message = "File skipped due to duplicate lambda invoaction"
54+
return {"statusCode": 200, "message": message, "file_key": file_key}
5155

5256
# Set default values for file-specific variables
5357
message_id = "Message id was not created"

filenameprocessor/tests/test_audit_table.py

Lines changed: 185 additions & 115 deletions
Large diffs are not rendered by default.

filenameprocessor/tests/test_elasticache.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,27 @@ def test_permissions_caching(self):
3737
Test that upload_to_elasticache successfully uploads the file to elasticache, which is then successfully read
3838
by get_permissions_config_json_from_cache
3939
"""
40-
mock_permissions = {"test_supplier_1": ["RSV_FULL"], "test_supplier_2": ["FLU_CREATE", "FLU_UPDATE"]}
41-
mock_permissions_config = generate_permissions_config_content(mock_permissions)
42-
s3_client.put_object(Bucket=BucketNames.CONFIG, Key=PERMISSIONS_CONFIG_FILE_KEY, Body=mock_permissions_config)
40+
mock_permissions_1 = {"test_supplier_1": ["RSV_FULL"], "test_supplier_2": ["FLU_CREATE", "FLU_UPDATE"]}
41+
mock_permissions_config_1 = generate_permissions_config_content(mock_permissions_1)
42+
43+
mock_permissions_2 = {
44+
"test_supplier_1": ["FLU_FULL"],
45+
"test_supplier_2": ["RSV_CREATE"],
46+
"test_supplier_3": ["RSV_UPDATE"],
47+
}
48+
mock_permissions_config_2 = generate_permissions_config_content(mock_permissions_2)
4349

4450
with patch("elasticache.redis_client", fakeredis.FakeStrictRedis()):
51+
# Test that the permissions config is successfully uploaded to elasticache
52+
s3_client.put_object(
53+
Bucket=BucketNames.CONFIG, Key=PERMISSIONS_CONFIG_FILE_KEY, Body=mock_permissions_config_1
54+
)
55+
upload_to_elasticache(PERMISSIONS_CONFIG_FILE_KEY, BucketNames.CONFIG)
56+
self.assertEqual(get_permissions_config_json_from_cache(), {"all_permissions": mock_permissions_1})
57+
58+
# Test that the cache is updated with the new permissions config
59+
s3_client.put_object(
60+
Bucket=BucketNames.CONFIG, Key=PERMISSIONS_CONFIG_FILE_KEY, Body=mock_permissions_config_2
61+
)
4562
upload_to_elasticache(PERMISSIONS_CONFIG_FILE_KEY, BucketNames.CONFIG)
46-
self.assertEqual(get_permissions_config_json_from_cache(), {"all_permissions": mock_permissions})
63+
self.assertEqual(get_permissions_config_json_from_cache(), {"all_permissions": mock_permissions_2})

filenameprocessor/tests/test_file_key_validation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from file_key_validation import is_valid_datetime, validate_file_key
1212
from errors import InvalidFileKeyError
1313

14-
VALID_FLU_EMIS_FILE_KEY = MockFileDetails.flu_emis.file_key
15-
VALID_RSV_RAVS_FILE_KEY = MockFileDetails.rsv_ravs.file_key
14+
VALID_FLU_EMIS_FILE_KEY = MockFileDetails.emis_flu.file_key
15+
VALID_RSV_RAVS_FILE_KEY = MockFileDetails.ravs_rsv_1.file_key
1616

1717

1818
class TestFileKeyValidation(TestCase):
@@ -89,9 +89,9 @@ def test_validate_file_key(self):
8989
# File key with missing ODS code
9090
(VALID_FLU_EMIS_FILE_KEY.replace("YGM41", ""), invalid_file_key_error_message),
9191
# File key with invalid timestamp
92-
(VALID_FLU_EMIS_FILE_KEY.replace("20240708T12130100", "20200132T12345600"), invalid_file_key_error_message),
92+
(VALID_FLU_EMIS_FILE_KEY.replace("20000101T00000001", "20200132T12345600"), invalid_file_key_error_message),
9393
# File key with missing timestamp
94-
(VALID_FLU_EMIS_FILE_KEY.replace("20240708T12130100", ""), invalid_file_key_error_message),
94+
(VALID_FLU_EMIS_FILE_KEY.replace("20000101T00000001", ""), invalid_file_key_error_message),
9595
# File key with incorrect extension
9696
(VALID_FLU_EMIS_FILE_KEY.replace(".csv", ".dat"), invalid_file_key_error_message),
9797
]

0 commit comments

Comments
 (0)