1818 MOCK_ODS_CODE_TO_SUPPLIER
1919)
2020from tests .utils_for_tests .mock_environment_variables import MOCK_ENVIRONMENT_DICT , BucketNames , Sqs
21- from tests .utils_for_tests .values_for_tests import MOCK_CREATED_AT_FORMATTED_STRING , MockFileDetails
21+ from tests .utils_for_tests .values_for_tests import MOCK_CREATED_AT_FORMATTED_STRING , MockFileDetails , \
22+ MOCK_BATCH_FILE_CONTENT , MOCK_FILE_HEADERS
2223
2324# Ensure environment variables are mocked before importing from src files
2425with patch .dict ("os.environ" , MOCK_ENVIRONMENT_DICT ):
@@ -92,7 +93,7 @@ def make_record(file_key: str):
9293 @staticmethod
9394 def make_record_with_message_id (file_key : str , message_id : str ):
9495 """
95- Makes a record which includes a message_id, with the s3 bucket name set to BucketNames.SOURCE and and
96+ Makes a record which includes a message_id, with the s3 bucket name set to BucketNames.SOURCE and
9697 s3 object key set to the file_key.
9798 """
9899 return {"s3" : {"bucket" : {"name" : BucketNames .SOURCE }, "object" : {"key" : file_key }}, "message_id" : message_id }
@@ -180,7 +181,7 @@ def test_lambda_handler_new_file_success_and_first_in_queue(self):
180181 for file_details in test_cases :
181182 with self .subTest (file_details .name ):
182183 # Setup the file in the source bucket
183- s3_client .put_object (Bucket = BucketNames .SOURCE , Key = file_details .file_key )
184+ s3_client .put_object (Bucket = BucketNames .SOURCE , Key = file_details .file_key , Body = MOCK_BATCH_FILE_CONTENT )
184185
185186 with ( # noqa: E999
186187 patch ("file_name_processor.uuid4" , return_value = file_details .message_id ), # noqa: E999
@@ -208,7 +209,7 @@ def test_lambda_handler_new_file_success_and_other_files_in_queue(self):
208209 file_details = MockFileDetails .ravs_rsv_1
209210 file_already_processing_details = MockFileDetails .ravs_rsv_2
210211
211- s3_client .put_object (Bucket = BucketNames .SOURCE , Key = file_details .file_key )
212+ s3_client .put_object (Bucket = BucketNames .SOURCE , Key = file_details .file_key , Body = MOCK_BATCH_FILE_CONTENT )
212213
213214 add_entry_to_table (file_already_processing_details , FileStatus .PROCESSING )
214215
@@ -234,6 +235,8 @@ def test_lambda_handler_existing_file(self):
234235 file_details = MockFileDetails .ravs_rsv_1
235236 add_entry_to_table (file_details , FileStatus .QUEUED )
236237
238+ s3_client .put_object (Bucket = BucketNames .SOURCE , Key = file_details .file_key , Body = MOCK_BATCH_FILE_CONTENT )
239+
237240 with ( # noqa: E999
238241 patch ("file_name_processor.uuid4" , return_value = file_details .message_id ), # noqa: E999
239242 patch ("file_name_processor.invoke_filename_lambda" ) as mock_invoke_filename_lambda , # noqa: E999
@@ -248,6 +251,32 @@ def test_lambda_handler_existing_file(self):
248251 self .assert_no_ack_file (file_details )
249252 mock_invoke_filename_lambda .assert_not_called ()
250253
254+ def test_lambda_handler_correctly_flags_empty_file (self ):
255+ """
256+ VED-745 Tests that for an empty batch file:
257+ * The file status is updated to 'Not processed - empty file' in the audit table
258+ * The message is not sent to SQS
259+ * The failure inf_ack file is created
260+ """
261+ file_details = MockFileDetails .ravs_rsv_1
262+ add_entry_to_table (file_details , FileStatus .QUEUED )
263+
264+ s3_client .put_object (Bucket = BucketNames .SOURCE , Key = file_details .file_key , Body = MOCK_FILE_HEADERS )
265+
266+ with ( # noqa: E999
267+ patch ("file_name_processor.uuid4" , return_value = file_details .message_id ), # noqa: E999
268+ patch ("file_name_processor.invoke_filename_lambda" ) as mock_invoke_filename_lambda , # noqa: E999
269+ ): # noqa: E999
270+ lambda_handler (
271+ self .make_event ([self .make_record_with_message_id (file_details .file_key , file_details .message_id )]),
272+ None ,
273+ )
274+
275+ assert_audit_table_entry (file_details , FileStatus .EMPTY )
276+ self .assert_no_sqs_message ()
277+ self .assert_ack_file_contents (file_details )
278+ mock_invoke_filename_lambda .assert_not_called ()
279+
251280 def test_lambda_handler_non_root_file (self ):
252281 """
253282 Tests that when the file is not in the root of the source bucket, no action is taken:
@@ -280,7 +309,7 @@ def test_lambda_handler_duplicate_file_other_files_in_queue(self):
280309 * The invoke_filename_lambda method is not called
281310 """
282311 file_details = MockFileDetails .ravs_rsv_1
283- s3_client .put_object (Bucket = BucketNames .SOURCE , Key = file_details .file_key )
312+ s3_client .put_object (Bucket = BucketNames .SOURCE , Key = file_details .file_key , Body = MOCK_BATCH_FILE_CONTENT )
284313
285314 duplicate_already_in_table = deepcopy (file_details )
286315 duplicate_already_in_table .message_id = "duplicate_id"
@@ -312,7 +341,7 @@ def test_lambda_invalid_file_key_no_other_files_in_queue(self):
312341 * The invoke_filename_lambda method is not called
313342 """
314343 invalid_file_key = "InvalidVaccineType_Vaccinations_v5_YGM41_20240708T12130100.csv"
315- s3_client .put_object (Bucket = BucketNames .SOURCE , Key = invalid_file_key )
344+ s3_client .put_object (Bucket = BucketNames .SOURCE , Key = invalid_file_key , Body = MOCK_BATCH_FILE_CONTENT )
316345 file_details = deepcopy (MockFileDetails .ravs_rsv_1 )
317346 file_details .file_key = invalid_file_key
318347 file_details .ack_file_key = self .get_ack_file_key (invalid_file_key )
@@ -351,7 +380,7 @@ def test_lambda_invalid_permissions_other_files_in_queue(self):
351380 * The invoke_filename_lambda method is called with queued file details
352381 """
353382 file_details = MockFileDetails .ravs_rsv_1
354- s3_client .put_object (Bucket = BucketNames .SOURCE , Key = file_details .file_key )
383+ s3_client .put_object (Bucket = BucketNames .SOURCE , Key = file_details .file_key , Body = MOCK_BATCH_FILE_CONTENT )
355384
356385 queued_file_details = MockFileDetails .ravs_rsv_2
357386 add_entry_to_table (queued_file_details , FileStatus .QUEUED )
@@ -405,9 +434,10 @@ def test_lambda_handler_multiple_records_for_same_queue(self):
405434 invalid_file_details_3 .audit_table_entry [AuditTableKeys .QUEUE_NAME ] = {"S" : invalid_file_details_3 .queue_name }
406435 invalid_file_details_3 .sqs_message_body ["filename" ] = invalid_file_details_3 .file_key
407436
408- s3_client .put_object (Bucket = BucketNames .SOURCE , Key = valid_file_details_1 .file_key )
409- s3_client .put_object (Bucket = BucketNames .SOURCE , Key = valid_file_details_2 .file_key )
410- s3_client .put_object (Bucket = BucketNames .SOURCE , Key = invalid_file_details_3 .file_key )
437+ s3_client .put_object (Bucket = BucketNames .SOURCE , Key = valid_file_details_1 .file_key , Body = MOCK_BATCH_FILE_CONTENT )
438+ s3_client .put_object (Bucket = BucketNames .SOURCE , Key = valid_file_details_2 .file_key , Body = MOCK_BATCH_FILE_CONTENT )
439+ s3_client .put_object (Bucket = BucketNames .SOURCE , Key = invalid_file_details_3 .file_key ,
440+ Body = MOCK_BATCH_FILE_CONTENT )
411441
412442 message_ids = [
413443 valid_file_details_1 .message_id ,
0 commit comments