Skip to content

Commit f605643

Browse files
authored
Rebase. Unit Tests Pass (#413)
1 parent 26800e8 commit f605643

File tree

5 files changed

+282
-279
lines changed

5 files changed

+282
-279
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Enums for event names, operations, and action flags.
3+
4+
# case eventName operation actionFlag
5+
----------------- --------- --------- ----------
6+
create INSERT CREATE NEW
7+
update MODIFY UPDATE UPDATE
8+
logical delete MODIFY DELETE DELETE
9+
physical delete REMOVE REMOVE N/A
10+
"""
11+
12+
class EventName():
13+
CREATE = "INSERT"
14+
UPDATE = "MODIFY"
15+
DELETE_LOGICAL = "MODIFY"
16+
DELETE_PHYSICAL = "REMOVE"
17+
18+
class Operation():
19+
CREATE = "CREATE"
20+
UPDATE = "UPDATE"
21+
DELETE_LOGICAL = "DELETE"
22+
DELETE_PHYSICAL = "REMOVE"
23+
24+
class ActionFlag():
25+
CREATE = "NEW"
26+
UPDATE = "UPDATE"
27+
DELETE_LOGICAL = "DELETE"

delta_backend/src/delta.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from botocore.exceptions import ClientError
99
from log_firehose import FirehoseLogger
1010
from Converter import Converter
11+
from common.mappings import ActionFlag, Operation, EventName
1112

1213
failure_queue_url = os.environ["AWS_SQS_QUEUE_URL"]
1314
delta_table_name = os.environ["DELTA_TABLE_NAME"]
@@ -37,6 +38,7 @@ def get_vaccine_type(patientsk) -> str:
3738

3839

3940
def handler(event, context):
41+
ret = True
4042
logger.info("Starting Delta Handler")
4143
log_data = dict()
4244
firehose_log = dict()
@@ -61,14 +63,14 @@ def handler(event, context):
6163
response = str()
6264
imms_id = str()
6365
operation = str()
64-
if record["eventName"] != "REMOVE":
66+
if record["eventName"] != EventName.DELETE_PHYSICAL:
6567
new_image = record["dynamodb"]["NewImage"]
6668
imms_id = new_image["PK"]["S"].split("#")[1]
6769
vaccine_type = get_vaccine_type(new_image["PatientSK"]["S"])
6870
supplier_system = new_image["SupplierSystem"]["S"]
6971
if supplier_system not in ("DPSFULL", "DPSREDUCED"):
7072
operation = new_image["Operation"]["S"]
71-
action_flag = "NEW" if operation == "CREATE" else operation
73+
action_flag = ActionFlag.CREATE if operation == Operation.CREATE else operation
7274
resource_json = json.loads(new_image["Resource"]["S"])
7375
FHIRConverter = Converter(json.dumps(resource_json))
7476
flat_json = FHIRConverter.runConversion(resource_json) # Get the flat JSON
@@ -94,17 +96,17 @@ def handler(event, context):
9496
firehose_log["event"] = log_data
9597
firehose_logger.send_log(firehose_log)
9698
logger.info(f"Record from DPS skipped for {imms_id}")
97-
return {"statusCode": 200, "body": f"Record from DPS skipped for {imms_id}"}
99+
continue
98100
else:
99-
operation = "REMOVE"
101+
operation = Operation.DELETE_PHYSICAL
100102
new_image = record["dynamodb"]["Keys"]
101103
logger.info(f"Record to delta:{new_image}")
102104
imms_id = new_image["PK"]["S"].split("#")[1]
103105
response = delta_table.put_item(
104106
Item={
105107
"PK": str(uuid.uuid4()),
106108
"ImmsID": imms_id,
107-
"Operation": "REMOVE",
109+
"Operation": Operation.DELETE_PHYSICAL,
108110
"VaccineType": "default",
109111
"SupplierSystem": "default",
110112
"DateTimeStamp": approximate_creation_time.isoformat(),
@@ -131,7 +133,6 @@ def handler(event, context):
131133
firehose_log["event"] = log_data
132134
firehose_logger.send_log(firehose_log)
133135
logger.info(log)
134-
return {"statusCode": 200, "body": "Records processed successfully"}
135136
else:
136137
log = f"Record NOT created for {imms_id}"
137138
operation_outcome["statusCode"] = "500"
@@ -140,7 +141,7 @@ def handler(event, context):
140141
firehose_log["event"] = log_data
141142
firehose_logger.send_log(firehose_log)
142143
logger.info(log)
143-
return {"statusCode": 500, "body": "Records not processed successfully"}
144+
ret = False
144145

145146
except Exception as e:
146147
operation_outcome["statusCode"] = "500"
@@ -155,7 +156,5 @@ def handler(event, context):
155156
log_data["operation_outcome"] = operation_outcome
156157
firehose_log["event"] = log_data
157158
firehose_logger.send_log(firehose_log)
158-
return {
159-
"statusCode": 500,
160-
"body": "Records not processed",
161-
}
159+
ret = False
160+
return ret

delta_backend/tests/test_convert_to_flat_json.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from SchemaParser import SchemaParser
99
from Converter import Converter
1010
from ConversionChecker import ConversionChecker
11+
from common.mappings import ActionFlag, Operation, EventName
1112
import ExceptionMessages
1213

1314
MOCK_ENV_VARS = {
@@ -73,7 +74,7 @@ def tearDown(self):
7374
self.mock_firehose_logger.stop()
7475

7576
@staticmethod
76-
def get_event(event_name="INSERT", operation="operation", supplier="EMIS"):
77+
def get_event(event_name=EventName.CREATE, operation="operation", supplier="EMIS"):
7778
"""Returns test event data."""
7879
return ValuesForTests.get_event(event_name, operation, supplier)
7980

@@ -83,8 +84,7 @@ def assert_dynamodb_record(self, operation_flag, action_flag, items, expected_va
8384
Ignores dynamically generated fields like PK, DateTimeStamp, and ExpiresAt.
8485
Ensures that the 'Imms' field matches exactly.
8586
"""
86-
self.assertEqual(response["statusCode"], 200)
87-
self.assertEqual(response["body"], "Records processed successfully")
87+
self.assertTrue(response)
8888

8989
filtered_items = [
9090
{k: v for k, v in item.items() if k not in ["PK", "DateTimeStamp", "ExpiresAt"]}
@@ -140,9 +140,9 @@ def test_fhir_converter_json_error_scenario(self):
140140
def test_handler_imms_convert_to_flat_json(self):
141141
"""Test that the Imms field contains the correct flat JSON data for CREATE, UPDATE, and DELETE operations."""
142142
expected_action_flags = [
143-
{"Operation": "CREATE", "EXPECTED_ACTION_FLAG": "NEW"},
144-
{"Operation": "UPDATE", "EXPECTED_ACTION_FLAG": "UPDATE"},
145-
{"Operation": "DELETE", "EXPECTED_ACTION_FLAG": "DELETE"},
143+
{"Operation": Operation.CREATE, "EXPECTED_ACTION_FLAG": ActionFlag.CREATE},
144+
{"Operation": Operation.UPDATE, "EXPECTED_ACTION_FLAG": ActionFlag.UPDATE},
145+
{"Operation": Operation.DELETE_LOGICAL, "EXPECTED_ACTION_FLAG": ActionFlag.DELETE_LOGICAL},
146146
]
147147

148148
for test_case in expected_action_flags:
@@ -546,4 +546,4 @@ def clear_table(self):
546546
items = result.get("Items", [])
547547

548548
if __name__ == "__main__":
549-
unittest.main()
549+
unittest.main()

0 commit comments

Comments
 (0)