Skip to content

Commit

Permalink
Fixed wording and add assert to operaction outcome type error
Browse files Browse the repository at this point in the history
Ref #76
  • Loading branch information
qscgyjqscgyj committed Feb 27, 2025
1 parent 785c5fe commit da7688a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
6 changes: 3 additions & 3 deletions app/sdc/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ async def get_external_service_bundle(session, service, template, context):


async def execute_mappers_bundles(client, mappers_bundles):
flatted_mappers_bundles = list(flatten(bundle["entry"] for bundle in mappers_bundles))
flattened_mappers_bundles = list(flatten(bundle["entry"] for bundle in mappers_bundles))

check_mappers_bundles_full_url_duplicates(flatted_mappers_bundles)
check_mappers_bundles_full_url_duplicates(flattened_mappers_bundles)

not_transaction = any(bundle.get("type") != "transaction" for bundle in mappers_bundles)

result_bundle = {
"resourceType": "Bundle",
"type": "batch" if not_transaction else "transaction",
"entry": flatted_mappers_bundles,
"entry": flattened_mappers_bundles,
}

return await client.execute("/", data=result_bundle)
Expand Down
13 changes: 4 additions & 9 deletions app/sdc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ def get_type(item, data):
def walk_dict(d, transform):
for k, v in d.items():
if is_list(v):
d[k] = [
walk_dict(vi, transform) if is_mapping(vi) else transform(vi, k)
for vi in v
]
d[k] = [walk_dict(vi, transform) if is_mapping(vi) else transform(vi, k) for vi in v]
elif is_mapping(v):
d[k] = walk_dict(v, transform)
else:
Expand All @@ -68,9 +65,7 @@ def prepare_link_ids(questionnaire, variables):


def prepare_bundle(raw_bundle, env):
return walk_dict(
raw_bundle, lambda v, _k: resolve_string_template(v, env, encode_result=True)
)
return walk_dict(raw_bundle, lambda v, _k: resolve_string_template(v, env, encode_result=True))


def resolve_string_template(i, env, encode_result=False):
Expand Down Expand Up @@ -172,10 +167,10 @@ def validate_context(context_definition, env):
raise ConstraintCheckOperationOutcome(errors)


def check_mappers_bundles_full_url_duplicates(flatted_mappers_bundles):
def check_mappers_bundles_full_url_duplicates(flattened_mappers_bundles):
full_urls_set = set()

for bundle in flatted_mappers_bundles:
for bundle in flattened_mappers_bundles:
full_url = bundle.get("fullUrl")

if full_url is None:
Expand Down
40 changes: 23 additions & 17 deletions tests/sdc/test_extract.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest

from fhirpy.base.lib import OperationOutcome
from fhirpy.base.utils import get_by_path

from app.converter.aidbox import from_first_class_extension
from app.test.utils import create_parameters
Expand Down Expand Up @@ -748,9 +750,9 @@ async def test_fhir_extract_using_list_endpoint_fails_because_of_constraint_chec
)


PATINET_1_ID = "patient1"
PATINET_1_FULL_URL = "urn:multiple-mappers-test-patient"
PATINET_2_ID = "patient2"
PATIENT_1_ID = "patient1"
PATIENT_1_FULL_URL = "urn:multiple-mappers-test-patient"
PATIENT_2_ID = "patient2"
OBSERVATION_CODE = "obs1"

