Skip to content

Commit 3ff146b

Browse files
committed
67 fails
1 parent 16900c5 commit 3ff146b

File tree

8 files changed

+137
-18
lines changed

8 files changed

+137
-18
lines changed

backend/src/models/utils/validation_utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from models.errors import MandatoryError
1212
from constants import Urls
1313
from clients import redis_client
14+
from unittest.mock import Mock
1415

1516

1617
def get_target_disease_codes(immunization: dict):
@@ -53,7 +54,14 @@ def convert_disease_codes_to_vaccine_type(disease_codes_input: list) -> Union[st
5354
otherwise raises a value error
5455
"""
5556
key = ":".join(sorted(disease_codes_input))
56-
vaccine_type = redis_client.hget("diseases_to_vacc", key)
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")
64+
5765
if not vaccine_type:
5866
raise ValueError(
5967
f"Validation errors: protocolApplied[0].targetDisease[*].coding[?(@.system=='http://snomed.info/sct')].code - "
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import json
2+
MOCK_REDIS_D2V_RESPONSE = {
3+
"4740000": "SHINGLES",
4+
"6142004": "FLU",
5+
"16814004": "PCV13",
6+
"23511006": "MENACWY",
7+
"27836007": "PERTUSSIS",
8+
"55735004": "RSV",
9+
"240532009": "HPV",
10+
"840539006": "COVID19",
11+
"14189004:36653000:36989005": "MMR",
12+
"14189004:36653000:36989005:38907003": "MMRV",
13+
"397430003:398102009:76902006": "3in1"
14+
}
15+
16+
MOCK_REDIS_V2D_RESPONSE = {
17+
"PERTUSSIS": "[{\"code\": \"27836007\", \"term\": \"Pertussis (disorder)\"}]",
18+
"RSV": "[{\"code\": \"55735004\", \"term\": \"Respiratory syncytial virus infection (disorder)\"}]",
19+
"3in1": "[{\"code\": \"398102009\", \"term\": \"Acute poliomyelitis\"}, {\"code\": \"397430003\", \"term\": \"Diphtheria caused by Corynebacterium diphtheriae\"}, {\"code\": \"76902006\", \"term\": \"Tetanus (disorder)\"}]",
20+
"MMR": "[{\"code\": \"14189004\", \"term\": \"Measles (disorder)\"}, {\"code\": \"36989005\", \"term\": \"Mumps (disorder)\"}, {\"code\": \"36653000\", \"term\": \"Rubella (disorder)\"}]",
21+
"HPV": "[{\"code\": \"240532009\", \"term\": \"Human papillomavirus infection\"}]",
22+
"MMRV": "[{\"code\": \"14189004\", \"term\": \"Measles (disorder)\"}, {\"code\": \"36989005\", \"term\": \"Mumps (disorder)\"}, {\"code\": \"36653000\", \"term\": \"Rubella (disorder)\"}, {\"code\": \"38907003\", \"term\": \"Varicella (disorder)\"}]",
23+
"PCV13": "[{\"code\": \"16814004\", \"term\": \"Pneumococcal infectious disease\"}]",
24+
"SHINGLES": "[{\"code\": \"4740000\", \"term\": \"Herpes zoster\"}]",
25+
"COVID19": "[{\"code\": \"840539006\", \"term\": \"Disease caused by severe acute respiratory syndrome coronavirus 2\"}]",
26+
"FLU": "[{\"code\": \"6142004\", \"term\": \"Influenza caused by seasonal influenza virus (disorder)\"}]",
27+
"MENACWY": "[{\"code\": \"23511006\", \"term\": \"Meningococcal infectious disease\"}]"
28+
}
29+
30+
def fake_hget(name, key):
31+
# Custom logic for your test
32+
if name == "diseases_to_vacc":
33+
if key in MOCK_REDIS_D2V_RESPONSE:
34+
return MOCK_REDIS_D2V_RESPONSE[key]
35+
elif name == "vacc_to_diseases":
36+
if key in MOCK_REDIS_V2D_RESPONSE:
37+
return MOCK_REDIS_V2D_RESPONSE[key]
38+
return None

backend/tests/test_fhir_batch_repository.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def modify_immunization(self, remove_nhs):
4141

4242
def create_immunization_test_logic(self, is_present, remove_nhs):
4343
"""Common logic for testing immunization creation."""
44+
print("create_immunization_test_logic...")
45+
print(f"Is present: {is_present}, Remove NHS: {remove_nhs}")
4446
self.modify_immunization(remove_nhs)
4547

4648
self.repository.create_immunization(
@@ -65,18 +67,18 @@ def create_immunization_test_logic(self, is_present, remove_nhs):
6567

6668
def test_create_immunization_with_nhs_number(self):
6769
"""Test creating Immunization with NHS number."""
68-
70+
print("test_create_immunization_with_nhs_number...")
6971
self.create_immunization_test_logic(is_present=True, remove_nhs=False)
7072

7173
def test_create_immunization_without_nhs_number(self):
7274
"""Test creating Immunization without NHS number."""
73-
75+
print("test_create_immunization_without_nhs_number...")
7476
self.create_immunization_test_logic(is_present=False, remove_nhs=True)
7577

7678

7779
def test_create_immunization_duplicate(self):
7880
"""it should not create Immunization since the request is duplicate"""
79-
81+
print("test_create_immunization_duplicate...")
8082
self.table.query = MagicMock(return_value={
8183
"id": imms_id,
8284
"identifier": [{"system": "test-system", "value": "12345"}],

backend/tests/test_forwarding_batch_lambda.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import copy
1818
import json
1919

20-
from tests.utils.mock_redis import MockRedisClient
2120
from tests.utils.test_utils_for_batch import ForwarderValues, MockFhirImmsResources
2221

2322

@@ -29,6 +28,21 @@
2928
@patch.dict(os.environ, ForwarderValues.MOCK_ENVIRONMENT_DICT)
3029
class TestForwardLambdaHandler(TestCase):
3130

31+
MOCK_REDIS_D2V_RESPONSE = json.dumps({
32+
"4740000": "SHINGLES",
33+
"6142004": "FLU",
34+
"16814004": "PCV13",
35+
"23511006": "MENACWY",
36+
"27836007": "PERTUSSIS",
37+
"55735004": "RSV",
38+
"240532009": "HPV",
39+
"840539006": "COVID19",
40+
"14189004:36653000:36989005": "MMR",
41+
"14189004:36653000:36989005:38907003": "MMRV",
42+
"397430003:398102009:76902006": "3in1"
43+
})
44+
45+
3246
def setUp(self):
3347
"""Set up dynamodb table test values to be used for the tests"""
3448
self.dynamodb_resource = boto3_resource("dynamodb", "eu-west-2")
@@ -60,6 +74,13 @@ def setUp(self):
6074
},
6175
],
6276
)
77+
self.redis_patcher = patch("models.utils.validation_utils.redis_client")
78+
self.mock_redis_client = self.redis_patcher.start()
79+
self.mock_redis_client.hget.return_value = self.MOCK_REDIS_D2V_RESPONSE
80+
81+
def tearDown(self):
82+
"""Tear down after each test. This runs after every test"""
83+
self.redis_patcher.stop()
6384

6485
@staticmethod
6586
def generate_fhir_json(include_fhir_json=True, identifier_value=None):
@@ -182,6 +203,7 @@ def test_forward_lambda_handler_single_operations(self, mock_send_message):
182203
"PatientSK": "RSV#4d2ac1eb-080f-4e54-9598-f2d53334681c",
183204
}
184205
)
206+
self.mock_redis_client.hget.return_value = self.MOCK_REDIS_D2V_RESPONSE
185207

186208
test_cases = [
187209
{

backend/tests/test_immunization_post_validator.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Test immunization pre validation rules on the model"""
22

33
import unittest
4+
from unittest.mock import patch
5+
import json
46
from copy import deepcopy
57
from pydantic import ValidationError
68
from jsonpath_ng.ext import parse
@@ -15,7 +17,7 @@
1517
from tests.utils.mandation_test_utils import MandationTests
1618
from tests.utils.values_for_tests import NameInstances
1719
from tests.utils.generic_utils import update_contained_resource_field
18-
20+
from sample_data.mock_redis_cache import MOCK_REDIS_D2V_RESPONSE, MOCK_REDIS_V2D_RESPONSE
1921

2022
class TestImmunizationModelPostValidationRules(unittest.TestCase):
2123
"""Test immunization post validation rules on the FHIR model"""
@@ -37,6 +39,8 @@ def setUp(self):
3739
"MMR",
3840
"RSV",
3941
]
42+
self.redis_patcher = patch("models.utils.validation_utils.redis_client")
43+
self.mock_redis_client = self.redis_patcher.start()
4044

