Skip to content

Commit dd92d51

Browse files
committed
20 errors
1 parent f6de61a commit dd92d51

File tree

6 files changed

+133
-36
lines changed

6 files changed

+133
-36
lines changed

backend/src/models/utils/validation_utils.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Utils for backend folder"""
22

33
import json
4-
4+
import inspect
55

66
from typing import Union
77
from .generic_utils import create_diagnostics_error
@@ -54,13 +54,7 @@ def convert_disease_codes_to_vaccine_type(disease_codes_input: list) -> Union[st
5454
otherwise raises a value error
5555
"""
5656
key = ":".join(sorted(disease_codes_input))
57-
# vaccine_type = redis_client.hget("diseases_to_vacc", key)
58-
59-
60-
if isinstance(redis_client.hget, Mock):
61-
vaccine_type = redis_client.hget("diseases_to_vacc", key)
62-
else:
63-
print("XXXXXX ------>>> redis_client.hget is mocked")
57+
vaccine_type = redis_client.hget("diseases_to_vacc", key)
6458

6559
if not vaccine_type:
6660
raise ValueError(

backend/tests/test_fhir_batch_service.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
import unittest
22
import uuid
33
from copy import deepcopy
4-
from unittest.mock import Mock, create_autospec
4+
from unittest.mock import Mock, create_autospec, patch
55
from tests.utils.immunization_utils import create_covid_19_immunization_dict_no_id
66
from models.errors import CustomValidationError
77
from models.fhir_immunization import ImmunizationValidator
88
from fhir_batch_repository import ImmunizationBatchRepository
99
from fhir_batch_service import ImmunizationBatchService
10+
from sample_data.mock_redis_cache import fake_hget
1011

11-
class TestCreateImmunizationBatchService(unittest.TestCase):
12+
13+
class TestFhirBatchServiceBase(unittest.TestCase):
14+
"""Base class for all tests to set up common fixtures"""
15+
16+
def setUp(self):
17+
super().setUp()
18+
self.redis_patcher = patch("models.utils.validation_utils.redis_client")
19+
self.mock_redis_client = self.redis_patcher.start()
20+
self.mock_redis_client.hget.side_effect = fake_hget
21+
self.logger_info_patcher = patch("logging.Logger.info")
22+
self.mock_logger_info = self.logger_info_patcher.start()
23+
24+
def tearDown(self):
25+
self.redis_patcher.stop()
26+
self.logger_info_patcher.stop()
27+
super().tearDown()
28+
29+
30+
class TestCreateImmunizationBatchService(TestFhirBatchServiceBase):
1231

1332
def setUp(self):
33+
super().setUp()
1434
self.mock_repo = create_autospec(ImmunizationBatchRepository)
1535
self.mock_validator = create_autospec(ImmunizationValidator)
1636
self.mock_table = Mock()
@@ -19,6 +39,14 @@ def setUp(self):
1939
self.mock_repo, ImmunizationValidator(add_post_validators=False)
2040
)
2141

42+
def tearDown(self):
43+
super().tearDown()
44+
self.mock_repo.reset_mock()
45+
self.mock_validator.reset_mock()
46+
self.mock_table.reset_mock()
47+
self.service = None
48+
self.pre_validate_fhir_service = None
49+
2250
def test_create_immunization_valid(self):
2351
"""it should create Immunization and return imms id location"""
2452

@@ -69,9 +97,10 @@ def test_create_immunization_post_validation_error(self):
6997
self.mock_repo.create_immunization.assert_not_called()
7098

7199

72-
class TestUpdateImmunizationBatchService(unittest.TestCase):
100+
class TestUpdateImmunizationBatchService(TestFhirBatchServiceBase):
73101

74102
def setUp(self):
103+
super().setUp()
75104
self.mock_repo = create_autospec(ImmunizationBatchRepository)
76105
self.mock_validator = create_autospec(ImmunizationValidator)
77106
self.mock_table = Mock()
@@ -80,6 +109,14 @@ def setUp(self):
80109
self.mock_repo, ImmunizationValidator(add_post_validators=False)
81110
)
82111

