Skip to content

Commit 18d41fb

Browse files
committed
Tests Pass
1 parent 3a6a304 commit 18d41fb

File tree

2 files changed

+70
-23
lines changed

2 files changed

+70
-23
lines changed

backend/tests/test_fhir_controller.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,35 @@
3232
from parameter_parser import patient_identifier_system, process_search_params
3333
from tests.utils.generic_utils import load_json_data
3434
from tests.utils.values_for_tests import ValidValues
35+
from utils.mock_redis import MOCK_REDIS_V2D_RESPONSE
3536

36-
"test"
37+
class TestFhirControllerBase(unittest.TestCase):
38+
"""Base class for all tests to set up common fixtures"""
3739

38-
39-
class TestFhirController(unittest.TestCase):
4040
def setUp(self):
41+
super().setUp()
42+
self.redis_patcher = patch("parameter_parser.redis_client")
43+
self.mock_redis_client = self.redis_patcher.start()
44+
self.logger_info_patcher = patch("logging.Logger.info")
45+
self.mock_logger_info = self.logger_info_patcher.start()
46+
47+
def tearDown(self):
48+
self.redis_patcher.stop()
49+
self.logger_info_patcher.stop()
50+
super().tearDown()
51+
52+
class TestFhirController(TestFhirControllerBase):
53+
def setUp(self):
54+
super().setUp()
4155
self.service = create_autospec(FhirService)
4256
self.repository = create_autospec(ImmunizationRepository)
4357
self.authorizer = create_autospec(Authorization)
4458
self.controller = FhirController(self.authorizer, self.service)
4559

60+
def tearDown(self):
61+
self.redis_patcher.stop()
62+
super().tearDown()
63+
4664
def test_create_response(self):
4765
"""it should return application/fhir+json with correct status code"""
4866
body = {"message": "a body"}
@@ -133,7 +151,6 @@ def test_get_imms_by_identifer_header_missing(self):
133151
response = self.controller.get_immunization_by_identifier(lambda_event)
134152

135153
self.assertEqual(response["statusCode"], 403)
136-
137154
@patch("fhir_controller.get_supplier_permissions")
138155
def test_not_found_for_identifier(self, mock_get_permissions):
139156
"""it should return not-found OperationOutcome if it doesn't exist"""
@@ -165,7 +182,6 @@ def test_not_found_for_identifier(self, mock_get_permissions):
165182

166183
imms = identifier.replace("|", "#")
167184
# When
168-
169185
response = self.controller.get_immunization_by_identifier(lambda_event)
170186