PATIENT_BUNDLE_DATA = {
Expand All @@ -760,7 +762,7 @@ async def test_fhir_extract_using_list_endpoint_fails_because_of_constraint_chec
"entry": [
{
"request": {"url": "/Patient", "method": "POST"},
"fullUrl": PATINET_1_FULL_URL,
"fullUrl": PATIENT_1_FULL_URL,
"resource": {
"resourceType": "Patient",
"id": """$ fhirpath("QuestionnaireResponse.item.where(linkId='patientId').answer.children().string").0""",
Expand All @@ -777,7 +779,7 @@ async def test_fhir_extract_using_list_endpoint_fails_because_of_constraint_chec
"entry": [
{
"request": {"url": "/Patient", "method": "POST"},
"fullUrl": PATINET_1_FULL_URL,
"fullUrl": PATIENT_1_FULL_URL,
"resource": {
"resourceType": "Patient",
"id": """$ fhirpath("QuestionnaireResponse.item.where(linkId='patientId2').answer.children().string").0""",
Expand Down Expand Up @@ -844,7 +846,7 @@ async def test_fhir_extract_using_list_endpoint_fails_because_of_constraint_chec
}
]
},
"subject": {"uri": PATINET_1_FULL_URL},
"subject": {"uri": PATIENT_1_FULL_URL},
"status": "final",
},
}
Expand Down Expand Up @@ -889,7 +891,7 @@ async def test_fce_extract_multiple_mappers(aidbox_client, safe_db):
**{
"questionnaire": q.id,
"item": [
{"linkId": "patientId", "answer": [{"value": {"string": PATINET_1_ID}}]},
{"linkId": "patientId", "answer": [{"value": {"string": PATIENT_1_ID}}]},
{"linkId": "observationCode", "answer": [{"value": {"string": OBSERVATION_CODE}}]},
],
},
Expand All @@ -900,13 +902,13 @@ async def test_fce_extract_multiple_mappers(aidbox_client, safe_db):
assert extraction[0]["resourceType"] == "Bundle"
assert len(extraction[0]["entry"]) == 2

p = await aidbox_client.resources("Patient").search(id=PATINET_1_ID).fetch_all()
p = await aidbox_client.resources("Patient").search(id=PATIENT_1_ID).fetch_all()
o = await aidbox_client.resources("Observation").search(code=OBSERVATION_CODE).fetch_all()

assert len(p) == 1
assert len(o) == 1

assert p[0].id == PATINET_1_ID
assert p[0].id == PATIENT_1_ID
assert o[0].code["coding"][0]["code"] == OBSERVATION_CODE
assert o[0].status == "final"

Expand Down Expand Up @@ -947,16 +949,18 @@ async def test_fce_extract_multiple_mappers_is_atomic(aidbox_client, safe_db):
**{
"questionnaire": q.id,
"item": [
{"linkId": "patientId", "answer": [{"value": {"string": PATINET_1_ID}}]},
{"linkId": "patientId", "answer": [{"value": {"string": PATIENT_1_ID}}]},
{"linkId": "observationCode", "answer": [{"value": {"string": OBSERVATION_CODE}}]},
],
},
)

with pytest.raises(OperationOutcome):
with pytest.raises(OperationOutcome) as excinfo:
await q.execute("$extract", data=qr)

p = await aidbox_client.resources("Patient").search(id=PATINET_1_ID).fetch_all()
assert get_by_path(excinfo.value.resource, ["issue", 0, "code"]) == "invalid"

p = await aidbox_client.resources("Patient").search(id=PATIENT_1_ID).fetch_all()
o = await aidbox_client.resources("Observation").search(code=OBSERVATION_CODE).fetch_all()

assert p == []
Expand Down Expand Up @@ -1007,18 +1011,20 @@ async def test_fce_extract_multiple_mappers_checks_unique_full_urls(aidbox_clien
**{
"questionnaire": q.id,
"item": [
{"linkId": "patientId", "answer": [{"value": {"string": PATINET_1_ID}}]},
{"linkId": "patientId2", "answer": [{"value": {"string": PATINET_2_ID}}]},
{"linkId": "patientId", "answer": [{"value": {"string": PATIENT_1_ID}}]},
{"linkId": "patientId2", "answer": [{"value": {"string": PATIENT_2_ID}}]},
{"linkId": "observationCode", "answer": [{"value": {"string": OBSERVATION_CODE}}]},
],
},
)

with pytest.raises(OperationOutcome):
with pytest.raises(OperationOutcome) as excinfo:
await q.execute("$extract", data=qr)

p1 = await aidbox_client.resources("Patient").search(id=PATINET_1_ID).fetch_all()
p2 = await aidbox_client.resources("Patient").search(id=PATINET_2_ID).fetch_all()
assert get_by_path(excinfo.value.resource, ["issue", 0, "code"]) == "duplicate-full-url"

p1 = await aidbox_client.resources("Patient").search(id=PATIENT_1_ID).fetch_all()
p2 = await aidbox_client.resources("Patient").search(id=PATIENT_2_ID).fetch_all()
o = await aidbox_client.resources("Observation").search(code=OBSERVATION_CODE).fetch_all()

assert p1 == []
Expand Down

0 comments on commit da7688a

Please sign in to comment.