Skip to content

Commit d879217

Browse files
committed
AMB-1744 add int test
1 parent 22f3a2f commit d879217

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

lambda_code/src/fhir_service.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ def __init__(self, imms_repo: ImmunisationRepository):
1212

1313
def get_immunisation_by_id(self, imms_id: str) -> Optional[Immunization]:
1414
imms = self.immunisation_repo.get_immunisation_by_id(imms_id)
15-
return Immunization.parse_obj(imms)
15+
if imms:
16+
# TODO: This shouldn't raise an exception since, we validate the message before storing it,
17+
# but what if the stored message is different from the requested FHIR version?
18+
return Immunization.parse_obj(imms)
19+
else:
20+
return None

lambda_code/tests/test_fhir_service.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ def test_get_immunisation_by_id(self):
2929
self.imms_repo.get_immunisation_by_id.assert_called_once_with(imms_id)
3030
self.assertEqual(act_imms.id, imms_id)
3131

32+
def test_immunisation_not_found(self):
33+
"""it should return None if immunisation doesn't exist"""
34+
imms_id = "none-existent-id"
35+
36+
self.imms_repo.get_immunisation_by_id.return_value = None
37+
38+
# When
39+
act_imms = self.fhir_service.get_immunisation_by_id(imms_id)
40+
41+
# Then
42+
self.imms_repo.get_immunisation_by_id.assert_called_once_with(imms_id)
43+
self.assertEqual(act_imms, None)
44+
3245
@staticmethod
3346
def _create_an_immunisation_obj(imms_id) -> Immunization:
3447
base_imms = {

tests/test_immunisation_api.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import uuid
2+
13
import pytest
24
import requests
35

@@ -7,13 +9,23 @@ class ImmunisationApi:
79
def __init__(self, url, token):
810
self.url = url
911
self.token = token
12+
self.headers = {
13+
"Authorization": self.token,
14+
"Content-Type": "application/fhir+json",
15+
"Accept": "application/fhir+json",
16+
}
1017

1118
def get_event_by_id(self, event_id):
12-
headers = {
13-
"Authorization": self.token
14-
}
15-
response = requests.get(f"{self.url}/event/{event_id}", headers=headers)
16-
return response
19+
return requests.get(f"{self.url}/event/{event_id}", headers=self._update_headers())
20+
21+
def _update_headers(self, headers=None):
22+
if headers is None:
23+
headers = {}
24+
updated = {**self.headers, **{
25+
"X-Correlation-ID": str(uuid.uuid4()),
26+
"X-Request-ID": str(uuid.uuid4()),
27+
}}
28+
return {**updated, **headers}
1729

1830

1931
@pytest.mark.nhsd_apim_authorization(
@@ -35,7 +47,6 @@ def test_get_event_by_id_not_found_nhs_login(nhsd_apim_proxy_url, nhsd_apim_auth
3547
# Assert
3648
assert result.status_code == 404
3749
assert res_body["resourceType"] == "OperationOutcome"
38-
assert res_body["issue"][0]["code"] == "not-found"
3950

4051

4152
@pytest.mark.nhsd_apim_authorization(
@@ -57,7 +68,6 @@ def test_get_event_by_id_invalid_nhs_login(nhsd_apim_proxy_url, nhsd_apim_auth_h
5768
# Assert
5869
assert result.status_code == 400
5970
assert res_body["resourceType"] == "OperationOutcome"
60-
assert res_body["issue"][0]["code"] == "invalid"
6171

6272

6373
@pytest.mark.nhsd_apim_authorization(
@@ -73,11 +83,10 @@ def test_get_event_by_id_happy_path_nhs_login(nhsd_apim_proxy_url, nhsd_apim_aut
7383
imms_api = ImmunisationApi(nhsd_apim_proxy_url, token)
7484

7585
# Act
76-
id = "e045626e-4dc5-4df3-bc35-da25263f901e"
77-
result = imms_api.get_event_by_id(id)
86+
imms_id = "e045626e-4dc5-4df3-bc35-da25263f901e"
87+
result = imms_api.get_event_by_id(imms_id)
7888
json_result = result.json()
7989

8090
# Assert
81-
assert result.headers["Content-Type"] == "application/fhir+json"
8291
assert result.status_code == 200
83-
assert json_result["identifier"][0]["value"] == id
92+
assert json_result["identifier"][0]["value"] == imms_id

0 commit comments

Comments
 (0)