|
1 | 1 | """Tests for lambda_handler""" |
2 | | - |
| 2 | +import sys |
3 | 3 | from unittest.mock import patch |
4 | 4 | from unittest import TestCase |
5 | 5 | from json import loads as json_loads |
@@ -154,6 +154,14 @@ def get_audit_table_items(): |
154 | 154 | """Return all items in the audit table""" |
155 | 155 | return dynamodb_client.scan(TableName=AUDIT_TABLE_NAME).get("Items", []) |
156 | 156 |
|
| 157 | + def test_lambda_handler_no_file_key_throws_exception(self): |
| 158 | + """Tests if exception is thrown when file_key is not provided""" |
| 159 | + |
| 160 | + broken_record = {"Records": [{"s3": {"bucket": {"name": "test"}}}]} |
| 161 | + with patch("file_name_processor.logger") as mock_logger: |
| 162 | + lambda_handler(broken_record, None) |
| 163 | + mock_logger.error.assert_called_once() |
| 164 | + |
157 | 165 | def test_lambda_handler_new_file_success_and_first_in_queue(self): |
158 | 166 | """ |
159 | 167 | Tests that for a new file, which passes validation and is the only file processing for the supplier_vaccineType |
@@ -528,3 +536,34 @@ def test_successful_processing_from_configs(self): |
528 | 536 | "supplier": ravs_rsv_file_details_2.supplier |
529 | 537 | } |
530 | 538 | self.assertEqual(result, expected_result) |
| 539 | + |
| 540 | + |
| 541 | +class TestMainEntryPoint(TestCase): |
| 542 | + def test_run_local_constructs_event_and_calls_lambda_handler(self): |
| 543 | + test_args = [ |
| 544 | + "file_name_processor.py", |
| 545 | + "--bucket", "test-bucket", |
| 546 | + "--key", "some/path/file.csv" |
| 547 | + ] |
| 548 | + |
| 549 | + expected_event = { |
| 550 | + "Records": [ |
| 551 | + { |
| 552 | + "s3": { |
| 553 | + "bucket": {"name": "test-bucket"}, |
| 554 | + "object": {"key": "some/path/file.csv"} |
| 555 | + } |
| 556 | + } |
| 557 | + ] |
| 558 | + } |
| 559 | + |
| 560 | + with ( |
| 561 | + patch.object(sys, "argv", test_args), |
| 562 | + patch("file_name_processor.lambda_handler") as mock_lambda_handler, |
| 563 | + patch("file_name_processor.print") as mock_print |
| 564 | + ): |
| 565 | + import file_name_processor |
| 566 | + file_name_processor.run_local() |
| 567 | + |
| 568 | + mock_lambda_handler.assert_called_once_with(event=expected_event, context={}) |
| 569 | + mock_print.assert_called() |
0 commit comments