Skip to content

Commit 947349b

Browse files
committed
Completed tests for get by identifier
1 parent 5617d09 commit 947349b

File tree

4 files changed

+89
-74
lines changed

4 files changed

+89
-74
lines changed

backend/src/fhir_service.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
BundleEntrySearch,
1313
)
1414
from fhir.resources.R4B.immunization import Immunization
15+
from poetry.console.commands import self
1516
from pydantic import ValidationError
1617

1718
import parameter_parser
@@ -51,9 +52,10 @@ class FhirService:
5152
def __init__(
5253
self,
5354
imms_repo: ImmunizationRepository,
55+
authoriser: Authoriser = Authoriser(),
5456
validator: ImmunizationValidator = ImmunizationValidator(),
5557
):
56-
self.authoriser = Authoriser()
58+
self.authoriser = authoriser
5759
self.immunization_repo = imms_repo
5860
self.validator = validator
5961

@@ -71,7 +73,7 @@ def get_immunization_by_identifier(
7173
return form_json(imms_resp, None, None, base_url)
7274

7375
if not self.authoriser.authorise(supplier_name, ApiOperationCode.SEARCH, {vaccination_type}):
74-
raise UnauthorizedVaxError
76+
raise UnauthorizedVaxError()
7577

7678
patient_full_url = f"urn:uuid:{str(uuid4())}"
7779
filtered_resource = Filter.search(imms_resp['resource'], patient_full_url)

backend/tests/test_fhir_controller.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def test_get_imms_by_identifer(self, mock_get_permissions):
112112
# Then
113113
mock_get_permissions.assert_called_once_with("test")
114114
self.service.get_immunization_by_identifier.assert_called_once_with(
115-
identifiers, ["COVID19.CUDS"], identifier, _element
115+
identifiers, "test", identifier, _element
116116
)
117117

118118
self.assertEqual(response["statusCode"], 200)
@@ -187,7 +187,7 @@ def test_not_found_for_identifier(self, mock_get_permissions):
187187
# Then
188188
mock_get_permissions.assert_called_once_with("test")
189189
self.service.get_immunization_by_identifier.assert_called_once_with(
190-
imms, ["COVID19.S"], identifier, _element
190+
imms, "test", identifier, _element
191191
)
192192

193193
self.assertEqual(response["statusCode"], 200)
@@ -407,7 +407,7 @@ def test_validate_imms_id_invalid_vaccinetype(self, mock_get_permissions):
407407
# Then
408408
mock_get_permissions.assert_called_once_with("test")
409409
self.service.get_immunization_by_identifier.assert_called_once_with(
410-
identifiers, ["COVID19.CRUDS"], identifier, _element
410+
identifiers, "test", identifier, _element
411411
)
412412

413413
self.assertEqual(response["statusCode"], 403)
@@ -457,7 +457,7 @@ def test_get_imms_by_identifier(self,mock_get_permissions):
457457
# Then
458458
mock_get_permissions.assert_called_once_with("test")
459459
self.service.get_immunization_by_identifier.assert_called_once_with(
460-
identifiers, ["COVID19.CRUDS"], converted_identifier, converted_element
460+
identifiers, "test", converted_identifier, converted_element
461461
)
462462
self.assertEqual(response["statusCode"], 200)
463463
body = json.loads(response["body"])
@@ -490,7 +490,7 @@ def test_not_found_for_identifier(self, mock_get_permissions):
490490
# Then
491491
mock_get_permissions.assert_called_once_with("test")
492492
self.service.get_immunization_by_identifier.assert_called_once_with(
493-
identifiers, ["COVID19.CRUDS"], converted_identifier, converted_element
493+
identifiers, "test", converted_identifier, converted_element
494494
)
495495
self.assertEqual(response["statusCode"], 200)
496496
body = json.loads(response["body"])
@@ -726,7 +726,7 @@ def test_validate_imms_id_invalid_vaccinetype(self, mock_get_permissions):
726726
# Then
727727
mock_get_permissions.assert_called_once_with("test")
728728
self.service.get_immunization_by_identifier.assert_called_once_with(
729-
identifiers, ["COVID19.CRUDS"], converted_identifier, converted_element
729+
identifiers, "test", converted_identifier, converted_element
730730
)
731731

732732
self.assertEqual(response["statusCode"], 403)
@@ -840,10 +840,10 @@ def setUp(self):
840840
self.controller = FhirController(self.authorizer, self.service)
841841
self.logger_info_patcher = patch("logging.Logger.info")
842842
self.mock_logger_info = self.logger_info_patcher.start()
843-
843+
844844
def tearDown(self):
845845
patch.stopall()
846-
846+
847847
@patch("fhir_controller.get_supplier_permissions")
848848
def test_create_immunization(self,mock_get_permissions):
849849
"""it should create Immunization and return resource's location"""
@@ -997,7 +997,7 @@ def setUp(self):
997997
self.controller = FhirController(self.authorizer, self.service)
998998
self.logger_info_patcher = patch("logging.Logger.info")
999999
self.mock_logger_info = self.logger_info_patcher.start()
1000-
1000+
10011001
def tearDown(self):
10021002
patch.stopall()
10031003

@@ -1442,7 +1442,7 @@ def test_update_immunization_for_batch_with_invalid_json(self, mock_get_supplier
14421442
self.assertEqual(response["statusCode"], 400)
14431443
outcome = json.loads(response["body"])
14441444
self.assertEqual(outcome["resourceType"], "OperationOutcome")
1445-
1445+
14461446
@patch("fhir_controller.get_supplier_permissions")
14471447
def test_update_immunization_when_reinstated_true(self, mock_get_permissions):
14481448
"""it should update reinstated Immunization"""
@@ -1480,7 +1480,7 @@ def test_update_immunization_missing_id(self):
14801480
}
14811481
with self.assertRaises(KeyError):
14821482
self.controller.update_immunization(aws_event)
1483-
1483+
14841484
@patch("fhir_controller.get_supplier_permissions")
14851485
def test_update_reinstated_immunization_with_diagnostics_error(self, mock_get_permissions):
14861486
"""it should return 400 if patient validation error is present"""
@@ -1517,10 +1517,10 @@ def setUp(self):
15171517
self.controller = FhirController(self.authorizer, self.service)
15181518
self.logger_info_patcher = patch("logging.Logger.info")
15191519
self.mock_logger_info = self.logger_info_patcher.start()
1520-
1520+
15211521
def tearDown(self):
15221522
patch.stopall()
1523-
1523+
15241524
def test_validate_imms_id(self):
15251525
"""it should validate lambda's Immunization id"""
15261526
invalid_id = {"pathParameters": {"id": "invalid %$ id"}, "headers": {"SupplierSystem": "Test"}}

backend/tests/test_fhir_repository.py

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def setUp(self):
4545
self.repository = ImmunizationRepository(table=self.table)
4646
self.logger_info_patcher = patch("logging.Logger.info")
4747
self.mock_logger_info = self.logger_info_patcher.start()
48-
48+
4949
def tearDown(self):
5050
patch.stopall()
5151

@@ -66,46 +66,26 @@ def test_get_immunization_by_identifier(self):
6666
}
6767
)
6868

