From da7688ae61e3e04f9b2075c26b9395ce4bcda227 Mon Sep 17 00:00:00 2001 From: qscgyjqscgyj Date: Thu, 27 Feb 2025 14:58:59 +0100 Subject: [PATCH] Fixed wording and add assert to operaction outcome type error Ref #76 --- app/sdc/extract.py | 6 +++--- app/sdc/utils.py | 13 ++++--------- tests/sdc/test_extract.py | 40 ++++++++++++++++++++++----------------- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/app/sdc/extract.py b/app/sdc/extract.py index c4fb4a1..4ad8573 100644 --- a/app/sdc/extract.py +++ b/app/sdc/extract.py @@ -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) diff --git a/app/sdc/utils.py b/app/sdc/utils.py index 159b5c5..3e3bb9c 100644 --- a/app/sdc/utils.py +++ b/app/sdc/utils.py @@ -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: @@ -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): @@ -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: diff --git a/tests/sdc/test_extract.py b/tests/sdc/test_extract.py index 29b5088..de98063 100644 --- a/tests/sdc/test_extract.py +++ b/tests/sdc/test_extract.py @@ -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 @@ -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 = { @@ -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""", @@ -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""", @@ -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", }, } @@ -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}}]}, ], }, @@ -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" @@ -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 == [] @@ -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 == []