Skip to content

Commit 4f756cc

Browse files
committed
remock test imm post valid
1 parent cbdd8be commit 4f756cc

File tree

2 files changed

+81
-7
lines changed

2 files changed

+81
-7
lines changed

backend/tests/test_immunization_post_validator.py

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from copy import deepcopy
77
from pydantic import ValidationError
88
from jsonpath_ng.ext import parse
9-
from utils.mock_redis import mock_redis_hget
109

1110

1211
from models.fhir_immunization import ImmunizationValidator
@@ -18,7 +17,8 @@
1817
from tests.utils.mandation_test_utils import MandationTests
1918
from tests.utils.values_for_tests import NameInstances
2019
from 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

2323
class 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 = [

backend/tests/utils/mock_redis.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,39 @@
2525
"FLU": "[{\"code\": \"6142004\", \"term\": \"Influenza caused by seasonal influenza virus (disorder)\"}]",
2626
"MENACWY": "[{\"code\": \"23511006\", \"term\": \"Meningococcal infectious disease\"}]"
2727
}
28+
last = None
29+
def get_data(name):
30+
# extract name of calling unit test and print to console
31+
import inspect
32+
# static variable to keep track of last printed message
33+
global last
34+
# get the name of the calling unit test
35+
# this is useful for debugging and understanding which test is calling this mock
36+
# function, especially when multiple tests are using the same mock
37+
test_name = None
38+
for frame_info in inspect.stack():
39+
if frame_info.function.startswith("test_"):
40+
test_name = frame_info.function
41+
# Optionally, get the class name if available
42+
test_self = frame_info.frame.f_locals.get("self", None)
43+
msg = ''
44+
if test_self:
45+
test_class = type(test_self).__name__
46+
msg = f"Mocking redis call for: {test_class} - {test_name}"
47+
else:
48+
msg = f"Mocking redis call for: {test_name}"
2849

50+
if msg != last:
51+
print(msg)
52+
last = msg
2953

30-
def get_data(name):
54+
break
55+
else:
56+
print("Mocking redis call: test function not found in stack.")
57+
58+
59+
60+
3161
if name == "diseases_to_vacc":
3262
return MOCK_REDIS_D2V_RESPONSE
3363
elif name == "vacc_to_diseases":
@@ -37,7 +67,10 @@ def get_data(name):
3767
def mock_redis_hget(name, key):
3868
ret = get_data(name)
3969
if key in ret:
70+
print(f"Mocking redis hget({name},{key}): {ret[key]}")
4071
return ret[key]
72+
73+
print(f"Mocking redis hget({name},{key}): None")
4174
return {}
4275

4376
def mock_redis_hkeys(name):

0 commit comments

Comments
 (0)