Skip to content

Commit 55b5343

Browse files
committed
Resolve PR comments
1 parent f3d372b commit 55b5343

8 files changed

Lines changed: 45 additions & 50 deletions

File tree

backend/src/controller/aws_apig_event_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
def get_path_parameter(event: APIGatewayProxyEventV1, param_name: str) -> str:
1212
return dict_utils.get_field(
13-
dict(event),
14-
"pathParameters",
13+
event["pathParameters"],
1514
param_name,
1615
default=""
1716
)

backend/src/controller/fhir_controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def get_immunization_by_id(self, aws_event: APIGatewayProxyEventV1) -> dict:
9494

9595
supplier_system = get_supplier_system_header(aws_event)
9696

97-
resource, version = self.fhir_service.get_immunization_by_id(imms_id, supplier_system)
97+
resource, version = self.fhir_service.get_immunization_and_version_by_id(imms_id, supplier_system)
9898

9999
return create_response(200, resource.json(), {E_TAG_HEADER_NAME: version})
100100

backend/src/repository/fhir_repository.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def get_immunization_by_identifier(self, identifier_pk: str) -> tuple[Optional[d
102102
else:
103103
return None, None
104104

105-
def get_immunization_by_id(self, imms_id: str) -> tuple[Optional[dict], Optional[str]]:
105+
def get_immunization_and_version_by_id(self, imms_id: str) -> tuple[Optional[dict], Optional[str]]:
106106
response = self.table.get_item(Key={"PK": _make_immunization_pk(imms_id)})
107107
item = response.get("Item")
108108

backend/src/service/fhir_service.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ def get_immunization_by_identifier(
8383
imms_resp['resource'] = filtered_resource
8484
return form_json(imms_resp, element, identifier, base_url)
8585

86-
def get_immunization_by_id(self, imms_id: str, supplier_system: str) -> tuple[Immunization, str]:
86+
def get_immunization_and_version_by_id(self, imms_id: str, supplier_system: str) -> tuple[Immunization, str]:
8787
"""
8888
Get an Immunization by its ID. Returns the immunization entity and version number.
8989
"""
90-
resource, version = self.immunization_repo.get_immunization_by_id(imms_id)
90+
resource, version = self.immunization_repo.get_immunization_and_version_by_id(imms_id)
9191

9292
if resource is None:
9393
raise ResourceNotFoundError(resource_type="Immunization", resource_id=imms_id)
@@ -230,7 +230,7 @@ def delete_immunization(self, imms_id: str, supplier_system: str) -> Immunizatio
230230
Exception will be raised if resource does not exist. Multiple calls to this method won't change
231231
the record in the database.
232232
"""
233-
existing_immunisation, _ = self.immunization_repo.get_immunization_by_id(imms_id)
233+
existing_immunisation, _ = self.immunization_repo.get_immunization_and_version_by_id(imms_id)
234234

235235
if not existing_immunisation:
236236
raise ResourceNotFoundError(resource_type="Immunization", resource_id=imms_id)

backend/tests/controller/test_fhir_api_exception_handler.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,19 @@ def dummy_func():
2525

2626
def test_exception_handler_handles_custom_exception_and_returns_fhir_response(self):
2727
"""Test that custom exceptions are handled by the wrapper and a valid response is returned to the client"""
28-
test_cases = {
29-
("Test UnauthorisedError", UnauthorizedError, 403, "forbidden", "Unauthorized request"),
30-
("Test UnauthorizedVaxError", UnauthorizedVaxError, 403, "forbidden",
31-
"Unauthorized request for vaccine type"),
32-
("Test ResourceNotFoundError", ResourceNotFoundError, 404, "not-found", "Immunization resource does not "
33-
"exist. ID: 123")
34-
}
28+
test_cases = [
29+
(UnauthorizedError(), 403, "forbidden", "Unauthorized request"),
30+
(UnauthorizedVaxError(), 403, "forbidden", "Unauthorized request for vaccine type"),
31+
(ResourceNotFoundError(resource_type="Immunization", resource_id="123"), 404, "not-found",
32+
"Immunization resource does not exist. ID: 123")
33+
]
3534

36-
for msg, error_type, expected_status, expected_code, expected_message in test_cases:
37-
with self.subTest(msg=msg):
35+
for error, expected_status, expected_code, expected_message in test_cases:
36+
with self.subTest(msg=f"Test {error.__class__.__name__}"):
3837

3938
@fhir_api_exception_handler
4039
def dummy_func():
41-
if msg == "Test ResourceNotFoundError":
42-
raise error_type(resource_type="Immunization", resource_id="123")
43-
44-
raise error_type()
40+
raise error
4541

4642
response = dummy_func()
4743

backend/tests/controller/test_fhir_controller.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ def test_get_imms_by_id(self):
673673
"""it should return Immunization resource if it exists"""
674674
# Given
675675
imms_id = "a-id"
676-
self.service.get_immunization_by_id.return_value = (Immunization.construct(), "1")
676+
self.service.get_immunization_and_version_by_id.return_value = (Immunization.construct(), "1")
677677
lambda_event = {
678678
"headers": {"SupplierSystem": "test"},
679679
"pathParameters": {"id": imms_id},
@@ -682,7 +682,7 @@ def test_get_imms_by_id(self):
682682
# When
683683
response = self.controller.get_immunization_by_id(lambda_event)
684684
# Then
685-
self.service.get_immunization_by_id.assert_called_once_with(imms_id, "test")
685+
self.service.get_immunization_and_version_by_id.assert_called_once_with(imms_id, "test")
686686

687687
self.assertEqual(response["statusCode"], 200)
688688
body = json.loads(response["body"])
@@ -701,7 +701,7 @@ def test_get_imms_by_id_returns_unauthorized_when_supplier_header_missing(self):
701701
# When
702702
response = self.controller.get_immunization_by_id(lambda_event)
703703
# Then
704-
self.service.get_immunization_by_id.assert_not_called()
704+
self.service.get_immunization_and_version_by_id.assert_not_called()
705705

706706
self.assertEqual(response["statusCode"], 403)
707707
body = json.loads(response["body"])
@@ -712,7 +712,7 @@ def test_get_imms_by_id_unauthorised_vax_error(self):
712712
"""it should return a 403 error is the service layer throws an UnauthorizedVaxError"""
713713
# Given
714714
imms_id = "a-id"
715-
self.service.get_immunization_by_id.side_effect = UnauthorizedVaxError
715+
self.service.get_immunization_and_version_by_id.side_effect = UnauthorizedVaxError
716716
lambda_event = {
717717
"headers": {"SupplierSystem": "test"},
718718
"pathParameters": {"id": imms_id},
@@ -730,7 +730,7 @@ def test_not_found(self):
730730
"""it should return not-found OperationOutcome if it doesn't exist"""
731731
# Given
732732
imms_id = "a-non-existing-id"
733-
self.service.get_immunization_by_id.side_effect = ResourceNotFoundError(
733+
self.service.get_immunization_and_version_by_id.side_effect = ResourceNotFoundError(
734734
resource_type="Immunization",
735735
resource_id=imms_id
736736
)
@@ -743,7 +743,7 @@ def test_not_found(self):
743743
response = self.controller.get_immunization_by_id(lambda_event)
744744

745745
# Then
746-
self.service.get_immunization_by_id.assert_called_once_with(imms_id, "test")
746+
self.service.get_immunization_and_version_by_id.assert_called_once_with(imms_id, "test")
747747

748748
self.assertEqual(response["statusCode"], 404)
749749
body = json.loads(response["body"])
@@ -756,7 +756,7 @@ def test_validate_imms_id(self):
756756

757757
response = self.controller.get_immunization_by_id(invalid_id)
758758

759-
self.assertEqual(self.service.get_immunization_by_id.call_count, 0)
759+
self.assertEqual(self.service.get_immunization_and_version_by_id.call_count, 0)
760760
self.assertEqual(response["statusCode"], 400)
761761
outcome = json.loads(response["body"])
762762
self.assertEqual(outcome["resourceType"], "OperationOutcome")
@@ -827,7 +827,7 @@ def test_malformed_resource(self):
827827
}
828828

829829
response = self.controller.create_immunization(aws_event)
830-
self.assertEqual(self.service.get_immunization_by_id.call_count, 0)
830+
self.assertEqual(self.service.get_immunization_and_version_by_id.call_count, 0)
831831
self.assertEqual(response["statusCode"], 400)
832832
outcome = json.loads(response["body"])
833833
self.assertEqual(outcome["resourceType"], "OperationOutcome")
@@ -1333,7 +1333,7 @@ def test_validate_imms_id(self):
13331333

13341334
response = self.controller.delete_immunization(invalid_id)
13351335

1336-
self.assertEqual(self.service.get_immunization_by_id.call_count, 0)
1336+
self.assertEqual(self.service.get_immunization_and_version_by_id.call_count, 0)
13371337
self.assertEqual(response["statusCode"], 400)
13381338
outcome = json.loads(response["body"])
13391339
self.assertEqual(outcome["resourceType"], "OperationOutcome")

backend/tests/repository/test_fhir_repository.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def test_get_immunization_by_id(self):
111111
}
112112
}
113113
)
114-
immunisation, version = self.repository.get_immunization_by_id(imms_id)
114+
immunisation, version = self.repository.get_immunization_and_version_by_id(imms_id)
115115