112+
def tearDown(self):
113+
super().tearDown()
114+
self.mock_repo.reset_mock()
115+
self.mock_validator.reset_mock()
116+
self.mock_table.reset_mock()
117+
self.service = None
118+
self.pre_validate_fhir_service = None
119+
83120
def test_update_immunization_valid(self):
84121
"""it should update Immunization and return imms id"""
85122

backend/tests/test_fhir_repository.py

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
import botocore.exceptions
88
from boto3.dynamodb.conditions import Attr, Key
9-
from src.fhir_repository import ImmunizationRepository
10-
from src.models.utils.validation_utils import get_vaccine_type
9+
from fhir_repository import ImmunizationRepository
10+
from models.utils.validation_utils import get_vaccine_type
1111
from models.errors import (
1212
ResourceNotFoundError,
1313
UnhandledResponseError,
@@ -16,9 +16,7 @@
1616
)
1717
from tests.utils.generic_utils import update_target_disease_code
1818
from tests.utils.immunization_utils import create_covid_19_immunization_dict
19-
20-
"test"
21-
19+
from sample_data.mock_redis_cache import fake_hget
2220

2321
def _make_immunization_pk(_id):
2422
return f"Immunization#{_id}"
@@ -27,12 +25,33 @@ def _make_immunization_pk(_id):
2725
def _make_patient_pk(_id):
2826
return f"Patient#{_id}"
2927

28+
class TestFhirRepositoryBase(unittest.TestCase):
29+
"""Base class for all tests to set up common fixtures"""
30+
31+
def setUp(self):
32+
super().setUp()
33+
self.redis_patcher = patch("models.utils.validation_utils.redis_client")
34+
self.mock_redis_client = self.redis_patcher.start()
35+
self.mock_redis_client.hget.side_effect = fake_hget
36+
self.logger_info_patcher = patch("logging.Logger.info")
37+
self.mock_logger_info = self.logger_info_patcher.start()
38+
39+
def tearDown(self):
40+
self.redis_patcher.stop()
41+
self.logger_info_patcher.stop()
42+
super().tearDown()
3043

31-
class TestGetImmunizationByIdentifier(unittest.TestCase):
44+
45+
class TestGetImmunizationByIdentifier(TestFhirRepository):
3246
def setUp(self):
47+
super().setUp()
3348
self.table = MagicMock()
3449
self.repository = ImmunizationRepository(table=self.table)
3550

51+
def tearDown(self):
52+
self.redis_patcher.stop()
53+
super().tearDown()
54+
3655
def test_get_immunization_by_identifier(self):
3756
"""it should find an Immunization by id"""
3857
imms_id = "a-id#an-id"
@@ -280,14 +299,18 @@ def test_create_throws_error_when_identifier_already_in_dynamodb(self):
280299
self.assertEqual(str(e.exception), f"The provided identifier: {identifier} is duplicated")
281300

282301

283-
class TestCreateImmunizationPatientIndex(unittest.TestCase):
302+
class TestCreateImmunizationPatientIndex(TestFhirRepositoryBase):
284303
"""create_immunization should create a patient record with vaccine type"""
285304

286305
def setUp(self):
306+
super().setUp()
287307
self.table = MagicMock()
288308
self.repository = ImmunizationRepository(table=self.table)
289309
self.patient = {"id": "a-patient-id"}
290310

311+
def tearDown(self):
312+
super().tearDown()
313+
291314
def test_create_patient_gsi(self):
292315
"""create Immunization method should create Patient index with nhs-number as ID and no system"""
293316
imms = create_covid_19_immunization_dict("an-id")
@@ -332,13 +355,17 @@ def test_create_patient_with_unauthorised_vaccine_type_permissions(self):
332355
self.repository.create_immunization(imms, self.patient, "COVID:create", "Test")
333356

334357

335-
class TestUpdateImmunization(unittest.TestCase):
358+
class TestUpdateImmunization(TestFhirRepository):
336359
def setUp(self):
360+
super().setUp()
337361
self.table = MagicMock()
338362
self.repository = ImmunizationRepository(table=self.table)
339363
self.patient = _make_a_patient("update-patient-id")
340364

341-
def test_update(self):
365+
def tearDown(self):
366+
return super().tearDown()
367+
368+
def test_update1(self):
342369
"""it should update record by replacing both Immunization and Patient"""
343370
imms_id = "an-imms-id"
344371
imms = create_covid_19_immunization_dict(imms_id)
@@ -651,14 +678,18 @@ def test_bad_response_from_dynamo(self):
651678
self.assertDictEqual(e.exception.response, response)
652679

