Files
WebApi/tests/test_synthea.py
T

63 lines
2.9 KiB
Python

"""Phase 3.5 gate: Synthea through the FHIR door. A seeded synthetic population (Synthea,
with the platform's cervical-screening module) arrives as a standard hospital would send it,
and every report it holds reaches silver: nothing held, one person per patient."""
import json
import os
import subprocess
from pathlib import Path
from fhir_checks import pages
from test_conectar import ok, wait_for
ROOT = Path(__file__).parents[2]
SYNTHEA = os.environ.get("SYNTHEA_JAR", "/opt/synthea/synthea.jar")
MODULES = ROOT / "tests" / "fixtures" / "synthea" / "modules"
DOOR = ROOT / "tests" / "fixtures" / "pipelines" / "fhir_door.json"
STUDY = "https://isssteleon.org/fhir/CodeSystem/study"
def _population(out: Path, people: int = 20) -> list[dict]:
"""Synthea's FHIR R4 transaction Bundles, one per patient (fixed seeds)."""
subprocess.run(
["java", "-jar", SYNTHEA, "-p", str(people), "-s", "7", "-cs", "7", "-g", "F",
"-a", "25-64", "-d", str(MODULES), "-m", "cervical_screening",
"--exporter.baseDirectory", str(out), "--exporter.fhir.export", "true",
"--exporter.hospital.fhir.export", "false",
"--exporter.practitioner.fhir.export", "false"],
check=True, capture_output=True, cwd=out.parent,
) # fmt: skip
return [json.loads(p.read_text()) for p in sorted((out / "fhir").glob("*.json"))]
def _reports(bundles: list[dict], study: str) -> int:
return sum(
1 for b in bundles for e in b["entry"]
if e["resource"]["resourceType"] == "DiagnosticReport"
and any(c.get("system") == STUDY and c.get("code") == study
for c in e["resource"].get("code", {}).get("coding", []))
) # fmt: skip
def test_a_synthea_population_comes_in_through_the_fhir_door(api, platform, tmp_path):
bundles = _population(tmp_path / "synthea")
patients = sum(1 for b in bundles for e in b["entry"]
if e["resource"]["resourceType"] == "Patient") # fmt: skip
assert patients == 20 and _reports(bundles, "cervical_cytology") > 40
receiving = DOOR.read_text().replace("{sender}", "synthea")
ok(api.put("/ingest/pipelines/synthea", json=json.loads(receiving)))
batch = {"resourceType": "Bundle", "type": "batch",
"entry": [e for b in bundles for e in b["entry"]]} # fmt: skip
answer = api.post("/cacu/fhir?sender=synthea", json=batch)
assert answer.status_code == 202, answer.text[:500]
wait_for(api, answer.headers["location"].rsplit("/", 1)[1])
# Nothing held, every report placed, one person per patient.
for study in ("cervical_cytology", "cervical_biopsy"):
assert ok(api.get("/review/held", params={"entity": study})) == [], study
assert platform.count(f"silver.{study}") == _reports(bundles, study), study
assert platform.count("silver.person") == patients
(served,) = pages(api, "/cacu/fhir/Patient", {"_count": 200})
assert served["total"] == patients