116116
# Validate the results
117117
self.assertDictEqual(expected_resource, immunisation)
@@ -123,7 +123,7 @@ def test_immunization_not_found(self):
123123
imms_id = "non-existent-id"
124124
self.table.get_item = MagicMock(return_value={})
125125

126-
imms, version = self.repository.get_immunization_by_id(imms_id)
126+
imms, version = self.repository.get_immunization_and_version_by_id(imms_id)
127127
self.assertIsNone(imms)
128128
self.assertIsNone(version)
129129

@@ -466,7 +466,7 @@ def test_get_deleted_immunization(self):
466466
imms_id = "a-deleted-id"
467467
self.table.get_item = MagicMock(return_value={"Item": {"Resource": "{}", "DeletedAt": time.time()}})
468468

469-
imms, version = self.repository.get_immunization_by_id(imms_id)
469+
imms, version = self.repository.get_immunization_and_version_by_id(imms_id)
470470
self.assertIsNone(imms)
471471
self.assertIsNone(version)
472472

backend/tests/service/test_fhir_service.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -205,29 +205,29 @@ def test_get_immunization_by_id(self):
205205
imms_id = "an-id"
206206
self.mock_redis_client.hget.return_value = "COVID-19"
207207
self.authoriser.authorise.return_value = True
208-
self.imms_repo.get_immunization_by_id.return_value = (create_covid_19_immunization(imms_id).dict(), "")
208+
self.imms_repo.get_immunization_and_version_by_id.return_value = (create_covid_19_immunization(imms_id).dict(), "")
209209

