From 8a090dfa8d4e9c9d00ed7ca6effbd021b45f9813 Mon Sep 17 00:00:00 2001 From: emmastephenson <80282552+emmastephenson@users.noreply.github.com> Date: Tue, 17 Oct 2023 14:32:38 -0700 Subject: [PATCH] Check for RR field presence before merging RR to eCR (#866) Avoids unnecessary work if an RR has already been merged with its eICR. --- phdi/fhir/conversion/convert.py | 4 +++ .../rr_extraction/merged_eICR.xml | 17 ++++++++++++ tests/fhir/conversion/test_conversion.py | 27 +++++++++++++++++++ 3 files changed, 48 insertions(+) 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 6511b33218..7cafc6cb7f 100644 --- a/phdi/fhir/conversion/convert.py +++ b/phdi/fhir/conversion/convert.py @@ -48,6 +48,10 @@ def add_rr_data_to_eicr(rr, ecr): rr = etree.fromstring(rr) ecr = etree.fromstring(ecr) + 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") + # Create the tags for elements we'll be looking for rr_tags = [ "templateId", 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 6e6e6a8a01..85d0e88a73 100644 --- a/tests/fhir/conversion/test_conversion.py +++ b/tests/fhir/conversion/test_conversion.py @@ -335,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