4145
def test_collected_errors(self):
4246
"""Test that when passed multiple validation errors, it returns a list of all expected errors"""
@@ -465,13 +469,16 @@ def test_post_organization_identifier_system(self):
465469
self, "performer[?(@.actor.type=='Organization')].actor.identifier.system"
466470
)
467471

468-
def test_pre_validate_extension_url(self):
472+
# TODO confirm if this is correct. Why pre-validate in post validator tests?
473+
def test_post_pre_validate_extension_url(self):
469474
"""
470475
Test pre_validate_extension_url accepts valid values and rejects
471476
if the snomed code are unable to fetch if the url is invalid
472477
and get passed only with the snomed url.
473478
"""
474479
# Test case: missing "extension"
480+
self.mock_redis_client.hget.return_value = MOCK_REDIS_D2V_RESPONSE
481+
475482
invalid_json_data = deepcopy(self.completed_json_data["COVID19"])
476483
invalid_json_data["extension"][0]["valueCodeableConcept"]["coding"][0]["system"]='https://xyz/Extension-UKCore-VaccinationProcedure'
477484

backend/tests/test_immunization_pre_validator.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from copy import deepcopy
55
from decimal import Decimal
66
from unittest import mock
7+
import json
78
from unittest.mock import patch
89

910
from jsonpath_ng.ext import parse
@@ -23,7 +24,6 @@
2324
practitioner_name_given_field_location,
2425
practitioner_name_family_field_location,
2526
)
26-
from .utils.mock_redis import mock_redis, MockRedisClient
2727
from .utils.pre_validation_test_utils import ValidatorModelTests
2828
from .utils.values_for_tests import ValidValues, InvalidValues
2929
from models.obtain_field_value import ObtainFieldValue
@@ -32,13 +32,35 @@
3232
class 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