210210
# When
211-
immunisation, version = self.fhir_service.get_immunization_by_id(imms_id, "Test Supplier")
211+
immunisation, version = self.fhir_service.get_immunization_and_version_by_id(imms_id, "Test Supplier")
212212

213213
# Then
214214
self.authoriser.authorise.assert_called_once_with("Test Supplier", ApiOperationCode.READ, {"COVID-19"})
215-
self.imms_repo.get_immunization_by_id.assert_called_once_with(imms_id)
215+
self.imms_repo.get_immunization_and_version_by_id.assert_called_once_with(imms_id)
216216

217217
self.assertEqual(immunisation.id, imms_id)
218218
self.assertEqual(version, "")
219219

220220
def test_immunization_not_found(self):
221221
"""it should return None if Immunization doesn't exist"""
222222
imms_id = "non-existent-id"
223-
self.imms_repo.get_immunization_by_id.return_value = None, None
223+
self.imms_repo.get_immunization_and_version_by_id.return_value = None, None
224224

225225
# When
226226
with self.assertRaises(ResourceNotFoundError) as error:
227-
self.fhir_service.get_immunization_by_id(imms_id, "Test Supplier")
227+
self.fhir_service.get_immunization_and_version_by_id(imms_id, "Test Supplier")
228228

229229
# Then
230-
self.imms_repo.get_immunization_by_id.assert_called_once_with(imms_id)
230+
self.imms_repo.get_immunization_and_version_by_id.assert_called_once_with(imms_id)
231231
self.assertEqual("Immunization resource does not exist. ID: non-existent-id", str(error.exception))
232232

233233
def test_get_immunization_by_id_patient_not_restricted(self):
@@ -240,17 +240,17 @@ def test_get_immunization_by_id_patient_not_restricted(self):
240240
immunization_data = load_json_data("completed_covid19_immunization_event.json")
241241
self.mock_redis_client.hget.return_value = "COVID-19"
242242
self.authoriser.authorise.return_value = True
243-
self.imms_repo.get_immunization_by_id.return_value = (immunization_data, "2")
243+
self.imms_repo.get_immunization_and_version_by_id.return_value = (immunization_data, "2")
244244

245245
expected_imms = load_json_data("completed_covid19_immunization_event_for_read.json")
246246
expected_output = Immunization.parse_obj(expected_imms)
247247

248248
# When
249-
actual_output, version = self.fhir_service.get_immunization_by_id(imms_id, "Test Supplier")
249+
actual_output, version = self.fhir_service.get_immunization_and_version_by_id(imms_id, "Test Supplier")
250250

251251
# Then
252252
self.authoriser.authorise.assert_called_once_with("Test Supplier", ApiOperationCode.READ, {"COVID-19"})
253-
self.imms_repo.get_immunization_by_id.assert_called_once_with(imms_id)
253+
self.imms_repo.get_immunization_and_version_by_id.assert_called_once_with(imms_id)
254254
self.assertEqual(actual_output, expected_output)
255255
self.assertEqual(version, "2")
256256

