128 lines
5.2 KiB
Python
128 lines
5.2 KiB
Python
"""The CapabilityStatement is built from the routes, so a route it cannot describe breaks it for
|
|
every domain. The experimental NCI operation (`$reference-risk`, behind CACU_NCI_REFERENCE) is
|
|
registered whether or not the flag is on, and its `cutoff` and `sample` are operation parameters,
|
|
not search parameters: the statement is served either way, and names no search parameter that
|
|
is not one."""
|
|
|
|
import json
|
|
|
|
import pytest
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
|
|
from webapi import fhir
|
|
|
|
DOMAINS = ("cacu", "cama", "tea")
|
|
OPERATION_PARAMETERS = {"cutoff", "sample"}
|
|
|
|
|
|
@pytest.fixture
|
|
def client() -> TestClient:
|
|
app = FastAPI()
|
|
app.include_router(fhir.router) # the router alone: metadata reads the routes, not the data
|
|
return TestClient(app)
|
|
|
|
|
|
def _statement(client: TestClient, domain: str) -> dict:
|
|
response = client.get(f"/{domain}/fhir/metadata")
|
|
assert response.status_code == 200, response.text
|
|
assert response.headers["content-type"].startswith("application/fhir+json")
|
|
body = response.json()
|
|
assert body["resourceType"] == "CapabilityStatement"
|
|
return body
|
|
|
|
|
|
def _resources(statement: dict) -> dict[str, dict]:
|
|
return {r["type"]: r for r in statement["rest"][0]["resource"]}
|
|
|
|
|
|
@pytest.mark.parametrize("flag", [None, "0", "1"])
|
|
@pytest.mark.parametrize("domain", DOMAINS)
|
|
def test_metadata_is_served_with_the_nci_reference_on_or_off(client, monkeypatch, domain, flag):
|
|
if flag is None:
|
|
monkeypatch.delenv(fhir.NCI_REFERENCE, raising=False)
|
|
else:
|
|
monkeypatch.setenv(fhir.NCI_REFERENCE, flag)
|
|
resources = _resources(_statement(client, domain))
|
|
assert "MeasureReport" in resources
|
|
for resource in resources.values():
|
|
codes = [i["code"] for i in resource["interaction"]]
|
|
assert len(codes) == len(set(codes)), f"{resource['type']}: repeated interactions {codes}"
|
|
for param in resource.get("searchParam", []):
|
|
assert param["type"], param
|
|
assert param["name"] not in OPERATION_PARAMETERS, (
|
|
f"{resource['type']}: {param['name']} belongs to the operation, not to a search")
|
|
|
|
|
|
def test_the_flag_does_not_change_what_the_statement_declares(client, monkeypatch):
|
|
def declared(flag: str | None) -> dict:
|
|
if flag is None:
|
|
monkeypatch.delenv(fhir.NCI_REFERENCE, raising=False)
|
|
else:
|
|
monkeypatch.setenv(fhir.NCI_REFERENCE, flag)
|
|
body = _statement(client, "cacu")
|
|
body.pop("date") # the day it was served
|
|
return json.loads(json.dumps(body, sort_keys=True))
|
|
|
|
assert declared(None) == declared("1")
|
|
|
|
|
|
@pytest.mark.parametrize("domain", ["cama", "tea"])
|
|
def test_the_nci_operation_stays_cacu_only_when_enabled(client, monkeypatch, domain):
|
|
monkeypatch.setenv(fhir.NCI_REFERENCE, "1")
|
|
response = client.get(f"/{domain}/fhir/Patient/any/$reference-risk")
|
|
assert response.status_code == 404
|
|
|
|
|
|
def test_the_nci_operation_is_refused_while_the_flag_is_off(client, monkeypatch):
|
|
monkeypatch.delenv(fhir.NCI_REFERENCE, raising=False)
|
|
response = client.get("/cacu/fhir/Patient/any/$reference-risk")
|
|
assert response.status_code == 404
|
|
|
|
|
|
@pytest.mark.parametrize("domain,measure", [("cacu", "cacu-eu7"), ("cama", "cama-birads")])
|
|
@pytest.mark.parametrize("period", ["2026-02", "2026-S1"])
|
|
def test_measure_cutoff_recalculates_the_partial_period(
|
|
monkeypatch, domain, measure, period
|
|
):
|
|
from datetime import UTC, date, datetime
|
|
|
|
from fastapi import Request
|
|
|
|
from core.domains import Domain
|
|
|
|
def row(day, person):
|
|
if domain == "cacu":
|
|
return {
|
|
"sample_id": person, "person_id": person, "sample_date": day,
|
|
"valid": 1, "result": "NILM", "birth_date": "1990-01-01",
|
|
"screening_history": "never", "detection": "initial", "first_case": None, "first_treatment": None,
|
|
"first_colposcopy": None, "first_postcolposcopy_cytology": None,
|
|
"known_at": "2026-02-01", "report_known_at": "2026-02-01",
|
|
}
|
|
return {
|
|
"person_id": person, "exam_date": day, "birads": "4", "birads_rank": 4,
|
|
"report_date": day, "known_at": "2026-02-01", "report_known_at": "2026-02-01",
|
|
}
|
|
|
|
monkeypatch.setattr(fhir.measure_reads, "rows", lambda *args, **kwargs: [
|
|
row("2026-02-05", "a"), row("2026-02-20", "b"),
|
|
])
|
|
request = Request({"type": "http", "method": "GET", "scheme": "http", "server": ("test", 80),
|
|
"path": f"/{domain}/fhir/MeasureReport", "query_string": b"", "headers": []})
|
|
|
|
def count(cutoff):
|
|
answer = fhir.search_measure_report(
|
|
request=request, domain=Domain(domain), measure=measure, periods=period,
|
|
as_of=datetime(2026, 2, 15, 23, 59, 59, tzinfo=UTC) if cutoff else None,
|
|
event_cutoff=date(2026, 2, 15) if cutoff else None,
|
|
period_cutoff=date(2026, 2, 15) if cutoff else None, _count=None, _offset=0,
|
|
)
|
|
report = json.loads(answer.body)["entry"][0]["resource"]
|
|
return report["group"][0]["population"][0]["count"], report["period"]
|
|
|
|
uncut, original = count(False)
|
|
cut, reported = count(True)
|
|
assert (uncut, cut) == (2, 1)
|
|
assert original == reported # report retains the requested month or semester
|