653680

654-
class TestImmunizationDecimals(unittest.TestCase):
681+
class TestImmunizationDecimals(TestFhirRepository):
655682
"""It should create a record and keep decimal precision"""
656683

657684
def setUp(self):
685+
super().setUp()
658686
self.table = MagicMock()
659687
self.repository = ImmunizationRepository(table=self.table)
660688
self.patient = {"id": "a-patient-id", "identifier": {"value": "an-identifier"}}
661689

690+
def tearDown(self):
691+
return super().tearDown()
692+
662693
def test_decimal_on_create(self):
663694
"""it should create Immunization, and preserve decimal value"""
664695
imms = create_covid_19_immunization_dict(imms_id="an-id")

backend/tests/test_fhir_service.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import datetime
44
import unittest
55
from copy import deepcopy
6-
from unittest.mock import create_autospec
6+
from unittest.mock import create_autospec, patch
77
from decimal import Decimal
88

99
from fhir.resources.R4B.bundle import Bundle as FhirBundle, BundleEntry
@@ -21,10 +21,27 @@
2121
create_covid_19_immunization_dict_no_id,
2222
VALID_NHS_NUMBER,
2323
)
24-
from .utils.generic_utils import load_json_data
25-
from src.constants import NHS_NUMBER_USED_IN_SAMPLE_DATA
24+
from utils.generic_utils import load_json_data
25+
from constants import NHS_NUMBER_USED_IN_SAMPLE_DATA
26+
from sample_data.mock_redis_cache import fake_hget
27+
28+
class TestFhirServiceBase(unittest.TestCase):
29+
"""Base class for all tests to set up common fixtures"""
30+
31+
def setUp(self):
32+
super().setUp()
33+
self.redis_patcher = patch("models.utils.validation_utils.redis_client")
34+
self.mock_redis_client = self.redis_patcher.start()
35+
self.mock_redis_client.hget.side_effect = fake_hget
36+
self.logger_info_patcher = patch("logging.Logger.info")
37+
self.mock_logger_info = self.logger_info_patcher.start()
38+
39+
def tearDown(self):
40+
self.redis_patcher.stop()
41+
self.logger_info_patcher.stop()
42+
super().tearDown()
43+
2644

27-
"test"
2845
class TestServiceUrl(unittest.TestCase):
2946
def test_get_service_url(self):
3047
"""it should create service url"""
@@ -49,15 +66,20 @@ def test_get_service_url(self):
4966
self.assertEqual(url, f"https://internal-dev.api.service.nhs.uk/{base_path}")
5067

5168

52-
class TestGetImmunizationByAll(unittest.TestCase):
69+
class TestGetImmunizationByAll(TestFhirServiceBase):
5370
"""Tests for FhirService.get_immunization_by_id"""
5471

5572
def setUp(self):
73+
super().setUp()
5674
self.imms_repo = create_autospec(ImmunizationRepository)
5775
self.pds_service = create_autospec(PdsService)
5876
self.validator = create_autospec(ImmunizationValidator)
5977
self.fhir_service = FhirService(self.imms_repo, self.pds_service, self.validator)
6078

79+
def tearDown(self):
80+
super().tearDown()
81+
patch.stopall()
82+
6183
def test_get_immunization_by_id_by_all(self):
6284
"""it should find an Immunization by id"""
6385
imms_id = "an-id"
@@ -117,7 +139,7 @@ def test_pre_validation_failed(self):
117139
self.assertEqual(error.exception.message, expected_msg)
118140
self.imms_repo.update_immunization.assert_not_called()
119141

120-
def test_post_validation_failed(self):
142+
def test_post_validation_failed_get_by_all(self):
121143
valid_imms = create_covid_19_immunization_dict("an-id", VALID_NHS_NUMBER)
122144

123145
bad_target_disease_imms = deepcopy(valid_imms)
@@ -148,14 +170,19 @@ def test_post_validation_failed(self):
148170
self.imms_repo.get_immunization_by_id_all.assert_not_called()
149171

150172