69-
imms = self.repository.get_immunization_by_identifier(imms_id, ["COVID19.CRUDS"])
69+
immunisation, immunisation_type = self.repository.get_immunization_by_identifier(imms_id)
7070

71-
# Validate the results
72-
self.assertDictEqual(resource["Resource"], imms["resource"])
73-
self.assertEqual(imms["version"], 1)
74-
self.assertEqual(imms["id"], "test")
75-
# self.table.get_item.assert_called_once_with(Key={"PK": (imms_id)})
7671
self.table.query.assert_called_once_with(
7772
IndexName="IdentifierGSI",
7873
KeyConditionExpression=Key("IdentifierPK").eq(imms_id),
7974
)
80-
81-
def test_unauthorized_get_immunization_by_identifier(self):
82-
"""it should not get an Immunization by id if vax perms do not exist"""
83-
imms_id = "a-id#an-id"
84-
resource = dict()
85-
resource["Resource"] = {"foo": "bar"}
86-
resource["Version"] = 1
87-
self.table.query = MagicMock(
88-
return_value={
89-
"Items": [
90-
{
91-
"Resource": json.dumps({"foo": "bar", "id": "test"}),
92-
"Version": 1,
93-
"PatientSK": "COVID19#2516525251",
94-
}
95-
]
96-
}
97-
)
98-
with self.assertRaises(UnauthorizedVaxError) as e:
99-
# When
100-
self.repository.get_immunization_by_identifier(imms_id, ["FLU.CRUD"])
75+
76+
self.assertDictEqual(immunisation["resource"], resource["Resource"])
77+
self.assertEqual(immunisation["version"], 1)
78+
self.assertEqual(immunisation["id"], "test")
79+
self.assertEqual(immunisation_type, "covid19")
10180

