From 51f8533c49b246eab9f694eba58980b64f7690ff Mon Sep 17 00:00:00 2001 From: emmastephenson Date: Fri, 13 Oct 2023 10:47:25 -0700 Subject: [PATCH 1/4] wip --- phdi/fhir/conversion/convert.py | 6 ++++++ tests/fhir/conversion/test_conversion.py | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/phdi/fhir/conversion/convert.py b/phdi/fhir/conversion/convert.py index 6511b33218..3e45432a61 100644 --- a/phdi/fhir/conversion/convert.py +++ b/phdi/fhir/conversion/convert.py @@ -48,6 +48,12 @@ def add_rr_data_to_eicr(rr, ecr): rr = etree.fromstring(rr) ecr = etree.fromstring(ecr) + # Check to make sure RR hasn't already been merged with eCR + + # you'd think throwing an exception would get the test to fail, but apparently not! + x = ecr.findtext("Reportability Response") + raise Exception("This is for testing purposes. Is RR present?", x) + # Create the tags for elements we'll be looking for rr_tags = [ "templateId", diff --git a/tests/fhir/conversion/test_conversion.py b/tests/fhir/conversion/test_conversion.py index 6e6e6a8a01..49e6780ae2 100644 --- a/tests/fhir/conversion/test_conversion.py +++ b/tests/fhir/conversion/test_conversion.py @@ -296,9 +296,13 @@ def test_add_rr_to_ecr(): ) as fp: ecr = fp.read() + print("eCR before conversion: ", ecr) + # extract rr fields, insert to ecr ecr = add_rr_data_to_eicr(rr, ecr) + print("eCR after conversion: ", ecr) + # confirm root tag added ecr_root = ecr.splitlines()[0] xsi_tag = "xmlns:xsi" @@ -307,7 +311,9 @@ def test_add_rr_to_ecr(): # confirm new section added ecr = etree.fromstring(ecr) tag = "{urn:hl7-org:v3}" + "section" + print("The test tag is: ", tag) section = ecr.find(f"./{tag}", namespaces=ecr.nsmap) + print("The section is: ", section) assert section is not None # confirm required elements added From 585847edea827bf682ef45157af78adfc2e243dd Mon Sep 17 00:00:00 2001 From: emmastephenson Date: Fri, 13 Oct 2023 14:07:41 -0700 Subject: [PATCH 2/4] wip --- phdi/fhir/conversion/convert.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/phdi/fhir/conversion/convert.py b/phdi/fhir/conversion/convert.py index 3e45432a61..ef4284fcd3 100644 --- a/phdi/fhir/conversion/convert.py +++ b/phdi/fhir/conversion/convert.py @@ -8,6 +8,7 @@ from phdi.fhir.transport import http_request_with_reauth from phdi.transport.http import http_request_with_retry from lxml import etree +import pathlib CCDA_CODES_TO_CONVERSION_RESOURCE = { @@ -23,6 +24,19 @@ } +def command_line_rr_extraction(rr_file_path, ecr_file_path): + with open( + pathlib.Path(__file__).parent.parent.parent + / "assets" + / "fhir-converter" + / "rr_extraction" + / "CDA_RR.xml" + ) as fp: + rr = fp.read() + + return rr + + def add_rr_data_to_eicr(rr, ecr): """ Extracts relevant fields from an RR document, and inserts them into a @@ -52,7 +66,7 @@ def add_rr_data_to_eicr(rr, ecr): # you'd think throwing an exception would get the test to fail, but apparently not! x = ecr.findtext("Reportability Response") - raise Exception("This is for testing purposes. Is RR present?", x) + print(x) # Create the tags for elements we'll be looking for rr_tags = [ From d9ea15451e39c0e912194fd014e667d7df3ce664 Mon Sep 17 00:00:00 2001 From: emmastephenson Date: Fri, 13 Oct 2023 15:30:36 -0700 Subject: [PATCH 3/4] check for rr before merging --- phdi/fhir/conversion/convert.py | 22 +++---------- .../rr_extraction/merged_eICR.xml | 17 ++++++++++ tests/fhir/conversion/test_conversion.py | 33 +++++++++++++++---- 3 files changed, 48 insertions(+), 24 deletions(-) create mode 100644 tests/assets/fhir-converter/rr_extraction/merged_eICR.xml diff --git a/phdi/fhir/conversion/convert.py b/phdi/fhir/conversion/convert.py index ef4284fcd3..683d21f71b 100644 --- a/phdi/fhir/conversion/convert.py +++ b/phdi/fhir/conversion/convert.py @@ -8,7 +8,6 @@ from phdi.fhir.transport import http_request_with_reauth from phdi.transport.http import http_request_with_retry from lxml import etree -import pathlib CCDA_CODES_TO_CONVERSION_RESOURCE = { @@ -24,19 +23,6 @@ } -def command_line_rr_extraction(rr_file_path, ecr_file_path): - with open( - pathlib.Path(__file__).parent.parent.parent - / "assets" - / "fhir-converter" - / "rr_extraction" - / "CDA_RR.xml" - ) as fp: - rr = fp.read() - - return rr - - def add_rr_data_to_eicr(rr, ecr): """ Extracts relevant fields from an RR document, and inserts them into a @@ -63,10 +49,10 @@ def add_rr_data_to_eicr(rr, ecr): ecr = etree.fromstring(ecr) # Check to make sure RR hasn't already been merged with eCR - - # you'd think throwing an exception would get the test to fail, but apparently not! - x = ecr.findtext("Reportability Response") - print(x) + # If it has, stop execution + if ecr.xpath('//*[contains(text(),"Reportability Response")]'): + print("This eCR has already been merged with RR data.") + return etree.tostring(ecr, encoding="unicode", method="xml") # Create the tags for elements we'll be looking for rr_tags = [ diff --git a/tests/assets/fhir-converter/rr_extraction/merged_eICR.xml b/tests/assets/fhir-converter/rr_extraction/merged_eICR.xml new file mode 100644 index 0000000000..26ea65fa86 --- /dev/null +++ b/tests/assets/fhir-converter/rr_extraction/merged_eICR.xml @@ -0,0 +1,17 @@ +
+ + + Reportability Response + + + + + + + + + + + + +
\ No newline at end of file diff --git a/tests/fhir/conversion/test_conversion.py b/tests/fhir/conversion/test_conversion.py index 49e6780ae2..85d0e88a73 100644 --- a/tests/fhir/conversion/test_conversion.py +++ b/tests/fhir/conversion/test_conversion.py @@ -296,13 +296,9 @@ def test_add_rr_to_ecr(): ) as fp: ecr = fp.read() - print("eCR before conversion: ", ecr) - # extract rr fields, insert to ecr ecr = add_rr_data_to_eicr(rr, ecr) - print("eCR after conversion: ", ecr) - # confirm root tag added ecr_root = ecr.splitlines()[0] xsi_tag = "xmlns:xsi" @@ -311,9 +307,7 @@ def test_add_rr_to_ecr(): # confirm new section added ecr = etree.fromstring(ecr) tag = "{urn:hl7-org:v3}" + "section" - print("The test tag is: ", tag) section = ecr.find(f"./{tag}", namespaces=ecr.nsmap) - print("The section is: ", section) assert section is not None # confirm required elements added @@ -341,3 +335,30 @@ def test_add_rr_to_ecr(): assert temps is not None assert temps.attrib["root"] == "2.16.840.1.113883.10.20.15.2.3.29" assert "RRVS19" in status_code.attrib["code"] + + +def test_add_rr_to_ecr_rr_already_present(capfd): + with open( + pathlib.Path(__file__).parent.parent.parent + / "assets" + / "fhir-converter" + / "rr_extraction" + / "CDA_RR.xml" + ) as fp: + rr = fp.read() + + # This eICR has already been merged with an RR + with open( + pathlib.Path(__file__).parent.parent.parent + / "assets" + / "fhir-converter" + / "rr_extraction" + / "merged_eICR.xml" + ) as fp: + ecr = fp.read() + + merged_ecr = add_rr_data_to_eicr(rr, ecr) + assert merged_ecr == ecr + + out, err = capfd.readouterr() + assert "This eCR has already been merged with RR data." in out From 4f885fcd36aeff8958963b3f02e4d80c283729fd Mon Sep 17 00:00:00 2001 From: emmastephenson Date: Tue, 17 Oct 2023 12:26:25 -0700 Subject: [PATCH 4/4] look for id instead of RR string --- phdi/fhir/conversion/convert.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/phdi/fhir/conversion/convert.py b/phdi/fhir/conversion/convert.py index 683d21f71b..7cafc6cb7f 100644 --- a/phdi/fhir/conversion/convert.py +++ b/phdi/fhir/conversion/convert.py @@ -48,9 +48,7 @@ def add_rr_data_to_eicr(rr, ecr): rr = etree.fromstring(rr) ecr = etree.fromstring(ecr) - # Check to make sure RR hasn't already been merged with eCR - # If it has, stop execution - if ecr.xpath('//*[contains(text(),"Reportability Response")]'): + if ecr.xpath('//*[@codeSystem="2.16.840.1.113883.6.1"]'): print("This eCR has already been merged with RR data.") return etree.tostring(ecr, encoding="unicode", method="xml")