Skip to content

Commit

Permalink
Fix populate for reference
Browse files Browse the repository at this point in the history
  • Loading branch information
ir4y committed Sep 17, 2024
1 parent 9e87728 commit 75208a5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
17 changes: 13 additions & 4 deletions app/sdc/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ def init_item():
)
if data and len(data):
type = get_type(item, data)
answers.extend([{"value": {type: d}} for d in data])
if type == "Reference":
answers.extend([{"value": {"Reference": {"reference": d}}} for d in data])
else:
answers.extend([{"value": {type: d}} for d in data])
if answers:
root_item["answer"] = answers
elif "initialExpression" in item:
Expand All @@ -71,10 +74,16 @@ def init_item():
raise OperationOutcome(f'Error: "{item["initialExpression"]["expression"]}" - {str(e)}')
if data and len(data):
type = get_type(item, data)
if item.get("repeats") is True:
answers = [{"value": {type: d}} for d in data]
if type == "Reference":
if item.get("repeats") is True:
answers = [{"value": {"Reference": {"reference": d}}} for d in data]
else:
answers = [{"value": {"Reference": {"reference": data[0]}}}]
else:
answers = [{"value": {type: data[0]}}]
if item.get("repeats") is True:
answers = [{"value": {type: d}} for d in data]
else:
answers = [{"value": {type: data[0]}}]
if answers:
root_item["answer"] = answers
elif "initial" in item:
Expand Down
40 changes: 40 additions & 0 deletions tests/sdc/test_populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from app.test.utils import create_parameters
from app.converter.aidbox import from_first_class_extension


@pytest.mark.asyncio
Expand Down Expand Up @@ -802,3 +803,42 @@ async def test_source_query_populate_fhir_from_api(aidbox_client, safe_db):
"questionnaire": questionnaire.id,
"resourceType": "QuestionnaireResponse",
}


@pytest.mark.asyncio
async def test_reference_populate(aidbox_client, safe_db):
q = aidbox_client.resource(
"Questionnaire",
**{
"status": "active",
"launchContext": [{"name": {"code": "LaunchPatient"}, "type": ["Patient"]}],
"item": [
{
"type": "reference",
"linkId": "patientId",
"initialExpression": {
"language": "text/fhirpath",
"expression": "'Patient/' + %LaunchPatient.id",
},
},
],
},
)
await q.save()

assert q.id is not None

launch_patient = {"resourceType": "Patient", "id": "patienit-id"}

p = await from_first_class_extension(await q.execute("$populate", data=create_parameters(LaunchPatient=launch_patient)), aidbox_client)

assert p == {
"resourceType": "QuestionnaireResponse",
"questionnaire": q.id,
"item": [
{
"linkId": "patientId",
"answer": [{"valueReference": {"reference": f"Patient/{launch_patient['id']}"}}],
}
],
}

0 comments on commit 75208a5

Please sign in to comment.