Skip to content
Merged
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 @@ -384,6 +384,50 @@ def test_create_document_reference_invalid_body():
}


def test_create_document_reference_empty_fields_in_body():
doc_ref = load_document_reference("Y05868-736253002-Valid")
doc_ref.author = []
doc_ref.custodian = {"identifier": {}, "reference": None}
doc_ref.category = [{"coding": [{"system": "", "code": None}]}]
doc_ref.text = ""

event = create_test_api_gateway_event(
headers=create_headers(),
body=doc_ref.model_dump_json(exclude_none=True),
)
result = handler(event, create_mock_context())
body = result.pop("body")

assert result == {
"statusCode": "400",
"headers": default_response_headers(),
"isBase64Encoded": False,
}

parsed_body = json.loads(body)

assert parsed_body == {
"resourceType": "OperationOutcome",
"issue": [
{
"severity": "error",
"code": "invalid",
"details": {
"coding": [
{
"code": "MESSAGE_NOT_WELL_FORMED",
"display": "Message not well formed",
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
}
],
},
"diagnostics": "Request body could not be parsed (DocumentReference: Value error, The following fields are empty: text, author, custodian.reference, custodian.identifier, category[0].coding[0].system, category[0].coding[0].code)",
"expression": ["DocumentReference"],
}
],
}


def test_create_document_reference_invalid_resource():
doc_ref = load_document_reference("Y05868-736253002-Valid")
doc_ref.custodian = None
Expand Down Expand Up @@ -781,9 +825,7 @@ def test_create_document_reference_cannot_set_status_to_not_current(repository):
def test_create_document_reference_no_relatesto_target():
doc_ref = load_document_reference("Y05868-736253002-Valid")
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="transforms", target=Reference(reference=None, identifier=None)
)
DocumentReferenceRelatesTo(code="transforms", target=Reference())
]

event = create_test_api_gateway_event(
Expand All @@ -795,7 +837,7 @@ def test_create_document_reference_no_relatesto_target():
body = result.pop("body")

assert result == {
"statusCode": "422",
"statusCode": "400",
"headers": default_response_headers(),
"isBase64Encoded": False,
}
Expand All @@ -807,18 +849,18 @@ def test_create_document_reference_no_relatesto_target():
"issue": [
{
"severity": "error",
"code": "business-rule",
"code": "invalid",
"details": {
"coding": [
{
"code": "UNPROCESSABLE_ENTITY",
"display": "Unprocessable Entity",
"code": "MESSAGE_NOT_WELL_FORMED",
"display": "Message not well formed",
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
}
]
],
},
"diagnostics": "No identifier value provided for relatesTo target",
"expression": ["relatesTo[0].target.identifier.value"],
"diagnostics": "Request body could not be parsed (DocumentReference: Value error, The following fields are empty: relatesTo[0].target)",
"expression": ["DocumentReference"],
}
],
}
Expand All @@ -829,9 +871,7 @@ def test_create_document_reference_invalid_relatesto_target_producer_id():
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="transforms",
target=Reference(
reference=None, identifier=Identifier(value="X26-99999-99999-999999")
),
target=Reference(identifier=Identifier(value="X26-99999-99999-999999")),
)
]

Expand Down Expand Up @@ -881,7 +921,6 @@ def test_create_document_reference_invalid_relatesto_not_exists(repository):
DocumentReferenceRelatesTo(
code="transforms",
target=Reference(
reference=None,
identifier=Identifier(value="Y05868-123456-123456-123456"),
),
)
Expand Down Expand Up @@ -942,9 +981,7 @@ def test_create_document_reference_invalid_relatesto_nhs_number(
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="transforms",
target=Reference(
reference=None, identifier=Identifier(value="Y05868-99999-99999-999999")
),
target=Reference(identifier=Identifier(value="Y05868-99999-99999-999999")),
)
]

Expand Down Expand Up @@ -1004,9 +1041,7 @@ def test_create_document_reference_invalid_relatesto_type(
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="transforms",
target=Reference(
reference=None, identifier=Identifier(value="Y05868-99999-99999-999999")
),
target=Reference(identifier=Identifier(value="Y05868-99999-99999-999999")),
)
]

