Files
WebApi/tests/test_cacu_eligibility.py
T

93 lines
3.3 KiB
Python

"""The CaCu eligibility operation serves the gold report as FHIR Parameters."""
import json
from datetime import date
import pandas as pd
import pytest
from fastapi import HTTPException
from core.domains import Domain
from persistance import gold
from webapi import fhir
def _patient_reads(monkeypatch, exists=True):
monkeypatch.setattr(
fhir.reads,
"rows",
lambda domain, kind, **params: ([{"person_id": params["id"]}] if exists else [])
if kind == "person" else [],
)
def test_patient_eligibility_operation_returns_reasons_and_applies_filters(monkeypatch):
_patient_reads(monkeypatch)
calls = []
def report(scenario, person_id, *, cutoff):
calls.append((scenario, person_id, cutoff))
return pd.DataFrame([{
"scenario": "surveillance",
"person_id": person_id,
"index_date": date(2025, 2, 1),
"index_sample_id": "sample-1",
"criterion": "hysterectomy",
"eligible": False,
}])
monkeypatch.setattr(fhir.gold_reads, "cacu_eligibility_report", report)
response = fhir.cacu_eligibility(
Domain.CACU, "patient-1", "surveillance", date(2025, 2, 15)
)
assert response.status_code == 200
assert response.media_type == fhir.FHIR_JSON
body = json.loads(response.body)
parts = {p["name"]: p for p in body["parameter"]}
assert body["resourceType"] == "Parameters"
assert parts["patient"]["valueReference"]["reference"] == "Patient/patient-1"
assert parts["cutoff"]["valueDate"] == "2025-02-15"
candidate = parts["candidate"]["part"]
reason = next(p for p in candidate if p["name"] == "criterion")
assert reason["valueCoding"]["code"] == "hysterectomy"
assert reason["valueCoding"]["system"].endswith("/CodeSystem/cacu-exclusion-criterion")
assert calls == [("surveillance", "patient-1", date(2025, 2, 15))]
def test_patient_eligibility_reports_stale_gold_as_fhir_conflict(monkeypatch):
_patient_reads(monkeypatch)
def outdated(*args, **kwargs):
raise gold.GoldSchemaError("rebuild gold.cacu_eligibility")
monkeypatch.setattr(fhir.gold_reads, "cacu_eligibility_report", outdated)
response = fhir.cacu_eligibility(
Domain.CACU, "patient-1", None, date(2025, 2, 15)
)
assert response.status_code == 409
assert response.media_type == fhir.FHIR_JSON
body = json.loads(response.body)
assert body["resourceType"] == "OperationOutcome"
assert body["issue"][0]["code"] == "conflict"
assert "rebuild gold.cacu_eligibility" in body["issue"][0]["diagnostics"]
def test_patient_eligibility_rejects_other_domains_unknown_scenarios_and_unknown_patients(
monkeypatch,
):
_patient_reads(monkeypatch)
with pytest.raises(HTTPException) as other_domain:
fhir.cacu_eligibility(Domain.CAMA, "patient-1", None, date(2025, 2, 15))
assert other_domain.value.status_code == 404
with pytest.raises(HTTPException) as unknown_scenario:
fhir.cacu_eligibility(Domain.CACU, "patient-1", "unknown", date(2025, 2, 15))
assert unknown_scenario.value.status_code == 422
_patient_reads(monkeypatch, exists=False)
with pytest.raises(HTTPException) as unknown_patient:
fhir.cacu_eligibility(Domain.CACU, "missing", None, date(2025, 2, 15))
assert unknown_patient.value.status_code == 404