Skip to content

Commit f06593a

Browse files
committed
Completed tests for get by identifier
1 parent 271c6a5 commit f06593a

File tree

4 files changed

+83
-71
lines changed

4 files changed

+83
-71
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
return form_json(imms_resp, element, identifier, base_url)
7779

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)
@@ -460,7 +460,7 @@ def test_validate_imms_id_invalid_vaccinetype(self, mock_get_permissions):
460460
# Then
461461
mock_get_permissions.assert_called_once_with("test")
462462
self.service.get_immunization_by_identifier.assert_called_once_with(
463-
identifiers, ["COVID19.CRUDS"], identifier, _element
463+
identifiers, "test", identifier, _element
464464
)
465465

466466
self.assertEqual(response["statusCode"], 403)
@@ -510,7 +510,7 @@ def test_get_imms_by_identifier(self,mock_get_permissions):
510510
# Then
511511
mock_get_permissions.assert_called_once_with("test")
512512
self.service.get_immunization_by_identifier.assert_called_once_with(
513-
identifiers, ["COVID19.CRUDS"], converted_identifier, converted_element
513+
identifiers, "test", converted_identifier, converted_element
514514
)
515515
self.assertEqual(response["statusCode"], 200)
516516
body = json.loads(response["body"])
@@ -543,7 +543,7 @@ def test_not_found_for_identifier(self, mock_get_permissions):
543543
# Then
544544
mock_get_permissions.assert_called_once_with("test")
545545
self.service.get_immunization_by_identifier.assert_called_once_with(
546-
identifiers, ["COVID19.CRUDS"], converted_identifier, converted_element
546+
identifiers, "test", converted_identifier, converted_element
547547
)
548548
self.assertEqual(response["statusCode"], 200)
549549
body = json.loads(response["body"])
@@ -779,7 +779,7 @@ def test_validate_imms_id_invalid_vaccinetype(self, mock_get_permissions):
779779
# Then
780780
mock_get_permissions.assert_called_once_with("test")
781781
self.service.get_immunization_by_identifier.assert_called_once_with(
782-
identifiers, ["COVID19.CRUDS"], converted_identifer, converted_element
782+
identifiers, "test", converted_identifer, converted_element
783783
)
784784

785785
self.assertEqual(response["statusCode"], 403)
@@ -893,10 +893,10 @@ def setUp(self):
893893
self.controller = FhirController(self.authorizer, self.service)
894894
self.logger_info_patcher = patch("logging.Logger.info")
895895
self.mock_logger_info = self.logger_info_patcher.start()
896-
896+
897897
def tearDown(self):
898898
patch.stopall()
899-
899+
900900
@patch("fhir_controller.get_supplier_permissions")
901901
def test_create_immunization(self,mock_get_permissions):
902902
"""it should create Immunization and return resource's location"""
@@ -1050,7 +1050,7 @@ def setUp(self):
10501050
self.controller = FhirController(self.authorizer, self.service)
10511051
self.logger_info_patcher = patch("logging.Logger.info")
10521052
self.mock_logger_info = self.logger_info_patcher.start()
1053-
1053+
10541054
def tearDown(self):
10551055
patch.stopall()
10561056

@@ -1495,7 +1495,7 @@ def test_update_immunization_for_batch_with_invalid_json(self, mock_get_supplier
14951495
self.assertEqual(response["statusCode"], 400)
14961496
outcome = json.loads(response["body"])
14971497
self.assertEqual(outcome["resourceType"], "OperationOutcome")
1498-
1498+
14991499
@patch("fhir_controller.get_supplier_permissions")
15001500
def test_update_immunization_when_reinstated_true(self, mock_get_permissions):
15011501
"""it should update reinstated Immunization"""
@@ -1533,7 +1533,7 @@ def test_update_immunization_missing_id(self):
15331533
}
15341534
with self.assertRaises(KeyError):
15351535
self.controller.update_immunization(aws_event)
1536-
1536+
15371537
@patch("fhir_controller.get_supplier_permissions")
15381538
def test_update_reinstated_immunization_with_diagnostics_error(self, mock_get_permissions):
15391539
"""it should return 400 if patient validation error is present"""
@@ -1570,10 +1570,10 @@ def setUp(self):
15701570
self.controller = FhirController(self.authorizer, self.service)
15711571
self.logger_info_patcher = patch("logging.Logger.info")
15721572
self.mock_logger_info = self.logger_info_patcher.start()
1573-
1573+
15741574
def tearDown(self):
15751575
patch.stopall()
1576-
1576+
15771577
def test_validate_imms_id(self):
15781578
"""it should validate lambda's Immunization id"""
15791579
invalid_id = {"pathParameters": {"id": "invalid %$ id"}, "headers": {"SupplierSystem": "Test"}}

backend/tests/test_fhir_repository.py

Lines changed: 14 additions & 33 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,44 +66,25 @@ 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

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

10080
def test_immunization_not_found(self):
10181
"""it should return None if Immunization doesn't exist"""
10282
imms_id = "non-existent-id"
10383
self.table.query = MagicMock(return_value={})
10484

105-
imms = self.repository.get_immunization_by_identifier(imms_id, ["COVID19.CRUD"])
106-
self.assertIsNone(imms)
85+
immunisation, immunisation_type = self.repository.get_immunization_by_identifier(imms_id)
86+
self.assertIsNone(immunisation)
87+
self.assertIsNone(immunisation_type)
10788

10889

10990
class TestGetImmunization(unittest.TestCase):
@@ -112,7 +93,7 @@ def setUp(self):
11293
self.repository = ImmunizationRepository(table=self.table)
11394
self.logger_info_patcher = patch("logging.Logger.info")
11495
self.mock_logger_info = self.logger_info_patcher.start()
115-
96+
11697
def tearDown(self):
11798
patch.stopall()
11899

@@ -382,7 +363,7 @@ def test_create_patient_with_unauthorised_vaccine_type_permissions(self):
382363
with self.assertRaises(UnauthorizedVaxError) as e:
383364
# When
384365
self.repository.create_immunization(imms, self.patient, ["COVID19.CRUD"], "Test")
385-
366+
386367

387368

388369
class TestUpdateImmunization(TestFhirRepositoryBase):
@@ -484,7 +465,7 @@ def test_update_throws_error_when_identifier_already_in_dynamodb(self):
484465
self.repository.update_immunization(imms_id, imms, self.patient, 1, ["COVID19.CRUD"], "Test")
485466

486467
self.assertEqual(str(e.exception), f"The provided identifier: {identifier} is duplicated")
487-
468+
488469
def test_reinstate_immunization_success(self):
489470
"""it should reinstate an immunization successfully"""
490471
self.mock_redis_client.hget.return_value = "COVID19"
@@ -506,7 +487,7 @@ def test_reinstate_immunization_success(self):
506487

507488
self.assertEqual(result, resource)
508489
self.assertEqual(version, 2)
509-
490+
510491
def test_update_reinstated_immunization_success(self):
511492
"""it should update a reinstated immunization successfully"""
512493
self.mock_redis_client.hget.return_value = "COVID19"
@@ -536,10 +517,10 @@ def setUp(self):
536517
self.repository = ImmunizationRepository(table=self.table)
537518
self.logger_info_patcher = patch("logging.Logger.info")
538519
self.mock_logger_info = self.logger_info_patcher.start()
539-
520+
540521
def tearDown(self):
541522
patch.stopall()
542-
523+
543524
def test_get_deleted_immunization(self):
544525
"""it should return None if Immunization is logically deleted"""
545526
imms_id = "a-deleted-id"

0 commit comments

Comments
 (0)