@@ -284,15 +284,15 @@ def test_unauthorised_error_raised_when_user_lacks_permissions(self):
284284
imms_id = "an-id"
285285
self.mock_redis_client.hget.return_value = "COVID-19"
286286
self.authoriser.authorise.return_value = False
287-
self.imms_repo.get_immunization_by_id.return_value = (create_covid_19_immunization(imms_id).dict(), 1)
287+
self.imms_repo.get_immunization_and_version_by_id.return_value = (create_covid_19_immunization(imms_id).dict(), 1)
288288

289289
with self.assertRaises(UnauthorizedVaxError):
290290
# When
291-
self.fhir_service.get_immunization_by_id(imms_id, "Test Supplier")
291+
self.fhir_service.get_immunization_and_version_by_id(imms_id, "Test Supplier")
292292

293293
# Then
294294
self.authoriser.authorise.assert_called_once_with("Test Supplier", ApiOperationCode.READ, {"COVID-19"})
295-
self.imms_repo.get_immunization_by_id.assert_called_once_with(imms_id)
295+
self.imms_repo.get_immunization_and_version_by_id.assert_called_once_with(imms_id)
296296

297297

298298
def test_post_validation_failed_get_invalid_target_disease(self):
@@ -729,43 +729,43 @@ def test_delete_immunization(self):
729729
imms = json.loads(create_covid_19_immunization(self.TEST_IMMUNISATION_ID).json())
730730
self.mock_redis_client.hget.return_value = "COVID19"
731731
self.authoriser.authorise.return_value = True
732-
self.imms_repo.get_immunization_by_id.return_value = (imms, "1")
732+
self.imms_repo.get_immunization_and_version_by_id.return_value = (imms, "1")
733733
self.imms_repo.delete_immunization.return_value = imms
734734

735735
# When
736736
act_imms = self.fhir_service.delete_immunization(self.TEST_IMMUNISATION_ID, "Test")
737737

738738
# Then
739-
self.imms_repo.get_immunization_by_id.assert_called_once_with(self.TEST_IMMUNISATION_ID)
739+
self.imms_repo.get_immunization_and_version_by_id.assert_called_once_with(self.TEST_IMMUNISATION_ID)
740740
self.imms_repo.delete_immunization.assert_called_once_with(self.TEST_IMMUNISATION_ID, "Test")
741741
self.authoriser.authorise.assert_called_once_with("Test", ApiOperationCode.DELETE, {"COVID19"})
742742
self.assertIsInstance(act_imms, Immunization)
743743
self.assertEqual(act_imms.id, self.TEST_IMMUNISATION_ID)
744744

745745
def test_delete_immunization_throws_not_found_exception_if_does_not_exist(self):
746746
"""it should raise a ResourceNotFound exception if the immunisation does not exist"""
747-
self.imms_repo.get_immunization_by_id.return_value = (None, None)
747+
self.imms_repo.get_immunization_and_version_by_id.return_value = (None, None)
748748

749749
# When
750750
with self.assertRaises(ResourceNotFoundError):
751751
self.fhir_service.delete_immunization(self.TEST_IMMUNISATION_ID, "Test")
752752

753753
# Then
754-
self.imms_repo.get_immunization_by_id.assert_called_once_with(self.TEST_IMMUNISATION_ID)
754+
self.imms_repo.get_immunization_and_version_by_id.assert_called_once_with(self.TEST_IMMUNISATION_ID)
755755
self.imms_repo.delete_immunization.assert_not_called()
756756

757757
def test_delete_immunization_throws_authorisation_exception_if_does_not_have_required_permissions(self):
758758
imms = json.loads(create_covid_19_immunization(self.TEST_IMMUNISATION_ID).json())
759759
self.mock_redis_client.hget.return_value = "FLU"
760760
self.authoriser.authorise.return_value = False
761-
self.imms_repo.get_immunization_by_id.return_value = (imms, "1")
761+
self.imms_repo.get_immunization_and_version_by_id.return_value = (imms, "1")
762762

763763
# When
764764
with self.assertRaises(UnauthorizedVaxError):
765765
self.fhir_service.delete_immunization(self.TEST_IMMUNISATION_ID, "Test")
766766

767767
# Then
768-
self.imms_repo.get_immunization_by_id.assert_called_once_with(self.TEST_IMMUNISATION_ID)
768+
self.imms_repo.get_immunization_and_version_by_id.assert_called_once_with(self.TEST_IMMUNISATION_ID)
769769
self.imms_repo.delete_immunization.assert_not_called()
770770
self.authoriser.authorise.assert_called_once_with("Test", ApiOperationCode.DELETE, {"FLU"})
771771

0 commit comments

Comments
 (0)