66from copy import deepcopy
77from pydantic import ValidationError
88from jsonpath_ng .ext import parse
9- from utils .mock_redis import mock_redis_hget
109
1110
1211from models .fhir_immunization import ImmunizationValidator
1817from tests .utils .mandation_test_utils import MandationTests
1918from tests .utils .values_for_tests import NameInstances
2019from tests .utils .generic_utils import update_contained_resource_field
21- from utils .mock_redis import MOCK_REDIS_D2V_RESPONSE , MOCK_REDIS_V2D_RESPONSE
20+ # import redis mock functions
21+ from utils .mock_redis import mock_redis_hget , mock_redis_hkeys
2222
2323class TestImmunizationModelPostValidationRules (unittest .TestCase ):
2424 """Test immunization post validation rules on the FHIR model"""
@@ -42,13 +42,16 @@ def setUp(self):
4242 ]
4343 self .redis_patcher = patch ("models.utils.validation_utils.redis_client" )
4444 self .mock_redis_client = self .redis_patcher .start ()
45- self .mock_redis_client .hget .side_effect = mock_redis_hget
45+
46+ def tearDown (self ):
47+ """Tear down after each test. This runs after every test"""
48+ self .redis_patcher .stop ()
4649
4750 def test_collected_errors (self ):
4851 """Test that when passed multiple validation errors, it returns a list of all expected errors"""
4952
5053 covid_19_json_data = deepcopy (self .completed_json_data ["COVID19" ])
51-
54+ self . mock_redis_client . hget . return_value = 'COVID19'
5255 for patient in covid_19_json_data ["contained" ]:
5356 if patient ["resourceType" ] == "Patient" :
5457 for name in patient ["name" ]:
@@ -80,6 +83,7 @@ def test_collected_errors(self):
8083
8184 def test_sample_data (self ):
8285 """Test that each piece of valid sample data passes post validation"""
86+ self .mock_redis_client .hget .return_value = "COVID19"
8387 for json_data in list (self .completed_json_data .values ()):
8488 self .assertIsNone (self .validator .validate (json_data ))
8589
@@ -95,6 +99,11 @@ def test_post_validate_and_set_vaccine_type(self):
9599 "protocolApplied[0].targetDisease[0].coding[?(@.system=='http://snomed.info/sct')].code"
96100 )
97101
102+ # self.mock_redis_client.hget.side_effect = mock_redis_hget
103+ self .mock_redis_client .hget .side_effect = [ "COVID19" , "FLU" ,
104+ "HPV" ,
105+ "MMR" ,
106+ "RSV" , None ]
98107 # Test that a valid combination of disease codes is accepted
99108 for vaccine_type in [
100109 "COVID19" ,
@@ -115,6 +124,8 @@ def test_post_validate_and_set_vaccine_type(self):
115124 + " - ['INVALID_VALUE'] is not a valid combination of disease codes for this service" ,
116125 )
117126
127+ self .mock_redis_client .hget .side_effect = None
128+ self .mock_redis_client .hget .return_value = None
118129 # Test that an invalid combination of disease codes is rejected
119130 invalid_target_disease = [
120131 {"coding" : [{"system" : "http://snomed.info/sct" , "code" : "14189004" , "display" : "Measles" }]},
@@ -138,6 +149,7 @@ def test_post_validate_and_set_vaccine_type(self):
138149
139150 def test_post_vaccination_procedure_code (self ):
140151 """Test that the JSON data is rejected if it does not contain vaccination_procedure_code"""
152+ self .mock_redis_client .hget .return_value = "COVID19"
141153 field_location = (
142154 "extension[?(@.url=='https://fhir.hl7.org.uk/StructureDefinition/Extension-UKCore-VaccinationProcedure')]"
143155 + ".valueCodeableConcept.coding[?(@.system=='http://snomed.info/sct')].code"
@@ -160,10 +172,12 @@ def test_post_patient_identifier_value(self):
160172 Test that the JSON data is accepted when it does not contain patient_identifier_value
161173 """
162174 field_location = "contained[?(@.resourceType=='Patient')].identifier[0].value"
175+ self .mock_redis_client .hget .return_value = "COVID19"
163176 MandationTests .test_missing_field_accepted (self , field_location )
164177
165178 def test_post_patient_name_given (self ):
166179 """Test that the JSON data is rejected if it does not contain patient_name_given"""
180+ self .mock_redis_client .hget .return_value = "RSV"
167181 valid_json_data = deepcopy (self .completed_json_data ["RSV" ])
168182 patient_name_given_field_location = "contained[?(@.resourceType=='Patient')].name[0].given"
169183 expected_error_message = f"Validation errors: { patient_name_given_field_location } is a mandatory field"
@@ -206,6 +220,7 @@ def test_post_patient_name_given(self):
206220 def test_post_patient_name_family (self ):
207221 """Test that the JSON data is rejected if it does not contain patient_name_family"""
208222 valid_json_data = deepcopy (self .completed_json_data ["RSV" ])
223+ self .mock_redis_client .hget .return_value = "COVID19"
209224 patient_name_family_field_location = "contained[?(@.resourceType=='Patient')].name[0].family"
210225 expected_error_message = f"{ patient_name_family_field_location } is a mandatory field"
211226
@@ -236,14 +251,17 @@ def test_post_patient_name_family(self):
236251
237252 def test_post_patient_birth_date (self ):
238253 """Test that the JSON data is rejected if it does not contain patient_birth_date"""
254+ self .mock_redis_client .hget .return_value = "COVID19"
239255 MandationTests .test_missing_mandatory_field_rejected (self , "contained[?(@.resourceType=='Patient')].birthDate" )
240256
241257 def test_post_patient_gender (self ):
242258 """Test that the JSON data is rejected if it does not contain patient_gender"""
259+ self .mock_redis_client .hget .return_value = "COVID19"
243260 MandationTests .test_missing_mandatory_field_rejected (self , "contained[?(@.resourceType=='Patient')].gender" )
244261
245262 def test_post_patient_address_postal_code (self ):
246263 """Test that the JSON data is rejected if it does not contain patient_address_postal_code"""
264+ self .mock_redis_client .hget .return_value = "COVID19"
247265 field_location = "contained[?(@.resourceType=='Patient')].address[0].postalCode"
248266 MandationTests .test_missing_mandatory_field_rejected (self , field_location )
249267
@@ -260,20 +278,24 @@ def test_post_occurrence_date_time(self):
260278
261279 def test_post_organization_identifier_value (self ):
262280 """Test that the JSON data is rejected if it does not contain organization_identifier_value"""
281+ self .mock_redis_client .hget .return_value = "COVID19"
263282 MandationTests .test_missing_mandatory_field_rejected (
264283 self , "performer[?(@.actor.type=='Organization')].actor.identifier.value"
265284 )
266285
267286 def test_post_identifer_value (self ):
268287 """Test that the JSON data is rejected if it does not contain identifier_value"""
288+ self .mock_redis_client .hget .return_value = "COVID19"
269289 MandationTests .test_missing_mandatory_field_rejected (self , "identifier[0].value" )
270290
271291 def test_post_identifer_system (self ):
272292 """Test that the JSON data is rejected if it does not contain identifier_system"""
293+ self .mock_redis_client .hget .return_value = "COVID19"
273294 MandationTests .test_missing_mandatory_field_rejected (self , "identifier[0].system" )
274295
275296 def test_post_practitioner_name_given (self ):
276297 """Test that the JSON data is rejected if it does not contain practitioner_name_given"""
298+ self .mock_redis_client .hget .return_value = "RSV"
277299 valid_json_data = deepcopy (self .completed_json_data ["RSV" ])
278300 practitioner_name_given_field_location = "contained[?(@.resourceType=='Practitioner')].name[0].given"
279301
@@ -297,6 +319,7 @@ def test_post_practitioner_name_given(self):
297319
298320 def test_post_practitioner_name_family (self ):
299321 """Test that the JSON data is rejected if it does not contain practitioner_name_family"""
322+ self .mock_redis_client .hget .return_value = "RSV"
300323 valid_json_data = deepcopy (self .completed_json_data ["RSV" ])
301324 practitioner_name_family_field_location = "contained[?(@.resourceType=='Practitioner')].name[0].family"
302325
@@ -320,10 +343,12 @@ def test_post_practitioner_name_family(self):
320343
321344 def test_post_recorded (self ):
322345 """Test that the JSON data is rejected if it does not contain recorded"""
346+ self .mock_redis_client .hget .return_value = "COVID19"
323347 MandationTests .test_missing_mandatory_field_rejected (self , "recorded" )
324348
325349 def test_post_primary_source (self ):
326350 """Test that the JSON data is rejected if it does not contain primary_source"""
351+ self .mock_redis_client .hget .return_value = "COVID19"
327352 MandationTests .test_missing_mandatory_field_rejected (self , "primarySource" )
328353
329354 # TODO: To confirm with imms if dose number string validation is correct (current working assumption is yes)
@@ -365,6 +390,8 @@ def test_post_dose_number_positive_int(self):
365390
366391 # dose_number_positive_int exists, dose_number_string does not exist
367392 valid_json_data = deepcopy (self .completed_json_data [vaccine_type ])
393+ self .mock_redis_client .hget .side_effect = None
394+ self .mock_redis_client .hget .return_value = 'COVID19'
368395 MandationTests .test_present_field_accepted (self , valid_json_data )
369396
370397 # dose_number_positive_int does not exist, dose_number_string exists
@@ -390,12 +417,15 @@ def test_post_manufacturer_display(self):
390417 Test that present or absent manufacturer_display is accepted or rejected
391418 as appropriate dependent on other fields
392419 """
420+ self .mock_redis_client .hget .side_effect = None
421+ self .mock_redis_client .hget .return_value = "COVID19"
393422 field_location = "manufacturer.display"
394423 for vaccine_type in self .all_vaccine_types :
395424 MandationTests .test_missing_field_accepted (self , field_location , self .completed_json_data [vaccine_type ])
396425
397426 def test_post_lot_number (self ):
398427 """Test that present or absent lot_number is accepted or rejected as appropriate dependent on other fields"""
428+ self .mock_redis_client .hget .side_effect = ['COVID19' , 'FLU' , 'HPV' , 'MMR' , 'RSV' ]
399429 field_location = "lotNumber"
400430 for vaccine_type in self .all_vaccine_types :
401431 MandationTests .test_missing_field_accepted (self , field_location , self .completed_json_data [vaccine_type ])
@@ -405,6 +435,7 @@ def test_post_expiration_date(self):
405435 Test that present or absent expiration_date is accepted or rejected
406436 as appropriate dependent on other fields
407437 """
438+ self .mock_redis_client .hget .side_effect = ['COVID19' , 'FLU' , 'HPV' , 'MMR' , 'RSV' ]
408439 field_location = "expirationDate"
409440 for vaccine_type in self .all_vaccine_types :
410441 MandationTests .test_missing_field_accepted (self , field_location , self .completed_json_data [vaccine_type ])
@@ -442,6 +473,7 @@ def test_post_dose_quantity_value(self):
442473 """
443474 Test that present or absent dose_quantity_value is accepted or rejected as appropriate dependent on other fields
444475 """
476+ self .mock_redis_client .hget .side_effect = ['COVID19' , 'FLU' , 'HPV' , 'MMR' , 'RSV' ]
445477 field_location = "doseQuantity.value"
446478 for vaccine_type in self .all_vaccine_types :
447479 MandationTests .test_missing_field_accepted (self , field_location , self .completed_json_data [vaccine_type ])
@@ -450,12 +482,15 @@ def test_post_dose_quantity_code(self):
450482 """
451483 Test that present or absent dose_quantity_code is accepted or rejected as appropriate dependent on other fields
452484 """
485+ self .mock_redis_client .hget .side_effect = ['COVID19' , 'FLU' , 'HPV' , 'MMR' , 'RSV' ]
453486 field_location = "doseQuantity.code"
454487 for vaccine_type in self .all_vaccine_types :
455488 MandationTests .test_missing_field_accepted (self , field_location , self .completed_json_data [vaccine_type ])
456489
457490 def test_post_dose_quantity_unit (self ):
458491 """Test that the JSON data is accepted when dose_quantity_unit is absent"""
492+ self .mock_redis_client .hget .side_effect = None
493+ self .mock_redis_client .hget .return_value = 'COVID19'
459494 MandationTests .test_missing_field_accepted (self , "doseQuantity.unit" )
460495
461496 # NOTE: THIS TEST IS COMMENTED OUT AS IT IS TESTING A REQUIRED ELEMENT (VALIDATION SHOULD ALWAYS PASS),
@@ -467,6 +502,8 @@ def test_post_dose_quantity_unit(self):
467502
468503 def test_post_organization_identifier_system (self ):
469504 """Test that the JSON data is rejected if it does not contain organization_identifier_system"""
505+ self .mock_redis_client .hget .side_effect = None
506+ self .mock_redis_client .hget .return_value = "COVID19"
470507 MandationTests .test_missing_mandatory_field_rejected (
471508 self , "performer[?(@.actor.type=='Organization')].actor.identifier.system"
472509 )
@@ -479,8 +516,8 @@ def test_post_pre_validate_extension_url(self):
479516 and get passed only with the snomed url.
480517 """
481518 # Test case: missing "extension"
482- self .mock_redis_client .hget .return_value = MOCK_REDIS_D2V_RESPONSE
483-
519+ self .mock_redis_client .hget .side_effect = None
520+ self . mock_redis_client . hget . return_value = "COVID19"
484521 invalid_json_data = deepcopy (self .completed_json_data ["COVID19" ])
485522 invalid_json_data ["extension" ][0 ]["valueCodeableConcept" ]["coding" ][0 ]["system" ]= 'https://xyz/Extension-UKCore-VaccinationProcedure'
486523
@@ -496,6 +533,7 @@ def test_post_location_identifier_value(self):
496533 Test that the JSON data is rejected if it does and does not contain
497534 location_identifier_value as appropriate
498535 """
536+ self .mock_redis_client .hget .side_effect = ['COVID19' , 'FLU' , 'HPV' , 'MMR' , 'RSV' ]
499537 field_location = "location.identifier.value"
500538 # Test cases for COVID-19, FLU, HPV and MMR where it is mandatory
501539 for vaccine_type in (
@@ -512,6 +550,7 @@ def test_post_location_identifier_system(self):
512550 """
513551 Test that the JSON data is rejected if it does and does not contain location_identifier_system as appropriate
514552 """
553+ self .mock_redis_client .hget .side_effect = ['COVID19' , 'FLU' , 'HPV' , 'MMR' , 'RSV' ]
515554 field_location = "location.identifier.system"
516555 # Test cases for COVID-19, FLU, HPV and MMR where it is mandatory
517556 for vaccine_type in (
@@ -526,6 +565,8 @@ def test_post_location_identifier_system(self):
526565
527566 def test_post_no_snomed_code (self ):
528567 """test that only snomed system is accepted"""
568+ self .mock_redis_client .hget .side_effect = None
569+ self .mock_redis_client .return_value = "COVID19"
529570 covid_19_json_data = deepcopy (self .completed_json_data ["COVID19" ])
530571
531572 invalid_target_disease_value = [
0 commit comments