171187
# Then
@@ -200,7 +216,6 @@ def test_get_imms_by_identifer_patient_identifier_and_element_present(self, mock
200216
self.assertEqual(response["statusCode"], 400)
201217
body = json.loads(response["body"])
202218
self.assertEqual(body["resourceType"], "OperationOutcome")
203-
204219
@patch("fhir_controller.get_supplier_permissions")
205220
def test_get_imms_by_identifer_both_body_and_query_params_present(self, mock_get_supplier_permissions):
206221
"""it should return Immunization Id if it exists"""
@@ -422,7 +437,6 @@ def test_validate_immunization_identifier_having_whitespace(self,mock_get_suppli
422437
self.assertEqual(response["statusCode"], 400)
423438
outcome = json.loads(response["body"])
424439
self.assertEqual(outcome["resourceType"], "OperationOutcome")
425-
426440
@patch("fhir_controller.get_supplier_permissions")
427441
def test_validate_imms_id_invalid_vaccinetype(self, mock_get_permissions):
428442
"""it should validate lambda's Immunization id"""
@@ -738,7 +752,6 @@ def test_validate_immunization_identifier_having_whitespace(self,mock_get_suppli
738752
self.assertEqual(response["statusCode"], 400)
739753
outcome = json.loads(response["body"])
740754
self.assertEqual(outcome["resourceType"], "OperationOutcome")
741-
742755
@patch("fhir_controller.get_supplier_permissions")
743756
def test_validate_imms_id_invalid_vaccinetype(self, mock_get_permissions):
744757
"""it should validate lambda's Immunization id"""
@@ -820,7 +833,6 @@ def test_get_imms_by_id_unauthorised_vax_error(self,mock_permissions):
820833
# Then
821834
mock_permissions.assert_called_once_with("test")
822835
self.assertEqual(response["statusCode"], 403)
823-
824836
@patch("fhir_controller.get_supplier_permissions")
825837
def test_get_imms_by_id_no_vax_permission(self, mock_permissions):
826838
"""it should return Immunization Id if it exists"""
@@ -1142,7 +1154,6 @@ def test_update_immunization_UnauthorizedVaxError(self, mock_get_supplier_permis
11421154
response = self.controller.update_immunization(aws_event)
11431155
mock_get_supplier_permissions.assert_called_once_with("Test")
11441156
self.assertEqual(response["statusCode"], 403)
1145-
11461157
@patch("fhir_controller.get_supplier_permissions")
11471158
def test_update_immunization_UnauthorizedVaxError_check_for_non_batch(self, mock_get_supplier_permissions):
11481159
"""it should not update the Immunization record"""
@@ -1573,7 +1584,6 @@ def test_immunization_exception_not_found(self, mock_get_permissions):
15731584
body = json.loads(response["body"])
15741585
self.assertEqual(body["resourceType"], "OperationOutcome")
15751586
self.assertEqual(body["issue"][0]["code"], "not-found")
1576-
15771587
@patch("fhir_controller.get_supplier_permissions")
15781588
def test_immunization_unhandled_error(self, mock_get_supplier_permissions):
15791589
"""it should return server-error OperationOutcome if service throws UnhandledResponseError"""
@@ -1596,8 +1606,9 @@ def test_immunization_unhandled_error(self, mock_get_supplier_permissions):
15961606
self.assertEqual(body["resourceType"], "OperationOutcome")
15971607
self.assertEqual(body["issue"][0]["code"], "exception")
15981608

1599-
class TestSearchImmunizations(unittest.TestCase):
1609+
class TestSearchImmunizations(TestFhirControllerBase):
16001610
def setUp(self):
1611+
super().setUp()
16011612
self.service = create_autospec(FhirService)
16021613
self.authorizer = create_autospec(Authorization)
16031614
self.controller = FhirController(self.authorizer, self.service)
@@ -1607,11 +1618,14 @@ def setUp(self):
16071618
self.date_to_key = "-date.to"
16081619
self.nhs_number_valid_value = "9000000009"
16091620
self.patient_identifier_valid_value = f"{patient_identifier_system}|{self.nhs_number_valid_value}"
1621+
self.mock_redis_client.hkeys.return_value = MOCK_REDIS_V2D_RESPONSE
1622+
1623+
def tearDown(self):
1624+
return super().tearDown()
16101625

16111626
@patch("fhir_controller.get_supplier_permissions")
16121627
def test_get_search_immunizations(self, mock_get_supplier_permissions):
16131628
"""it should search based on patient_identifier and immunization_target"""
1614-
16151629
mock_get_supplier_permissions.return_value = ["COVID19.S"]
16161630
search_result = Bundle.construct()
16171631
self.service.search_immunizations.return_value = search_result
@@ -1795,7 +1809,6 @@ def test_post_search_immunizations(self,mock_get_supplier_permissions):
17951809
"headers": {"Content-Type": "application/x-www-form-urlencoded", "SupplierSystem": "Test"},
17961810
"body": base64_encoded_body,
17971811
}
1798-
17991812
# When
18001813
response = self.controller.search_immunizations(lambda_event)
18011814
# Then
@@ -1906,6 +1919,7 @@ def test_post_search_immunizations_for_unauthorized_vaccine_type_search_403(self
19061919

19071920
@patch("fhir_controller.process_search_params", wraps=process_search_params)
19081921
def test_uses_parameter_parser(self, process_search_params: Mock):
1922+
self.mock_redis_client.hkeys.return_value = MOCK_REDIS_V2D_RESPONSE
19091923
lambda_event = {
19101924
"multiValueQueryStringParameters": {
19111925
self.patient_identifier_key: ["https://fhir.nhs.uk/Id/nhs-number|9000000009"],

backend/tests/test_fhir_service.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import datetime
44
import unittest
55
from copy import deepcopy
6+
from unittest.mock import create_autospec, patch
67
from unittest import skip
78
from unittest.mock import create_autospec
89
from decimal import Decimal
@@ -23,10 +24,25 @@
2324
create_covid_19_immunization_dict_no_id,
2425
VALID_NHS_NUMBER,
2526
)
26-
from .utils.generic_utils import load_json_data
27-
from src.constants import NHS_NUMBER_USED_IN_SAMPLE_DATA
27+
from utils.generic_utils import load_json_data
28+
from constants import NHS_NUMBER_USED_IN_SAMPLE_DATA
29+
30+
class TestFhirServiceBase(unittest.TestCase):
31+
"""Base class for all tests to set up common fixtures"""
32+
33+
def setUp(self):
34+
super().setUp()
35+
self.redis_patcher = patch("models.utils.validation_utils.redis_client")
36+
self.mock_redis_client = self.redis_patcher.start()
37+
self.logger_info_patcher = patch("logging.Logger.info")
38+
self.mock_logger_info = self.logger_info_patcher.start()
39+
40+
def tearDown(self):
41+
self.redis_patcher.stop()
42+
self.logger_info_patcher.stop()
43+
super().tearDown()
44+
2845

29-
"test"
3046
class TestServiceUrl(unittest.TestCase):
3147
def test_get_service_url(self):
3248
"""it should create service url"""
@@ -51,15 +67,20 @@ def test_get_service_url(self):
5167
self.assertEqual(url, f"https://internal-dev.api.service.nhs.uk/{base_path}")
5268

5369

54-
class TestGetImmunizationByAll(unittest.TestCase):
70+
class TestGetImmunizationByAll(TestFhirServiceBase):
5571
"""Tests for FhirService.get_immunization_by_id"""
5672

5773
def setUp(self):
74+
super().setUp()
5875
self.imms_repo = create_autospec(ImmunizationRepository)
5976
self.pds_service = create_autospec(PdsService)
6077
self.validator = create_autospec(ImmunizationValidator)
6178
self.fhir_service = FhirService(self.imms_repo, self.pds_service, self.validator)
6279

80+
def tearDown(self):
81+
super().tearDown()
82+
patch.stopall()
83+
6384
def test_get_immunization_by_id_by_all(self):
6485
"""it should find an Immunization by id"""
6586
imms_id = "an-id"
@@ -119,7 +140,8 @@ def test_pre_validation_failed(self):
119140
self.assertEqual(error.exception.message, expected_msg)
120141
self.imms_repo.update_immunization.assert_not_called()
121142

122-
def test_post_validation_failed(self):
143+
def test_post_validation_failed_get_by_all(self):
144+
self.mock_redis_client.hget.side_effect = [None, 'COVID-19']
123145
valid_imms = create_covid_19_immunization_dict("an-id", VALID_NHS_NUMBER)
124146

125147
bad_target_disease_imms = deepcopy(valid_imms)
@@ -150,14 +172,19 @@ def test_post_validation_failed(self):
150172
self.imms_repo.get_immunization_by_id_all.assert_not_called()
151173

152174

153-
class TestGetImmunization(unittest.TestCase):
175+
class TestGetImmunization(TestFhirServiceBase):
154176
"""Tests for FhirService.get_immunization_by_id"""
155177

156178
def setUp(self):
179+
super().setUp()
157180
self.imms_repo = create_autospec(ImmunizationRepository)
158181
self.pds_service = create_autospec(PdsService)
159182
self.validator = create_autospec(ImmunizationValidator)
160183
self.fhir_service = FhirService(self.imms_repo, self.pds_service, self.validator)
184+
self.logger_info_patcher = patch("logging.Logger.info")
185+
self.mock_logger_info = self.logger_info_patcher.start()
186+
def tearDown(self):
187+
patch.stopall()
161188

162189
def test_get_immunization_by_id(self):
163190
"""it should find an Immunization by id"""
@@ -234,7 +261,8 @@ def test_pre_validation_failed(self):
234261
self.imms_repo.update_immunization.assert_not_called()
235262
self.pds_service.get_patient_details.assert_not_called()
236263

237-
def test_post_validation_failed(self):
264+
def test_post_validation_failed_get(self):
265+
self.mock_redis_client.hget.side_effect = [None, 'COVID-19']
238266
valid_imms = create_covid_19_immunization_dict("an-id", VALID_NHS_NUMBER)
239267

240268
bad_target_disease_imms = deepcopy(valid_imms)
@@ -309,10 +337,11 @@ def test_immunization_not_found(self):
309337
self.assertEqual(act_imms["entry"], [])
310338

311339

312-
class TestCreateImmunization(unittest.TestCase):
340+
class TestCreateImmunization(TestFhirServiceBase):
313341
"""Tests for FhirService.create_immunization"""
314342

315343
def setUp(self):
344+
super().setUp()
316345
self.imms_repo = create_autospec(ImmunizationRepository)
317346
self.pds_service = create_autospec(PdsService)
318347
self.validator = create_autospec(ImmunizationValidator)
@@ -323,6 +352,10 @@ def setUp(self):
323352
ImmunizationValidator(add_post_validators=False),
324353
)
325354

355+
def tearDown(self):
356+
patch.stopall()
357+
super().tearDown()
358+
326359
def test_create_immunization(self):
327360
"""it should create Immunization and validate it"""
328361
imms_id = "an-id"
@@ -382,7 +415,7 @@ def test_pre_validation_failed(self):
382415

383416
def test_post_validation_failed(self):
384417
"""it should throw exception if Immunization is not valid"""
385-
418+
self.mock_redis_client.hget.side_effect = [None, 'COVID-19']
386419
valid_imms = create_covid_19_immunization_dict_no_id(VALID_NHS_NUMBER)
387420

388421
bad_target_disease_imms = deepcopy(valid_imms)

0 commit comments

Comments
 (0)