Skip to content

Commit 97854c4

Browse files
authored
Update dependencies and refactor tests to use unified mock_aws decorator (#1398)
- Bump `moto` version to 5.1.22 in both `poetry.lock` and `pyproject.toml` for improved AWS mocking capabilities. - Update Python version requirements for `moto` and other dependencies in `poetry.lock`. - Refactor test files to replace individual service mocks (e.g., `mock_s3`, `mock_dynamodb`) with a unified `mock_aws` decorator for cleaner test setup. - Introduce helper functions for creating Boto3 clients and resetting common clients to streamline test initialization. - Update environment variable mocks to include AWS credentials for testing purposes.
1 parent 06384ce commit 97854c4

20 files changed

Lines changed: 234 additions & 157 deletions

lambdas/filenameprocessor/poetry.lock

Lines changed: 27 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lambdas/filenameprocessor/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ python = "~3.11"
1515
boto3 = "~1.42.74"
1616
boto3-stubs-lite = { extras = ["dynamodb"], version = "~1.42.74" }
1717
aws-lambda-typing = "~2.20.0"
18-
moto = "^4"
18+
moto = "^5.1.22"
1919
requests = "~2.32.5"
2020
responses = "~0.26.0"
2121
pydantic = "~1.10.13"

lambdas/filenameprocessor/tests/test_elasticache.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,37 @@
44
from unittest import TestCase
55
from unittest.mock import Mock, patch
66

7-
from boto3 import client as boto3_client
8-
from moto import mock_s3
7+
from moto import mock_aws
98

109
from utils_for_tests.mock_environment_variables import MOCK_ENVIRONMENT_DICT
1110
from utils_for_tests.utils_for_filenameprocessor_tests import (
1211
GenericSetUp,
1312
GenericTearDown,
13+
create_boto3_clients,
1414
create_mock_hget,
1515
)
1616

1717
# Ensure environment variables are mocked before importing from src files
1818
with patch.dict("os.environ", MOCK_ENVIRONMENT_DICT):
19-
from common.clients import REGION_NAME
2019
from elasticache import (
2120
get_supplier_permissions_from_cache,
2221
get_supplier_system_from_cache,
2322
get_valid_vaccine_types_from_cache,
2423
)
2524

26-
s3_client = boto3_client("s3", region_name=REGION_NAME)
25+
s3_client = None
2726

2827

29-
@mock_s3
28+
@mock_aws
3029
@patch.dict("os.environ", MOCK_ENVIRONMENT_DICT)
3130
@patch("elasticache.get_redis_client")
3231
class TestElasticache(TestCase):
3332
"""Tests for elasticache functions"""
3433

3534
def setUp(self):
3635
"""Set up the S3 buckets"""
36+
global s3_client
37+
(s3_client,) = create_boto3_clients("s3")
3738
GenericSetUp(s3_client)
3839

3940
def tearDown(self):

lambdas/filenameprocessor/tests/test_lambda_handler.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
from unittest.mock import ANY, Mock, patch
1010

1111
import fakeredis
12-
from boto3 import client as boto3_client
13-
from moto import mock_dynamodb, mock_firehose, mock_s3, mock_sqs
12+
from moto import mock_aws
1413

1514
from utils_for_tests.mock_environment_variables import (
1615
MOCK_ENVIRONMENT_DICT,
@@ -22,6 +21,7 @@
2221
GenericSetUp,
2322
GenericTearDown,
2423
assert_audit_table_entry,
24+
create_boto3_clients,
2525
create_mock_hget,
2626
)
2727
from utils_for_tests.values_for_tests import (
@@ -34,15 +34,14 @@
3434

3535
# Ensure environment variables are mocked before importing from src files
3636
with patch.dict("os.environ", MOCK_ENVIRONMENT_DICT):
37-
from common.clients import REGION_NAME
3837
from common.models.batch_constants import AUDIT_TABLE_NAME, AuditTableKeys, FileStatus
3938
from constants import EXTENDED_ATTRIBUTES_VACC_TYPE
4039
from file_name_processor import handle_record, lambda_handler
4140

42-
s3_client = boto3_client("s3", region_name=REGION_NAME)
43-
sqs_client = boto3_client("sqs", region_name=REGION_NAME)
44-
firehose_client = boto3_client("firehose", region_name=REGION_NAME)
45-
dynamodb_client = boto3_client("dynamodb", region_name=REGION_NAME)
41+
s3_client = None
42+
sqs_client = None
43+
firehose_client = None
44+
dynamodb_client = None
4645

4746
# NOTE: The default throughout these tests is to use permissions config which allows all suppliers full permissions
4847
# for all vaccine types. This default is overridden for some specific tests.
@@ -52,10 +51,7 @@
5251

5352

5453
@patch.dict("os.environ", MOCK_ENVIRONMENT_DICT)
55-
@mock_s3
56-
@mock_sqs
57-
@mock_firehose
58-
@mock_dynamodb
54+
@mock_aws
5955
class TestLambdaHandlerDataSource(TestCase):
6056
"""Tests for lambda_handler when a data sources (vaccine data) file is received."""
6157

@@ -87,6 +83,10 @@ def run(self, result=None):
8783
super().run(result)
8884

8985
def setUp(self):
86+
global s3_client, sqs_client, firehose_client, dynamodb_client
87+
s3_client, sqs_client, firehose_client, dynamodb_client = create_boto3_clients(
88+
"s3", "sqs", "firehose", "dynamodb"
89+
)
9090
GenericSetUp(s3_client, firehose_client, sqs_client, dynamodb_client)
9191
self.logger_patcher = patch("file_name_processor.logger")
9292
self.mock_logger = self.logger_patcher.start()
@@ -483,7 +483,9 @@ def test_lambda_handler_extended_attributes_extension_checks(self, mock_get_redi
483483
s3_client.put_object(Bucket=BucketNames.SOURCE, Key=bad_ext_key, Body=MOCK_EXTENDED_ATTRIBUTES_FILE_CONTENT)
484484
with patch("file_name_processor.uuid4", return_value="EA_bad_ext_id"):
485485
lambda_handler(self.make_event([self.make_record(bad_ext_key)]), None)
486-
item = self.get_audit_table_items()[-1]
486+
item = next(
487+
item for item in self.get_audit_table_items() if item[AuditTableKeys.MESSAGE_ID]["S"] == "EA_bad_ext_id"
488+
)
487489
self.assertEqual(item[AuditTableKeys.STATUS]["S"], "Failed")
488490
s3_client.get_object(Bucket=BucketNames.SOURCE, Key=f"extended-attributes-archive/{bad_ext_key}")
489491
"""
@@ -673,14 +675,15 @@ def test_lambda_adds_event_to_audit_table_as_failed_when_unexpected_exception_is
673675

674676

675677
@patch.dict("os.environ", MOCK_ENVIRONMENT_DICT)
676-
@mock_s3
677-
@mock_dynamodb
678-
@mock_sqs
679-
@mock_firehose
678+
@mock_aws
680679
class TestUnexpectedBucket(TestCase):
681680
"""Tests for lambda_handler when an unexpected bucket name is used"""
682681

683682
def setUp(self):
683+
global s3_client, sqs_client, firehose_client, dynamodb_client
684+
s3_client, sqs_client, firehose_client, dynamodb_client = create_boto3_clients(
685+
"s3", "sqs", "firehose", "dynamodb"
686+
)
684687
GenericSetUp(s3_client, firehose_client, sqs_client, dynamodb_client)
685688

686689
def tearDown(self):

lambdas/filenameprocessor/tests/test_send_sqs_message.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,38 @@
55
from unittest import TestCase
66
from unittest.mock import patch
77

8-
from boto3 import client as boto3_client
9-
from moto import mock_sqs
8+
from moto import mock_aws
109

1110
from utils_for_tests.mock_environment_variables import MOCK_ENVIRONMENT_DICT, Sqs
11+
from utils_for_tests.utils_for_filenameprocessor_tests import create_boto3_clients, reset_common_clients
1212
from utils_for_tests.values_for_tests import MockFileDetails
1313

1414
# Ensure environment variables are mocked before importing from src files
1515
with patch.dict("os.environ", MOCK_ENVIRONMENT_DICT):
16-
from common.clients import REGION_NAME
1716
from models.errors import UnhandledSqsError
1817
from send_sqs_message import make_and_send_sqs_message, send_to_supplier_queue
1918

20-
sqs_client = boto3_client("sqs", region_name=REGION_NAME)
19+
sqs_client = None
2120

2221
FLU_EMIS_FILE_DETAILS = MockFileDetails.emis_flu
2322
RSV_RAVS_FILE_DETAILS = MockFileDetails.ravs_rsv_1
2423

2524
NON_EXISTENT_QUEUE_ERROR_MESSAGE = (
26-
"An unexpected error occurred whilst sending to SQS: An error occurred (AWS.SimpleQueueService.NonExistent"
27-
+ "Queue) when calling the SendMessage operation: The specified queue does not exist for this wsdl version."
25+
"An unexpected error occurred whilst sending to SQS: An error occurred "
26+
"(AWS.SimpleQueueService.NonExistentQueue) when calling the SendMessage operation"
2827
)
2928

3029

31-
@mock_sqs
30+
@mock_aws
3231
@patch.dict("os.environ", MOCK_ENVIRONMENT_DICT)
3332
class TestSendSQSMessage(TestCase):
3433
"""Tests for send_sqs_message functions"""
3534

35+
def setUp(self):
36+
global sqs_client
37+
reset_common_clients()
38+
(sqs_client,) = create_boto3_clients("sqs")
39+
3640
def test_send_to_supplier_queue_success(self):
3741
"""Test send_to_supplier_queue function for a successful message send"""
3842
# Set up the sqs_queue
@@ -75,7 +79,7 @@ def test_send_to_supplier_queue_failure_due_to_queue_does_not_exist(self):
7579
vaccine_type=FLU_EMIS_FILE_DETAILS.vaccine_type,
7680
supplier=FLU_EMIS_FILE_DETAILS.supplier,
7781
)
78-
self.assertEqual(NON_EXISTENT_QUEUE_ERROR_MESSAGE, str(context.exception))
82+
self.assertIn(NON_EXISTENT_QUEUE_ERROR_MESSAGE, str(context.exception))
7983

8084
def test_make_and_send_sqs_message_success(self):
8185
"""Test make_and_send_sqs_message function for a successful message send"""

lambdas/filenameprocessor/tests/test_utils_for_filenameprocessor.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,33 @@
44
from unittest import TestCase
55
from unittest.mock import patch
66

7-
from boto3 import client as boto3_client
8-
from moto import mock_s3
7+
from moto import mock_aws
98

109
from utils_for_tests.mock_environment_variables import (
1110
MOCK_ENVIRONMENT_DICT,
1211
)
1312
from utils_for_tests.utils_for_filenameprocessor_tests import (
1413
GenericSetUp,
1514
GenericTearDown,
15+
create_boto3_clients,
1616
)
1717

1818
# Ensure environment variables are mocked before importing from src files
1919
with patch.dict("os.environ", MOCK_ENVIRONMENT_DICT):
20-
from common.clients import REGION_NAME
2120
from constants import AUDIT_TABLE_TTL_DAYS
2221
from utils_for_filenameprocessor import get_creation_and_expiry_times
2322

24-
s3_client = boto3_client("s3", region_name=REGION_NAME)
23+
s3_client = None
2524

2625

27-
@mock_s3
26+
@mock_aws
2827
class TestUtilsForFilenameprocessor(TestCase):
2928
"""Tests for utils_for_filenameprocessor functions"""
3029

3130
def setUp(self):
3231
"""Set up the s3 buckets"""
32+
global s3_client
33+
(s3_client,) = create_boto3_clients("s3")
3334
GenericSetUp(s3_client)
3435

3536
def tearDown(self):

lambdas/filenameprocessor/tests/utils_for_tests/mock_environment_variables.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class Sqs:
3535
# NOTE: FILE_NAME_GSI and CONFIG_BUCKET_NAME environment variables are set in the terraform,
3636
# but not used in the src code and so are not set here.
3737
MOCK_ENVIRONMENT_DICT = {
38+
"AWS_ACCESS_KEY_ID": "testing",
39+
"AWS_SECRET_ACCESS_KEY": "testing",
40+
"AWS_SESSION_TOKEN": "testing",
41+
"AWS_REGION": REGION_NAME,
42+
"AWS_DEFAULT_REGION": REGION_NAME,
3843
"SOURCE_BUCKET_NAME": BucketNames.SOURCE,
3944
"ACK_BUCKET_NAME": BucketNames.DESTINATION,
4045
"DPS_BUCKET_NAME": BucketNames.DPS_DESTINATION,

0 commit comments

Comments
 (0)