Skip to content

Commit af0bdf0

Browse files
committed
Unit tests
1 parent 3942b31 commit af0bdf0

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

filenameprocessor/src/file_name_processor.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def lambda_handler(event: dict, context) -> None: # pylint: disable=unused-argu
170170
logger.info("Filename processor lambda task completed")
171171

172172

173-
if __name__ == "__main__":
173+
def run_local():
174174
parser = argparse.ArgumentParser("file_name_processor")
175175
parser.add_argument("--bucket", required=True, help="Bucket name.", type=str)
176176
parser.add_argument("--key", required=True, help="Object key.", type=str)
@@ -180,15 +180,15 @@ def lambda_handler(event: dict, context) -> None: # pylint: disable=unused-argu
180180
"Records": [
181181
{
182182
"s3": {
183-
"bucket": {
184-
"name": args.bucket
185-
},
186-
"object": {
187-
"key": args.key
188-
}
183+
"bucket": {"name": args.bucket},
184+
"object": {"key": args.key}
189185
}
190186
}
191187
]
192188
}
193189
print(event)
194190
print(lambda_handler(event=event, context={}))
191+
192+
193+
if __name__ == "__main__":
194+
run_local()

filenameprocessor/tests/test_lambda_handler.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Tests for lambda_handler"""
2-
2+
import sys
33
from unittest.mock import patch
44
from unittest import TestCase
55
from json import loads as json_loads
@@ -154,6 +154,14 @@ def get_audit_table_items():
154154
"""Return all items in the audit table"""
155155
return dynamodb_client.scan(TableName=AUDIT_TABLE_NAME).get("Items", [])
156156

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+
157165
def test_lambda_handler_new_file_success_and_first_in_queue(self):
158166
"""
159167
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):
528536
"supplier": ravs_rsv_file_details_2.supplier
529537
}
530538
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

Comments
 (0)