Skip to content

Commit 93a647a

Browse files
committed
unit tests, mock dynamodb, refactoring II
1 parent 82cd341 commit 93a647a

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

ack_backend/tests/utils/generic_setup_and_teardown_for_ack_backend.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
"""Generic setup and teardown for ACK backend tests"""
22

3-
from tests.utils.mock_environment_variables import BucketNames, Firehose, REGION_NAME
3+
from unittest.mock import patch
4+
5+
from tests.utils.mock_environment_variables import BucketNames, MOCK_ENVIRONMENT_DICT, Firehose
6+
7+
# Ensure environment variables are mocked before importing from src files
8+
with patch.dict("os.environ", MOCK_ENVIRONMENT_DICT):
9+
from clients import REGION_NAME
10+
from constants import AuditTableKeys, AUDIT_TABLE_NAME, AUDIT_TABLE_QUEUE_NAME_GSI, AUDIT_TABLE_FILENAME_GSI
411

512

613
class GenericSetUp:
@@ -11,7 +18,7 @@ class GenericSetUp:
1118
* If firehose_client is provided, creates a firehose delivery stream
1219
"""
1320

14-
def __init__(self, s3_client=None, firehose_client=None):
21+
def __init__(self, s3_client=None, firehose_client=None, dynamodb_client=None):
1522

1623
if s3_client:
1724
for bucket_name in [BucketNames.SOURCE, BucketNames.DESTINATION, BucketNames.MOCK_FIREHOSE]:
@@ -30,11 +37,41 @@ def __init__(self, s3_client=None, firehose_client=None):
3037
},
3138
)
3239

40+
if dynamodb_client:
41+
dynamodb_client.create_table(
42+
TableName=AUDIT_TABLE_NAME,
43+
KeySchema=[{"AttributeName": AuditTableKeys.MESSAGE_ID, "KeyType": "HASH"}],
44+
AttributeDefinitions=[
45+
{"AttributeName": AuditTableKeys.MESSAGE_ID, "AttributeType": "S"},
46+
{"AttributeName": AuditTableKeys.FILENAME, "AttributeType": "S"},
47+
{"AttributeName": AuditTableKeys.QUEUE_NAME, "AttributeType": "S"},
48+
{"AttributeName": AuditTableKeys.STATUS, "AttributeType": "S"},
49+
],
50+
ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},
51+
GlobalSecondaryIndexes=[
52+
{
53+
"IndexName": AUDIT_TABLE_FILENAME_GSI,
54+
"KeySchema": [{"AttributeName": AuditTableKeys.FILENAME, "KeyType": "HASH"}],
55+
"Projection": {"ProjectionType": "KEYS_ONLY"},
56+
"ProvisionedThroughput": {"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},
57+
},
58+
{
59+
"IndexName": AUDIT_TABLE_QUEUE_NAME_GSI,
60+
"KeySchema": [
61+
{"AttributeName": AuditTableKeys.QUEUE_NAME, "KeyType": "HASH"},
62+
{"AttributeName": AuditTableKeys.STATUS, "KeyType": "RANGE"},
63+
],
64+
"Projection": {"ProjectionType": "ALL"},
65+
"ProvisionedThroughput": {"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},
66+
},
67+
],
68+
)
69+
3370

3471
class GenericTearDown:
3572
"""Performs generic tear down of mock resources"""
3673

37-
def __init__(self, s3_client=None, firehose_client=None):
74+
def __init__(self, s3_client=None, firehose_client=None, dynamodb_client=None):
3875

3976
if s3_client:
4077
for bucket_name in [BucketNames.SOURCE, BucketNames.DESTINATION, BucketNames.MOCK_FIREHOSE]:
@@ -44,3 +81,6 @@ def __init__(self, s3_client=None, firehose_client=None):
4481

4582
if firehose_client:
4683
firehose_client.delete_delivery_stream(DeliveryStreamName=Firehose.STREAM_NAME)
84+
85+
if dynamodb_client:
86+
dynamodb_client.delete_table(TableName=AUDIT_TABLE_NAME)

ack_backend/tests/utils/values_for_ack_backend_tests.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,20 @@ class ValidValues:
219219
"|MESSAGE_DELIVERY\n"
220220
)
221221

222+
upload_ack_file_expected_log = {
223+
"function_name": "ack_processor_upload_ack_file",
224+
"date_time": fixed_datetime.strftime("%Y-%m-%d %H:%M:%S"),
225+
"status": "success",
226+
"supplier": MOCK_MESSAGE_DETAILS.supplier,
227+
"file_key": MOCK_MESSAGE_DETAILS.file_key,
228+
"vaccine_type": MOCK_MESSAGE_DETAILS.vaccine_type,
229+
"message_id": MOCK_MESSAGE_DETAILS.row_id,
230+
"row_count": 100,
231+
"statusCode": 200,
232+
"message": "Record processing complete",
233+
}
234+
235+
222236

223237
class InvalidValues:
224238
"""Invalid values for use in tests"""

0 commit comments

Comments
 (0)