Skip to content

Commit 83fce8e

Browse files
committed
VED-740: review and add more bundle and entry level test
1 parent 465c365 commit 83fce8e

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

backend/src/fhir_service.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from uuid import uuid4
33
import datetime
44
import os
5-
import json
65
from enum import Enum
76
from typing import Optional, Union
87

backend/src/models/utils/generic_utils.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,29 +140,29 @@ def create_diagnostics_error(value):
140140
exp_error = {"diagnostics": diagnostics}
141141
return exp_error
142142

143-
def empty_bundle(self_url: str) -> Dict[str, Any]:
143+
def make_empty_bundle(self_url: str) -> Dict[str, Any]:
144144
return {
145145
"resourceType": "Bundle",
146146
"type": "searchset",
147147
"link": [{"relation": "self", "url": self_url}],
148-
"total": 0,
149148
"entry": [],
149+
"total": 0,
150150
}
151151

152152
def form_json(response, _elements, identifier, baseurl):
153153
self_url = f"{baseurl}?identifier={identifier}" + (f"&_elements={_elements}" if _elements else "")
154154

155155
if not response:
156-
return empty_bundle(self_url)
156+
return make_empty_bundle(self_url)
157157

158158
meta = {"versionId": response["version"]} if "version" in response else {}
159159

160160
# Full Immunization payload to be returned if only the identifier parameter was provided and truncated when _elements is used
161161
if _elements:
162-
element = {e.strip().lower() for e in _elements.split(",") if e.strip()}
162+
elements = {e.strip().lower() for e in _elements.split(",") if e.strip()}
163163
resource = {"resourceType": "Immunization"}
164-
if "id" in element: resource["id"] = response["id"]
165-
if "meta" in element and meta: resource["meta"] = meta
164+
if "id" in elements: resource["id"] = response["id"]
165+
if "meta" in elements and meta: resource["meta"] = meta
166166

167167
else:
168168
resource = response["resource"]
@@ -179,7 +179,7 @@ def form_json(response, _elements, identifier, baseurl):
179179
entry=[entry],
180180
total=1)
181181

182-
182+
# Reassigned total to ensure it appears last in the response to match expected output
183183
data = json.loads(fhir_bundle.json(by_alias=True))
184184
data["total"] = data.pop("total")
185185
return data

backend/tests/test_fhir_service.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,14 @@ def test_get_immunization_by_identifier(self):
337337
self.imms_repo.get_immunization_by_identifier.assert_called_once_with(imms_id, "COVID19.S")
338338

339339
self.assertEqual(act_imms["resourceType"], "Bundle")
340+
self.assertEqual(service_resp.get("type"), "searchset")
341+
self.assertIn("entry", service_resp)
342+
self.assertEqual(len(service_resp["entry"]), 1)
343+
self.assertIn("total", service_resp)
344+
self.assertEqual(service_resp["total"], 1)
345+
res = service_resp["entry"][0]["resource"]
346+
self.assertEqual(res["resourceType"], "Immunization")
347+
self.assertEqual(res["id"], imms_id)
340348

341349
def test_immunization_not_found(self):
342350
"""it should return None if Immunization doesn't exist"""

0 commit comments

Comments
 (0)