Expand Down Expand Up @@ -1220,9 +1255,7 @@ def test_create_document_reference_supersede_deletes_old_pointers_replace(
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="replaces",
target=Reference(
reference=None, identifier=Identifier(value="Y05868-99999-99999-999999")
),
target=Reference(identifier=Identifier(value="Y05868-99999-99999-999999")),
)
]

Expand Down Expand Up @@ -1281,9 +1314,7 @@ def test_create_document_reference_supersede_succeeds_with_toggle(
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="replaces",
target=Reference(
reference=None, identifier=Identifier(value="Y05868-99999-99999-000000")
),
target=Reference(identifier=Identifier(value="Y05868-99999-99999-000000")),
)
]

Expand Down Expand Up @@ -1341,9 +1372,7 @@ def test_create_document_reference_supersede_fails_without_toggle(
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="replaces",
target=Reference(
reference=None, identifier=Identifier(value="Y05868-99999-99999-000000")
),
target=Reference(identifier=Identifier(value="Y05868-99999-99999-000000")),
)
]

Expand Down Expand Up @@ -1400,9 +1429,7 @@ def test_create_document_reference_create_relatesto_not_replaces(
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="transforms",
target=Reference(
reference=None, identifier=Identifier(value="Y05868-99999-99999-999999")
),
target=Reference(identifier=Identifier(value="Y05868-99999-99999-999999")),
)
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_update_document_reference_happy_path(repository: DocumentPointerReposit
event = create_test_api_gateway_event(
headers=create_headers(),
path_parameters={"id": "Y05868-99999-99999-999999"},
body=doc_ref.model_dump_json(),
body=doc_ref.model_dump_json(exclude_none=True),
)

result = handler(event, create_mock_context())
Expand Down Expand Up @@ -109,7 +109,7 @@ def test_update_document_reference_happy_path_with_ssp(
event = create_test_api_gateway_event(
headers=create_headers(),
path_parameters={"id": "Y05868-99999-99999-999999"},
body=doc_ref.model_dump_json(),
body=doc_ref.model_dump_json(exclude_none=True),
)

result = handler(event, create_mock_context())
Expand Down Expand Up @@ -580,18 +580,13 @@ def test_update_document_reference_immutable_fields(repository):
repository.create(doc_pointer)

doc_ref.type = CodeableConcept(
id=None,
coding=[
Coding(
id=None,
system="http://snomed.info/sct",
version=None,
code="861421000000109",
display="End of life care coordination summary",
userSelected=None,
)
],
text=None,
)

event = create_test_api_gateway_event(
Expand Down Expand Up @@ -861,7 +856,7 @@ def test_update_document_reference_with_meta_lastupdated_ignored(
event = create_test_api_gateway_event(
headers=create_headers(),
path_parameters={"id": "Y05868-99999-99999-999999"},
body=doc_ref.model_dump_json(),
body=doc_ref.model_dump_json(exclude_none=True),
)

result = handler(event, create_mock_context())
Expand Down Expand Up @@ -931,7 +926,7 @@ def test_update_document_reference_with_invalid_date_ignored(
event = create_test_api_gateway_event(
headers=create_headers(),
path_parameters={"id": "Y05868-99999-99999-999999"},
body=doc_ref.model_dump_json(),
body=doc_ref.model_dump_json(exclude_none=True),
)

result = handler(event, create_mock_context())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -748,9 +748,7 @@ def test_upsert_document_reference_pointer_type_not_allowed(
def test_upsert_document_reference_no_relatesto_target():
doc_ref = load_document_reference("Y05868-736253002-Valid")
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="transforms", target=Reference(reference=None, identifier=None)
)
DocumentReferenceRelatesTo(code="transforms", target=Reference())
]

event = create_test_api_gateway_event(
Expand All @@ -762,7 +760,7 @@ def test_upsert_document_reference_no_relatesto_target():
body = result.pop("body")

assert result == {
"statusCode": "422",
"statusCode": "400",
"headers": default_response_headers(),
"isBase64Encoded": False,
}
Expand All @@ -774,18 +772,18 @@ def test_upsert_document_reference_no_relatesto_target():
"issue": [
{
"severity": "error",
"code": "business-rule",
"code": "invalid",
"details": {
"coding": [
{
"code": "UNPROCESSABLE_ENTITY",
"display": "Unprocessable Entity",
"code": "MESSAGE_NOT_WELL_FORMED",
"display": "Message not well formed",
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
}
]
],
},
"diagnostics": "No identifier value provided for relatesTo target",
"expression": ["relatesTo[0].target.identifier.value"],
"diagnostics": "Request body could not be parsed (DocumentReference: Value error, The following fields are empty: relatesTo[0].target)",
"expression": ["DocumentReference"],
}
],
}
Expand All @@ -796,9 +794,7 @@ def test_upsert_document_reference_invalid_relatesto_target_producer_id():
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="transforms",
target=Reference(
reference=None, identifier=Identifier(value="X26-99999-99999-999999")
),
target=Reference(identifier=Identifier(value="X26-99999-99999-999999")),
)
]

Expand Down Expand Up @@ -848,7 +844,6 @@ def test_upsert_document_reference_invalid_relatesto_not_exists(repository):
DocumentReferenceRelatesTo(
code="transforms",
target=Reference(
reference=None,
identifier=Identifier(value="Y05868-99999-99999-999999"),
),
)
Expand Down Expand Up @@ -902,7 +897,6 @@ def test_upsert_document_reference_invalid_relatesto_not_exists_still_creates_wi
DocumentReferenceRelatesTo(
code="transforms",
target=Reference(
reference=None,
identifier=Identifier(value="Y05868-99999-99999-999999"),
),
)
Expand Down Expand Up @@ -967,9 +961,7 @@ def test_upsert_document_reference_invalid_relatesto_nhs_number(
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="transforms",
target=Reference(
reference=None, identifier=Identifier(value="Y05868-99999-99999-999999")
),
target=Reference(identifier=Identifier(value="Y05868-99999-99999-999999")),
)
]

