Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minimum provenance to FHIR converter for data source #755

Merged
merged 19 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
b31ea86
added two functions for minimum provenance
Jul 31, 2023
48fae20
added test for first of the SDK functions
Aug 1, 2023
6141092
fixed the SDK function to make it generic; data_source is supplied
robertmitchellv Aug 1, 2023
58e0de1
removed test for function we're not using; added test for refactored …
robertmitchellv Aug 1, 2023
27c5aed
forgot to run black
robertmitchellv Aug 1, 2023
b2ac5a0
ensured that there was a 'meta' for every resource; test passes locally
robertmitchellv Aug 1, 2023
e23bb1c
added another test to make sure ValueError is raised and to bump up c…
robertmitchellv Aug 1, 2023
f4a6d97
Merge branch 'main' into robert/add-data-source-to-bundle
robertmitchellv Aug 1, 2023
dffa872
removed the functions and tests from SDK
robertmitchellv Aug 2, 2023
060d181
added function and test to the fhir-converter script--it hasn't been …
robertmitchellv Aug 2, 2023
b96b29b
removed old import from utils, added import to container tests and te…
robertmitchellv Aug 2, 2023
56fed1e
removed pathlib depend and added minimal test bundle
robertmitchellv Aug 2, 2023
4d1fcfa
added pytest to test imports
robertmitchellv Aug 2, 2023
d9d015c
added function to the fhir converter function using input_type as source
robertmitchellv Aug 2, 2023
2048d65
realized there was already a valid bundle; removed the one i added
robertmitchellv Aug 2, 2023
eddeef6
removed unneeded test data
robertmitchellv Aug 2, 2023
5085229
added logic to test provenance in the fhir converter
robertmitchellv Aug 2, 2023
d056aa3
added test for provenance to the valid fhir converter test; passes lo…
robertmitchellv Aug 2, 2023
7330c0f
typo
Aug 2, 2023
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
33 changes: 33 additions & 0 deletions containers/fhir-converter/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,38 @@ async def convert(input: FhirConverterInput, response: Response):
return result


def add_data_source_to_bundle(bundle: dict, data_source: str) -> dict:
"""
Given a FHIR bundle and a a data source parameter the function
will loop through the bundle and add a Meta.source entry for
every resource in the bundle.

:param bundle: The FHIR bundle to add minimum provenance to.
:param data_source: The data source of the FHIR bundle.
:return: The FHIR bundle with the a Meta.source entry for each
FHIR resource in the bunle
"""
if data_source == "":
raise ValueError(
"The data_source parameter must be a defined, non-empty string."
)

for entry in bundle.get("entry", []):
resource = entry.get("resource", {})
if "meta" in resource:
meta = resource["meta"]
else:
meta = {}
resource["meta"] = meta

if "source" in meta:
meta["source"].append(data_source)
else:
meta["source"] = [data_source]

return bundle


def convert_to_fhir(
input_data: str,
input_type: str,
Expand Down Expand Up @@ -239,6 +271,7 @@ def convert_to_fhir(
result = json.dumps(result)
result = result.replace(old_id, new_id)
result = json.loads(result)
add_data_source_to_bundle(result["FhirResource"], input_type)

else:
result = vars(converter_response)
Expand Down
23 changes: 21 additions & 2 deletions containers/fhir-converter/tests/test_FHIR-Converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from unittest import mock
from fastapi.testclient import TestClient
import json
from app.main import app
import pytest
from app.main import app, add_data_source_to_bundle

client = TestClient(app)

Expand Down Expand Up @@ -189,7 +190,6 @@ def test_convert_valid_request(
):
global valid_response
patched_subprocess_run.return_value = mock.Mock(returncode=0)
print(valid_response)
patched_json_load.return_value = valid_response
patched_file_path = mock.Mock()
actual_response = client.post(
Expand All @@ -203,6 +203,7 @@ def test_convert_valid_request(
valid_response = json.dumps(valid_response)
valid_response = valid_response.replace(old_id, new_id)
valid_response = json.loads(valid_response)
add_data_source_to_bundle(valid_response["FhirResource"], "elr")
assert actual_response == valid_response


Expand Down Expand Up @@ -256,3 +257,21 @@ def test_convert_invalid_root_template(patched_subprocess_run):
)
assert actual_response.status_code == 422
assert actual_response.json() == invalid_root_template_response


def test_add_data_source_to_bundle():
expected_data_source = "ecr"
bundle_result = add_data_source_to_bundle(valid_response, expected_data_source)
for entry in bundle_result.get("entry", []):
resource = entry.get("resource", {})
assert expected_data_source in resource["meta"]["source"]


def test_add_data_source_to_bundle_missing_arg():
expected_error_message = (
"The data_source parameter must be a defined, non-empty string."
)
with pytest.raises(ValueError) as excinfo:
add_data_source_to_bundle(valid_response, "")
result_error_message = str(excinfo.value)
assert expected_error_message in result_error_message