Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_send_request_to_dynamo_create_badrequest(self):
imms_pk = _make_immunization_pk(imms_id)
imms = create_covid_immunization(imms_id)
create_result = CustomValidationError(
message="Validation errors: contained[?(@.resourceType=='Patient')].identifier[0].value does not exists"
message="Validation errors: contained[?(@.resourceType=='Patient')].identifier[?(@.system=='https://fhir.nhs.uk/Id/nhs-number')].value does not exists"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be better to put this in a function, so its use is clear and it can be re-used?


message_body = {
Expand Down Expand Up @@ -178,7 +178,7 @@ def test_send_request_to_dynamo_update_badrequest(self):
imms_pk = _make_immunization_pk(imms_id)
imms = create_covid_immunization(imms_id)
update_result = CustomValidationError(
message="Validation errors: contained[?(@.resourceType=='Patient')].identifier[0].value does not exists"
message="Validation errors: contained[?(@.resourceType=='Patient')].identifier[?(@.system=='https://fhir.nhs.uk/Id/nhs-number')].value does not exists"
)
message_body = {
"supplier": "test_supplier",
Expand Down Expand Up @@ -297,7 +297,7 @@ def test_send_request_to_dynamo_delete_badrequest(self):
imms_pk = _make_immunization_pk(imms_id)
imms = create_covid_immunization(imms_id)
update_result = CustomValidationError(
message="Validation errors: contained[?(@.resourceType=='Patient')].identifier[0].value does not exists"
message="Validation errors: contained[?(@.resourceType=='Patient')].identifier[?(@.system=='https://fhir.nhs.uk/Id/nhs-number')].value does not exists"
)
message_body = {
"supplier": "test_supplier",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def validate(self):
self.pre_validate_practitioner_reference,
self.pre_validate_patient_identifier_extension,
self.pre_validate_patient_identifier,
self.pre_validate_patient_identifier_system,
self.pre_validate_patient_identifier_value,
self.pre_validate_patient_name,
self.pre_validate_patient_name_given,
Expand Down Expand Up @@ -271,6 +272,21 @@ def pre_validate_patient_identifier(self, values: dict) -> None:
except (KeyError, IndexError):
pass

def pre_validate_patient_identifier_system(self, values: dict) -> None:
"""
Pre-validate that, if contained[?(@.resourceType=='Patient')].identifier[0].system (
legacy CSV field name: NHS_NUMBER) exists, then it is 'https://fhir.nhs.uk/Id/nhs-number'
"""
field_location = "contained[?(@.resourceType=='Patient')].identifier[0].system"
predefined_values = [Urls.NHS_NUMBER]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre‑validators still enforce that any Patient identifier if present must use the NHS number.

try:
field_value = [x for x in values["contained"] if x.get("resourceType") == "Patient"][0]["identifier"][0][
"system"
]
PreValidation.for_string(field_value, field_location, predefined_values=predefined_values)
except (KeyError, IndexError):
pass

def pre_validate_patient_identifier_value(self, values: dict) -> None:
"""
Pre-validate that, if contained[?(@.resourceType=='Patient')].identifier[0].value (
Expand Down
2 changes: 1 addition & 1 deletion lambdas/shared/src/common/models/field_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FieldLocations:
target_disease = "protocolApplied[0].targetDisease"
target_disease_codes = f"protocolApplied[0].targetDisease[0].coding[?(@.system=='{Urls.SNOMED}')].code"
patient_identifier_value = (
"contained[?(@.resourceType=='Patient')].identifier[0].value" # TODO: Fix to use nhs number url lookup
f"contained[?(@.resourceType=='Patient')].identifier[?(@.system=='{Urls.NHS_NUMBER}')].value"
)
patient_name_given: str = field(init=False)
patient_name_family: str = field(init=False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ def test_post_patient_identifier_value(self):
"""
Test that the JSON data is accepted when it does not contain patient_identifier_value
"""
field_location = "contained[?(@.resourceType=='Patient')].identifier[0].value"
field_location = (
"contained[?(@.resourceType=='Patient')].identifier[?(@.system=='https://fhir.nhs.uk/Id/nhs-number')].value"
)
self.mock_redis.hget.return_value = "COVID"
self.mock_redis_getter.return_value = self.mock_redis
MandationTests.test_missing_field_accepted(self, field_location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,16 @@ def test_pre_validate_patient_identifier_extension(self):
expected_error_message="contained[?(@.resourceType=='Patient')].identifier[0] must not include an extension",
)

def test_pre_validate_patient_identifier_system(self):
"""Test pre_validate_patient_identifier_system accepts valid values and rejects invalid values"""
ValidatorModelTests.test_string_value(
self,
field_location="contained[?(@.resourceType=='Patient')].identifier[0].system",
predefined_values=["https://fhir.nhs.uk/Id/nhs-number"],
valid_strings_to_test=["https://fhir.nhs.uk/Id/nhs-number"],
invalid_strings_to_test=["https://google.com/nhs", "Not-a-URI"],
)

def test_pre_validate_patient_identifier_value(self):
"""Test pre_validate_patient_identifier_value accepts valid values and rejects invalid values"""
ValidatorModelTests.test_string_value(
Expand Down