151-
class TestGetImmunization(unittest.TestCase):
173+
class TestGetImmunization(TestFhirServiceBase):
152174
"""Tests for FhirService.get_immunization_by_id"""
153175

154176
def setUp(self):
177+
super().setUp()
155178
self.imms_repo = create_autospec(ImmunizationRepository)
156179
self.pds_service = create_autospec(PdsService)
157180
self.validator = create_autospec(ImmunizationValidator)
158181
self.fhir_service = FhirService(self.imms_repo, self.pds_service, self.validator)
182+
self.logger_info_patcher = patch("logging.Logger.info")
183+
self.mock_logger_info = self.logger_info_patcher.start()
184+
def tearDown(self):
185+
patch.stopall()
159186

160187
def test_get_immunization_by_id(self):
161188
"""it should find an Immunization by id"""
@@ -232,7 +259,7 @@ def test_pre_validation_failed(self):
232259
self.imms_repo.update_immunization.assert_not_called()
233260
self.pds_service.get_patient_details.assert_not_called()
234261

235-
def test_post_validation_failed(self):
262+
def test_post_validation_failed_get(self):
236263
valid_imms = create_covid_19_immunization_dict("an-id", VALID_NHS_NUMBER)
237264

238265
bad_target_disease_imms = deepcopy(valid_imms)
@@ -307,10 +334,11 @@ def test_immunization_not_found(self):
307334
self.assertEqual(act_imms["entry"], [])
308335

309336

310-
class TestCreateImmunization(unittest.TestCase):
337+
class TestCreateImmunization(TestFhirServiceBase):
311338
"""Tests for FhirService.create_immunization"""
312339

313340
def setUp(self):
341+
super().setUp()
314342
self.imms_repo = create_autospec(ImmunizationRepository)
315343
self.pds_service = create_autospec(PdsService)
316344
self.validator = create_autospec(ImmunizationValidator)
@@ -321,6 +349,10 @@ def setUp(self):
321349
ImmunizationValidator(add_post_validators=False),
322350
)
323351

352+
def tearDown(self):
353+
patch.stopall()
354+
super().tearDown()
355+
324356
def test_create_immunization(self):
325357
"""it should create Immunization and validate it"""
326358
imms_id = "an-id"
@@ -378,7 +410,7 @@ def test_pre_validation_failed(self):
378410
self.imms_repo.create_immunization.assert_not_called()
379411
self.pds_service.get_patient_details.assert_not_called()
380412

381-
def test_post_validation_failed(self):
413+
def test_post_validation_failed_create(self):
382414
"""it should throw exception if Immunization is not valid"""
383415

384416
valid_imms = create_covid_19_immunization_dict_no_id(VALID_NHS_NUMBER)

backend/tests/test_immunization_pre_validator.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,6 @@ def test_pre_validate_extension_url1(self):
765765
full_error_message = str(error.exception)
766766
actual_error_messages = full_error_message.replace("Validation errors: ", "").split("; ")
767767
self.assertIn("extension[0].url must be one of the following: https://fhir.hl7.org.uk/StructureDefinition/Extension-UKCore-VaccinationProcedure", actual_error_messages)
768-
print("hello world")
769768

770769
def test_pre_validate_extension_snomed_code(self):
771770
"""Test test_pre_validate_extension_url accepts valid values and rejects invalid values for extension[0].url"""

backend/tests/test_update_imms.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010

1111
class TestUpdateImmunizations(unittest.TestCase):
1212
def setUp(self):
13-
# self.controller = create_autospec(FhirController)
14-
self.logger_exception_patcher = patch("logging.Logger.exception")
15-
self.mock_logger_exception = self.logger_exception_patcher.start()
13+
self.controller = create_autospec(FhirController)
1614
# patch FhirController
1715
self.controller = create_autospec(FhirController)
18-
self.controller.update_immunization = FhirController.update_immunization
16+
self.controller.update_immunization = FhirController.update_immunization
17+
self.logger_exception_patcher = patch("logging.Logger.exception")
18+
self.mock_logger_exception = self.logger_exception_patcher.start()
19+
self.logger_info_patcher = patch("logging.Logger.info")
20+
self.mock_logger_info = self.logger_info_patcher.start()
21+
22+
1923

2024

2125

0 commit comments

Comments
 (0)