10281
def test_immunization_not_found(self):
10382
"""it should return None if Immunization doesn't exist"""
10483
imms_id = "non-existent-id"
10584
self.table.query = MagicMock(return_value={})
10685

107-
imms = self.repository.get_immunization_by_identifier(imms_id, ["COVID19.CRUD"])
108-
self.assertIsNone(imms)
86+
immunisation, immunisation_type = self.repository.get_immunization_by_identifier(imms_id)
87+
self.assertIsNone(immunisation)
88+
self.assertIsNone(immunisation_type)
10989

11090

11191
class TestGetImmunization(unittest.TestCase):
@@ -114,7 +94,7 @@ def setUp(self):
11494
self.repository = ImmunizationRepository(table=self.table)
11595
self.logger_info_patcher = patch("logging.Logger.info")
11696
self.mock_logger_info = self.logger_info_patcher.start()
117-
97+
11898
def tearDown(self):
11999
patch.stopall()
120100

@@ -384,7 +364,7 @@ def test_create_patient_with_unauthorised_vaccine_type_permissions(self):
384364
with self.assertRaises(UnauthorizedVaxError) as e:
385365
# When
386366
self.repository.create_immunization(imms, self.patient, ["COVID19.CRUD"], "Test")
387-
367+
388368

389369

390370
class TestUpdateImmunization(TestFhirRepositoryBase):
@@ -486,7 +466,7 @@ def test_update_throws_error_when_identifier_already_in_dynamodb(self):
486466
self.repository.update_immunization(imms_id, imms, self.patient, 1, ["COVID19.CRUD"], "Test")
487467

488468
self.assertEqual(str(e.exception), f"The provided identifier: {identifier} is duplicated")
489-
469+
490470
def test_reinstate_immunization_success(self):
491471
"""it should reinstate an immunization successfully"""
492472
self.mock_redis_client.hget.return_value = "COVID19"
@@ -508,7 +488,7 @@ def test_reinstate_immunization_success(self):
508488

509489
self.assertEqual(result, resource)
510490
self.assertEqual(version, 2)
511-
491+
512492
def test_update_reinstated_immunization_success(self):
513493
"""it should update a reinstated immunization successfully"""
514494
self.mock_redis_client.hget.return_value = "COVID19"
@@ -538,10 +518,10 @@ def setUp(self):
538518
self.repository = ImmunizationRepository(table=self.table)
539519
self.logger_info_patcher = patch("logging.Logger.info")
540520
self.mock_logger_info = self.logger_info_patcher.start()
541-
521+
542522
def tearDown(self):
543523
patch.stopall()
544-
524+
545525
def test_get_deleted_immunization(self):
546526
"""it should return None if Immunization is logically deleted"""
547527
imms_id = "a-deleted-id"

0 commit comments

Comments
 (0)