Skip to content

Commit aae949a

Browse files
committed
improve coverage
1 parent a95ae31 commit aae949a

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

lambdas/id_sync/src/ieds_db_operations.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from common.aws_dynamodb import get_dynamodb_table
44
from common.clients import logger, dynamodb_client
55
import json
6-
import ast
7-
86
from utils import make_status
97
from exceptions.id_sync_exception import IdSyncException
108

@@ -136,37 +134,27 @@ def paginate_items_for_patient_pk(patient_pk: str) -> list:
136134

137135
def extract_patient_resource_from_item(item: dict) -> dict | None:
138136
"""
139-
Extract a Patient resource dict from an IEDS database.
137+
Extract a Patient resource from an IEDS database.
140138
"""
141139
patient_resource = item.get("Resource", None)
142140
logger.info(f"patient_resource (raw): {patient_resource}")
143141

144-
# Accept either a dict (preferred) or a JSON / Python-literal string
145142
if isinstance(patient_resource, str):
146-
# Try JSON first, then fall back to ast.literal_eval for single-quotes
147143
try:
148144
patient_resource_parsed = json.loads(patient_resource)
149-
except Exception:
150-
try:
151-
patient_resource_parsed = ast.literal_eval(patient_resource)
152-
except Exception:
153-
logger.debug("extract_patient_resource_from_item: Resource is a string but could not be parsed")
154-
return None
145+
except json.JSONDecodeError:
146+
logger.warning("Failed to decode patient_resource JSON string")
147+
return None
155148
patient_resource = patient_resource_parsed
156149

157150
if not isinstance(patient_resource, dict):
158151
return None
159152

160-
# The Patient resource may be nested under 'contained' or be the resource itself
161153
contained = patient_resource.get("contained") or []
162154
for response in contained:
163155
if isinstance(response, dict) and response.get("resourceType") == "Patient":
164156
return response
165157

166-
# Fallback: if the resource is itself a Patient, return it
167-
if patient_resource.get("resourceType") == "Patient":
168-
return patient_resource
169-
170158
return None
171159

172160

lambdas/id_sync/tests/test_ieds_db_operations.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
11
import unittest
2+
3+
from ieds_db_operations import extract_patient_resource_from_item
24
from unittest.mock import patch, MagicMock
35
from exceptions.id_sync_exception import IdSyncException
4-
56
import ieds_db_operations
67

78

9+
class TestExtractPatientResourceFromItem(unittest.TestCase):
10+
11+
def test_extract_from_dict_with_contained_patient(self):
12+
item = {
13+
"Resource": {
14+
"resourceType": "Immunization",
15+
"contained": [
16+
{"resourceType": "Patient", "id": "P1", "name": [{"family": "Doe"}]}
17+
],
18+
}
19+
}
20+
21+
patient = extract_patient_resource_from_item(item)
22+
self.assertIsNotNone(patient)
23+
self.assertIsInstance(patient, dict)
24+
self.assertEqual(patient.get("resourceType"), "Patient")
25+
self.assertEqual(patient.get("id"), "P1")
26+
27+
def test_extract_from_json_string(self):
28+
resource_json = '{"resourceType": "Immunization", "contained": [{"resourceType": "Patient", "id": "P2"}]}'
29+
item = {"Resource": resource_json}
30+
31+
patient = extract_patient_resource_from_item(item)
32+
self.assertIsNotNone(patient)
33+
self.assertEqual(patient.get("id"), "P2")
34+
35+
def test_malformed_json_string_returns_none(self):
36+
# A malformed JSON string should not raise, but return None
37+
item = {"Resource": "{not: valid json}"}
38+
self.assertIsNone(extract_patient_resource_from_item(item))
39+
40+
def test_non_dict_resource_returns_none(self):
41+
item = {"Resource": 12345}
42+
self.assertIsNone(extract_patient_resource_from_item(item))
43+
44+
def test_missing_resource_returns_none(self):
45+
item = {}
46+
self.assertIsNone(extract_patient_resource_from_item(item))
47+
48+
849
class TestIedsDbOperations(unittest.TestCase):
950
"""Base test class for IEDS database operations"""
1051

0 commit comments

Comments
 (0)