Expand Down Expand Up @@ -1029,9 +1021,7 @@ def test_upsert_document_reference_invalid_relatesto_type(
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="transforms",
target=Reference(
reference=None, identifier=Identifier(value="Y05868-99999-99999-999999")
),
target=Reference(identifier=Identifier(value="Y05868-99999-99999-999999")),
)
]

Expand Down Expand Up @@ -1244,9 +1234,7 @@ def test_upsert_document_reference_supersede_deletes_old_pointers_replace(
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="replaces",
target=Reference(
reference=None, identifier=Identifier(value="Y05868-99999-99999-999999")
),
target=Reference(identifier=Identifier(value="Y05868-99999-99999-999999")),
)
]

Expand Down Expand Up @@ -1304,9 +1292,7 @@ def test_upsert_document_reference_supersede_succeeds_with_toggle(
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="replaces",
target=Reference(
reference=None, identifier=Identifier(value="Y05868-99999-99999-000000")
),
target=Reference(identifier=Identifier(value="Y05868-99999-99999-000000")),
)
]

Expand Down Expand Up @@ -1364,9 +1350,7 @@ def test_upsert_document_reference_supersede_fails_without_toggle(
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="replaces",
target=Reference(
reference=None, identifier=Identifier(value="Y05868-99999-99999-000000")
),
target=Reference(identifier=Identifier(value="Y05868-99999-99999-000000")),
)
]

Expand Down Expand Up @@ -1422,9 +1406,7 @@ def test_upsert_document_reference_create_relatesto_not_replaces(
doc_ref.relatesTo = [
DocumentReferenceRelatesTo(
code="transforms",
target=Reference(
reference=None, identifier=Identifier(value="Y05868-99999-99999-999999")
),
target=Reference(identifier=Identifier(value="Y05868-99999-99999-999999")),
)
]

Expand Down
4 changes: 2 additions & 2 deletions layer/nrlf/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ def append_value_set_url(loc_string: str) -> str:

def diag_for_error(error: ErrorDetails) -> str:
loc_string = format_error_location(error["loc"])
msg = f"{loc_string or 'root'}: {error['msg']}"
msg = f"{loc_string or 'DocumentReference'}: {error['msg']}"
msg += append_value_set_url(loc_string)
return msg


def expression_for_error(error: ErrorDetails) -> Optional[str]:
return format_error_location(error["loc"]) or "root"
return format_error_location(error["loc"]) or "DocumentReference"


class OperationOutcomeError(Exception):
Expand Down
Loading