33import datetime
44import unittest
55from copy import deepcopy
6- from unittest .mock import create_autospec
6+ from unittest .mock import create_autospec , patch
77from decimal import Decimal
88
99from fhir .resources .R4B .bundle import Bundle as FhirBundle , BundleEntry
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"
2845class 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 )
0 commit comments