|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, MagicMock, call |
| 3 | +from io import StringIO |
| 4 | + |
| 5 | +import update_ack_file |
| 6 | +from update_ack_file import invoke_filename_lambda |
| 7 | + |
| 8 | +class TestUpdateAckFile(unittest.TestCase): |
| 9 | + def setUp(self): |
| 10 | + # Patch all AWS and external dependencies |
| 11 | + self.s3_client_patcher = patch('update_ack_file.s3_client') |
| 12 | + self.mock_s3_client = self.s3_client_patcher.start() |
| 13 | + |
| 14 | + self.logger_patcher = patch('update_ack_file.logger') |
| 15 | + self.mock_logger = self.logger_patcher.start() |
| 16 | + |
| 17 | + self.get_row_count_patcher = patch('update_ack_file.get_row_count') |
| 18 | + self.mock_get_row_count = self.get_row_count_patcher.start() |
| 19 | + |
| 20 | + self.change_audit_status_patcher = patch('update_ack_file.change_audit_table_status_to_processed') |
| 21 | + self.mock_change_audit_status = self.change_audit_status_patcher.start() |
| 22 | + |
| 23 | + self.get_next_queued_file_details_patcher = patch('update_ack_file.get_next_queued_file_details') |
| 24 | + self.mock_get_next_queued_file_details = self.get_next_queued_file_details_patcher.start() |
| 25 | + |
| 26 | + self.invoke_filename_lambda_patcher = patch('update_ack_file.invoke_filename_lambda') |
| 27 | + self.mock_invoke_filename_lambda = self.invoke_filename_lambda_patcher.start() |
| 28 | + |
| 29 | + self.lambda_client_patcher = patch('update_ack_file.lambda_client') |
| 30 | + self.mock_lambda_client = self.lambda_client_patcher.start() |
| 31 | + |
| 32 | + def tearDown(self): |
| 33 | + self.s3_client_patcher.stop() |
| 34 | + self.logger_patcher.stop() |
| 35 | + self.get_row_count_patcher.stop() |
| 36 | + self.change_audit_status_patcher.stop() |
| 37 | + self.get_next_queued_file_details_patcher.stop() |
| 38 | + self.invoke_filename_lambda_patcher.stop() |
| 39 | + self.lambda_client_patcher.stop() |
| 40 | + # Setup |
| 41 | + self.mock_get_row_count.side_effect = [3, 3] |
| 42 | + self.mock_get_next_queued_file_details.return_value = {"filename": "next_unique.csv", "message_id": "msg-unique-2"} |
| 43 | + accumulated_csv_content = StringIO("header1|header2\n") |
| 44 | + ack_data_rows = [ |
| 45 | + {"a": 1, "b": 2, "row": "happy1"}, |
| 46 | + {"a": 3, "b": 4, "row": "happy2"}, |
| 47 | + {"a": 5, "b": 6, "row": "happy3"} |
| 48 | + ] |
| 49 | + # Act |
| 50 | + update_ack_file.upload_ack_file( |
| 51 | + temp_ack_file_key="TempAck/test_happy_path.csv", |
| 52 | + message_id="msg-unique-1", |
| 53 | + supplier_queue="queue-unique-1", |
| 54 | + accumulated_csv_content=accumulated_csv_content, |
| 55 | + ack_data_rows=ack_data_rows, |
| 56 | + archive_ack_file_key="forwardedFile/test_happy_path.csv", |
| 57 | + file_key="test_happy_path.csv" |
| 58 | + ) |
| 59 | + # Assert |
| 60 | + self.mock_s3_client.upload_fileobj.assert_called_once() |
| 61 | + self.mock_get_next_queued_file_details.assert_called_once_with("queue-unique-1") |
| 62 | + self.mock_invoke_filename_lambda.assert_called_once_with("next_unique.csv", "msg-unique-2") |
| 63 | + self.mock_logger.info.assert_called() |
| 64 | + |
| 65 | + def test_audit_table_updated_correctly(self): |
| 66 | + """ VED-167 - Test that the audit table has been updated correctly""" |
| 67 | + # Setup |
| 68 | + self.mock_get_row_count.side_effect = [3, 3] |
| 69 | + accumulated_csv_content = StringIO("header1|header2\n") |
| 70 | + ack_data_rows = [ |
| 71 | + {"a": 1, "b": 2, "row": "audit-test-1"}, |
| 72 | + {"a": 3, "b": 4, "row": "audit-test-2"}, |
| 73 | + {"a": 5, "b": 6, "row": "audit-test-3"} |
| 74 | + ] |
| 75 | + message_id = "msg-audit-table" |
| 76 | + # Act |
| 77 | + update_ack_file.upload_ack_file( |
| 78 | + temp_ack_file_key="TempAck/audit_table_test.csv", |
| 79 | + message_id=message_id, |
| 80 | + supplier_queue="queue-audit-table", |
| 81 | + accumulated_csv_content=accumulated_csv_content, |
| 82 | + ack_data_rows=ack_data_rows, |
| 83 | + archive_ack_file_key="forwardedFile/audit_table_test.csv", |
| 84 | + file_key="audit_table_test.csv" |
| 85 | + ) |
| 86 | + # Assert: Only check audit table update |
| 87 | + self.mock_change_audit_status.assert_called_once_with("audit_table_test.csv", message_id) |
| 88 | + |
| 89 | + def test_move_file(self): |
| 90 | + """ VED-167 test that the file has been moved to the appropriate location """ |
| 91 | + update_ack_file.move_file("bucket-unique", "src/file_unique.csv", "dest/file_unique.csv") |
| 92 | + self.mock_s3_client.copy_object.assert_called_once_with( |
| 93 | + Bucket="bucket-unique", |
| 94 | + CopySource={"Bucket": "bucket-unique", "Key": "src/file_unique.csv"}, |
| 95 | + Key="dest/file_unique.csv" |
| 96 | + ) |
| 97 | + self.mock_s3_client.delete_object.assert_called_once_with(Bucket="bucket-unique", Key="src/file_unique.csv") |
| 98 | + self.mock_logger.info.assert_called_with("File moved from %s to %s", "src/file_unique.csv", "dest/file_unique.csv") |
| 99 | + |
| 100 | + def test_next_queued_file_triggers_lambda(self): |
| 101 | + """ VED-167 Test that the next queued file details are used to re-invoke the lambda.""" |
| 102 | + # Setup |
| 103 | + self.mock_get_row_count.side_effect = [3, 3] |
| 104 | + next_file = "next_for_lambda.csv" |
| 105 | + next_message_id = "msg-next-lambda" |
| 106 | + queue_name = "queue-lambda-trigger" |
| 107 | + self.mock_get_next_queued_file_details.return_value = {"filename": next_file, "message_id": next_message_id} |
| 108 | + accumulated_csv_content = StringIO("header1|header2\n") |
| 109 | + ack_data_rows = [ |
| 110 | + {"a": 1, "b": 2, "row": "lambda1"}, |
| 111 | + {"a": 3, "b": 4, "row": "lambda2"}, |
| 112 | + {"a": 5, "b": 6, "row": "lambda3"} |
| 113 | + ] |
| 114 | + # Act |
| 115 | + update_ack_file.upload_ack_file( |
| 116 | + temp_ack_file_key="TempAck/next_lambda_test.csv", |
| 117 | + message_id="msg-lambda-trigger", |
| 118 | + supplier_queue=queue_name, |
| 119 | + accumulated_csv_content=accumulated_csv_content, |
| 120 | + ack_data_rows=ack_data_rows, |
| 121 | + archive_ack_file_key="forwardedFile/next_lambda_test.csv", |
| 122 | + file_key="next_lambda_test.csv" |
| 123 | + ) |
| 124 | + # Assert: Check that the next queued file was used to re-invoke the lambda |
| 125 | + self.mock_get_next_queued_file_details.assert_called_once_with(queue_name) |
0 commit comments