Skip to content

Commit 1580a4b

Browse files
committed
Rebase. Unit Tests Pass
1 parent fb7972f commit 1580a4b

File tree

5 files changed

+257
-276
lines changed

5 files changed

+257
-276
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Define 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+
logically delete MODIFY DELETE DELETE
9+
physically 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: 11 additions & 6 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, RecordError
11+
from common.mappings import ActionFlag, Operation, EventName
1112
import ExceptionMessages
1213

1314
MOCK_ENV_VARS = {
@@ -100,7 +101,7 @@ def tearDown(self):
100101
self.mock_firehose_logger.stop()
101102

102103
@staticmethod
103-
def get_event(event_name="INSERT", operation="operation", supplier="EMIS"):
104+
def get_event(event_name=EventName.CREATE, operation="operation", supplier="EMIS"):
104105
"""Returns test event data."""
105106
return ValuesForTests.get_event(event_name, operation, supplier)
106107

@@ -110,8 +111,7 @@ def assert_dynamodb_record(self, operation_flag, action_flag, items, expected_va
110111
Ignores dynamically generated fields like PK, DateTimeStamp, and ExpiresAt.
111112
Ensures that the 'Imms' field matches exactly.
112113
"""
113-
self.assertEqual(response["statusCode"], 200)
114-
self.assertEqual(response["body"], "Records processed successfully")
114+
self.assertTrue(response)
115115

116116
filtered_items = [
117117
{k: v for k, v in item.items() if k not in ["PK", "DateTimeStamp", "ExpiresAt"]}
@@ -126,6 +126,11 @@ def assert_dynamodb_record(self, operation_flag, action_flag, items, expected_va
126126
self.assertIsInstance(imms_data, dict)
127127
self.assertGreater(len(imms_data), 0)
128128

129+
for key, expected_value in expected_values.items():
130+
self.assertIn(key, filtered_items[0], f"{key} is missing")
131+
if (filtered_items[0][key] != expected_value):
132+
print (f"{key} mismatch {filtered_items[0][key]} != {expected_value}")
133+
129134
# Check Imms JSON structure matches exactly
130135
self.assertEqual(imms_data, expected_imms, "Imms data does not match expected JSON structure")
131136

@@ -167,9 +172,9 @@ def test_fhir_converter_json_error_scenario(self):
167172
def test_handler_imms_convert_to_flat_json(self):
168173
"""Test that the Imms field contains the correct flat JSON data for CREATE, UPDATE, and DELETE operations."""
169174
expected_action_flags = [
170-
{"Operation": "CREATE", "EXPECTED_ACTION_FLAG": "NEW"},
171-
{"Operation": "UPDATE", "EXPECTED_ACTION_FLAG": "UPDATE"},
172-
{"Operation": "DELETE", "EXPECTED_ACTION_FLAG": "DELETE"},
175+
{"Operation": Operation.CREATE, "EXPECTED_ACTION_FLAG": ActionFlag.CREATE},
176+
{"Operation": Operation.UPDATE, "EXPECTED_ACTION_FLAG": ActionFlag.UPDATE},
177+
{"Operation": Operation.DELETE_LOGICAL, "EXPECTED_ACTION_FLAG": ActionFlag.DELETE_LOGICAL},
173178
]
174179

175180
for test_case in expected_action_flags:

0 commit comments

Comments
 (0)