44from copy import deepcopy
55from decimal import Decimal
66from unittest import mock
7+ import json
78from unittest .mock import patch
89
910from jsonpath_ng .ext import parse
2324 practitioner_name_given_field_location ,
2425 practitioner_name_family_field_location ,
2526)
26- from .utils .mock_redis import mock_redis , MockRedisClient
2727from .utils .pre_validation_test_utils import ValidatorModelTests
2828from .utils .values_for_tests import ValidValues , InvalidValues
2929from models .obtain_field_value import ObtainFieldValue
3232class TestImmunizationModelPreValidationRules (unittest .TestCase ):
3333 """Test immunization pre validation rules on the FHIR model using the covid sample data"""
3434
35+ MOCK_REDIS_D2V_RESPONSE = json .dumps ({
36+ "4740000" : "SHINGLES" ,
37+ "6142004" : "FLU" ,
38+ "16814004" : "PCV13" ,
39+ "23511006" : "MENACWY" ,
40+ "27836007" : "PERTUSSIS" ,
41+ "55735004" : "RSV" ,
42+ "240532009" : "HPV" ,
43+ "840539006" : "COVID19" ,
44+ "14189004:36653000:36989005" : "MMR" ,
45+ "14189004:36653000:36989005:38907003" : "MMRV" ,
46+ "397430003:398102009:76902006" : "3in1"
47+ })
48+
3549 def setUp (self ):
3650 """Set up for each test. This runs before every test"""
3751 self .json_data = load_json_data (filename = "completed_covid19_immunization_event.json" )
3852 self .validator = ImmunizationValidator (add_post_validators = False )
3953 # redis_patcher = mock.patch("redis.StrictRedis", return_value=MockRedisClient())
4054 # self.addCleanup(redis_patcher.stop)
4155 # redis_patcher.start()
56+ # patch clients.redis_client
57+ # @patch('models.utils.validation_utils.redis_client.hget')
58+ self .redis_patcher = patch ("models.utils.validation_utils.redis_client" )
59+ self .mock_redis_client = self .redis_patcher .start ()
60+
61+
62+ def tearDown (self ):
63+ patch .stopall ()
4264
4365 def test_collected_errors (self ):
4466 """Test that when passed multiple validation errors, it returns a list of all expected errors."""
@@ -671,6 +693,8 @@ def test_pre_validate_extension(self):
671693 actual_error_messages = full_error_message .replace ("Validation errors: " , "" ).split ("; " )
672694 self .assertIn ("extension is a mandatory field" , actual_error_messages )
673695
696+ def test_pre_validate_missing_valueCodeableConcept (self ):
697+ """Test pre_validate_extension missing "valueCodeableConcept" within an extension"""
674698 # Test case: missing "valueCodeableConcept" within an extension
675699 invalid_json_data = deepcopy (self .json_data )
676700 del invalid_json_data ["extension" ][0 ]["valueCodeableConcept" ]
@@ -682,6 +706,7 @@ def test_pre_validate_extension(self):
682706 actual_error_messages = full_error_message .replace ("Validation errors: " , "" ).split ("; " )
683707 self .assertIn ("extension[?(@.url=='https://fhir.hl7.org.uk/StructureDefinition/Extension-UKCore-VaccinationProcedure')].valueCodeableConcept is a mandatory field" , actual_error_messages )
684708
709+ def test_pre_validate_missing_valueCodeableConcept2 (self ):
685710 # Test case: missing "coding" within "valueCodeableConcept"
686711 invalid_json_data = deepcopy (self .json_data )
687712 del invalid_json_data ["extension" ][0 ]["valueCodeableConcept" ]["coding" ]
@@ -693,12 +718,15 @@ def test_pre_validate_extension(self):
693718 actual_error_messages = full_error_message .replace ("Validation errors: " , "" ).split ("; " )
694719 self .assertIn ("extension[?(@.url=='https://fhir.hl7.org.uk/StructureDefinition/Extension-UKCore-VaccinationProcedure')].valueCodeableConcept.coding is a mandatory field" , actual_error_messages )
695720
721+ def test_pre_validate_missing_valueCodeableConcept3 (self ):
696722 # Test case: valid data (should not raise an exception)
723+ self .mock_redis_client .hget .return_value = self .MOCK_REDIS_D2V_RESPONSE
697724 valid_json_data = deepcopy (self .json_data )
698725 try :
699726 self .validator .validate (valid_json_data )
700727 except Exception as error :
701728 self .fail (f"Validation unexpectedly raised an exception: { error } " )
729+ print ("Validation passed successfully with valid data." )
702730
703731
704732 def test_pre_validate_extension_length (self ):
@@ -725,7 +753,7 @@ def test_pre_validate_extension_length(self):
725753 actual_error_messages = full_error_message .replace ("Validation errors: " , "" ).split ("; " )
726754 self .assertIn ("extension must be an array of length 1" , actual_error_messages )
727755
728- def test_pre_validate_extension_url (self ):
756+ def test_pre_validate_extension_url1 (self ):
729757 """Test test_pre_validate_extension_url accepts valid values and rejects invalid values for extension[0].url"""
730758 # Test case: missing "extension"
731759 invalid_json_data = deepcopy (self .json_data )
@@ -737,6 +765,7 @@ def test_pre_validate_extension_url(self):
737765 full_error_message = str (error .exception )
738766 actual_error_messages = full_error_message .replace ("Validation errors: " , "" ).split ("; " )
739767 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" )
740769
741770 def test_pre_validate_extension_snomed_code (self ):
742771 """Test test_pre_validate_extension_url accepts valid values and rejects invalid values for extension[0].url"""
@@ -1096,6 +1125,7 @@ def test_pre_validate_site_coding_display(self):
10961125
10971126 def test_pre_validate_route_coding (self ):
10981127 """Test pre_validate_route_coding accepts valid values and rejects invalid values"""
1128+ print ("Testing pre_validate_route_coding" )
10991129 ValidatorModelTests .test_unique_list (
11001130 self ,
11011131 field_location = "route.coding" ,
@@ -1109,11 +1139,13 @@ def test_pre_validate_route_coding(self):
11091139
11101140 def test_pre_validate_route_coding_code (self ):
11111141 """Test pre_validate_route_coding_code accepts valid values and rejects invalid values"""
1142+ print ("Testing pre_validate_route_coding_code" )
11121143 field_location = "route.coding[?(@.system=='http://snomed.info/sct')].code"
11131144 ValidatorModelTests .test_string_value (self , field_location , valid_strings_to_test = ["dummy" ])
11141145
11151146 def test_pre_validate_route_coding_display (self ):
11161147 """Test pre_validate_route_coding_display accepts valid values and rejects invalid values"""
1148+ print ("Testing pre_validate_route_coding_display" )
11171149 field_location = "route.coding[?(@.system=='http://snomed.info/sct')].display"
11181150 ValidatorModelTests .test_string_value (self , field_location , valid_strings_to_test = ["dummy" ])
11191151
0 commit comments