backend/tests/test_utils.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
"""Tests for generic utils"""
22

33
import unittest
4+
import json
5+
from unittest.mock import patch, MagicMock
46
from copy import deepcopy
7+
from sample_data.mock_redis_cache import fake_hget
8+
9+
from models.utils.validation_utils import convert_disease_codes_to_vaccine_type, get_vaccine_type
10+
from utils.generic_utils import load_json_data, update_target_disease_code
511

6-
from src.models.utils.validation_utils import convert_disease_codes_to_vaccine_type, get_vaccine_type
7-
from .utils.generic_utils import load_json_data, update_target_disease_code
8-
"test"
912

1013
class TestGenericUtils(unittest.TestCase):
1114
"""Tests for generic utils functions"""
12-
1315
def setUp(self):
1416
"""Set up for each test. This runs before every test"""
1517
self.json_data = load_json_data(filename="completed_mmr_immunization_event.json")
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+
22+
def tearDown(self):
23+
"""Tear down after each test. This runs after every test"""
24+
self.redis_patcher.stop()
1625

1726
def test_convert_disease_codes_to_vaccine_type(self):
1827
"""
@@ -52,8 +61,9 @@ def test_get_vaccine_type(self):
5261
"""
5362
# TEST VALID DATA
5463
valid_json_data = load_json_data(filename=f"completed_rsv_immunization_event.json")
55-
print(valid_json_data)
56-
self.assertEqual(get_vaccine_type(valid_json_data), "RSV")
64+
# print(valid_json_data)
65+
vac_type = get_vaccine_type(valid_json_data)
66+
self.assertEqual(vac_type, "RSV")
5767

5868
# VALID DATA: coding field with multiple coding systems including SNOMED
5969
flu_json_data = load_json_data(filename=f"completed_flu_immunization_event.json")

backend/tests/test_validation_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
obtain_name_field_location,
1313
)
1414

15-
from src.models.fhir_immunization import ImmunizationValidator
16-
from .utils.generic_utils import (
15+
from models.fhir_immunization import ImmunizationValidator
16+
from utils.generic_utils import (
1717
# these have an underscore to avoid pytest collecting them as tests
1818
load_json_data,
1919
)
20-
from .utils.values_for_tests import ValidValues, InvalidValues, NameInstances
20+
from utils.values_for_tests import ValidValues, InvalidValues, NameInstances
2121

2222

2323
class TestValidatorUtils(unittest.TestCase):

0 commit comments

Comments
 (0)