Skip to content

Commit

Permalink
feat: remove leading zeros from patient id (#190)
Browse files Browse the repository at this point in the history
* feat: remove leading zeros from patient id

This also moves test files into subfolder `tests`.

* feat: enable removing of leading zeros using env var

This will enable removing of leading zeros only if env var
`REMOVE_LEADING_PATIENTID_ZEROS` is set to 'true'.

* chore: add new env var to compose.decompose-xmls.yaml

* fix: change code to apply MegaLinter rules

* tests: add ci job to run tests
  • Loading branch information
pcvolkmer authored Jul 31, 2024
1 parent f632bff commit 6e8c835
Show file tree
Hide file tree
Showing 7 changed files with 1,158 additions and 1 deletion.
23 changes: 23 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ jobs:
secrets:
github-token: ${{ secrets.GITHUB_TOKEN }}

tests:
strategy:
matrix:
# Python versions to run tests for
python-version: ["3.11"]
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
cd src/decompose_xmls
pip install pytest
pip install -r requirements.txt
pip install -r requirements-test.txt
- name: Run decompose_xmls tests
run: |
cd src/decompose_xmls
pytest
test-k8s:
runs-on: ubuntu-22.04
# disabled since the major deployment platform is currently
Expand Down
2 changes: 2 additions & 0 deletions docker-compose/compose.decompose-xmls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ services:
KAFKA_BOOTSTRAP_SERVERS: "kafka:9092"
KAFKA_OUTPUT_TOPIC: "onkostar.MELDUNG_EXPORT"
SAVE_AS_FILES_ENABLED: "false"
# Set to 'true' to enable removing of leading zeros in patient IDs
REMOVE_LEADING_PATIENTID_ZEROS: "false"
volumes:
- ./input-obds-reports:/app/input-obds-reports
- ./output-obds-reports:/app/output-obds-reports
792 changes: 792 additions & 0 deletions src/decompose_xmls/__snapshots__/decompose_xmls_test.ambr

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions src/decompose_xmls/decompose_xmls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import time
import regex
import xml.etree.ElementTree as ET
from io import BytesIO

Expand Down Expand Up @@ -46,6 +47,13 @@ def kafka_delivery_report(err, msg):
print(f"Message delivered to {msg.topic()}@{msg.partition()}")


def remove_leading_zeros(patient_id: str) -> str:
if os.environ.get('REMOVE_LEADING_PATIENTID_ZEROS') == 'true':
return regex.sub(r'^0+', '', patient_id)
else:
return patient_id


def decompose_sammelmeldung(root: ET.Element, filename: str):
results = []
settings = Settings()
Expand Down Expand Up @@ -91,8 +99,15 @@ def decompose_sammelmeldung(root: ET.Element, filename: str):
# append child Patient_Stammdaten first,
# append child Menge_Meldung_Group second
element_patient = ET.Element("Patient")

# Fix leading IDs in
if patient_id is not None:
patient_id = remove_leading_zeros(patient_id)
patient_stammdaten.set("Patient_ID", patient_id)

element_patient.append(patient_stammdaten)
element_patient.append(menge_meldung_group)

# build grandparent tag Menge_Patient and add Patient + children
menge_patient = ET.Element("Menge_Patient")
menge_patient.append(element_patient)
Expand Down
29 changes: 28 additions & 1 deletion src/decompose_xmls/decompose_xmls_test.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
import os
import xml.etree.ElementTree as ET

import pytest

from decompose_xmls.decompose_xmls import decompose_sammelmeldung
from .decompose_xmls import decompose_sammelmeldung


@pytest.mark.parametrize(
"obds_input_file_path", [("input-obds-reports/test-2patients.xml")]
)
def test_decompose_sammelmeldung(snapshot, obds_input_file_path):
os.environ['REMOVE_LEADING_PATIENTID_ZEROS'] = 'false'
tree = ET.parse(obds_input_file_path)
root = tree.getroot()

result = decompose_sammelmeldung(root, obds_input_file_path)
assert result == snapshot


@pytest.mark.parametrize(
"obds_input_file_path", [("input-obds-reports/test-patientid-with-zeros.xml")]
)
def test_decompose_with_pathient_id_starting_with_zero(snapshot, obds_input_file_path):
os.environ['REMOVE_LEADING_PATIENTID_ZEROS'] = 'true'
tree = ET.parse(obds_input_file_path)
root = tree.getroot()

result = decompose_sammelmeldung(root, obds_input_file_path)
assert result == snapshot


@pytest.mark.parametrize(
"obds_input_file_path", [("input-obds-reports/test-patientid-with-zeros.xml")]
)
def test_decompose_keep_pathient_id_starting_with_zero(snapshot, obds_input_file_path):
os.environ['REMOVE_LEADING_PATIENTID_ZEROS'] = 'false'

tree = ET.parse(obds_input_file_path)
root = tree.getroot()

Expand Down
Loading

0 comments on commit 6e8c835

Please sign in to comment.