96 lines
4.7 KiB
Python
96 lines
4.7 KiB
Python
"""What this module covers: the publication metadata of CaMa's FHIR `Measure`s (figures and category captions), read
|
|
from dbt's manifest alone.
|
|
|
|
Fixtures: `manifest`, dbt's `manifest.json` (`DBT_MANIFEST`; skipped without it); `parametros.toml`, the platform's
|
|
reference parameters, whose CaMa catalogs hold the captions and colours the dbt definitions repeat.
|
|
"""
|
|
|
|
import os
|
|
import tomllib
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from webapi.fhir.measures import CATEGORY_LABEL, FIGURES, labels, measure_resource
|
|
from webapi.kpis import definitions_of
|
|
|
|
REFERENCE = Path(__file__).parents[1] / "webapi" / "orchestration" / "reference" / "parametros.toml"
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def manifest():
|
|
path = os.environ.get("DBT_MANIFEST")
|
|
if not path:
|
|
pytest.skip("DBT_MANIFEST isn't set: `dbt compile` in Transformation writes it")
|
|
return Path(path)
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def measures(manifest):
|
|
"""Each CaMa measure as the API serves it, by its FHIR id."""
|
|
titles = labels(manifest)
|
|
return {d.measure_id.replace("_", "-"): measure_resource(d, *titles.get(d.metric, (d.metric, "")))
|
|
for d in definitions_of("cama", manifest).values()}
|
|
|
|
|
|
def figures(measure: dict) -> list[str]:
|
|
return [e["valueString"] for e in measure["extension"] if e["url"] == FIGURES]
|
|
|
|
|
|
def captions(measure: dict) -> dict[str, tuple[str, str]]:
|
|
"""The measure's declared categories, in order: code -> (caption, colour)."""
|
|
found = {}
|
|
for e in measure["extension"]:
|
|
if e["url"] == CATEGORY_LABEL:
|
|
part = {p["url"]: p.get("valueCode") or p["valueString"] for p in e["extension"]}
|
|
found[part["codigo"]] = (part["etiqueta"], part["color"])
|
|
return found
|
|
|
|
|
|
def catalog(name: str) -> dict[str, tuple[str, str]]:
|
|
with REFERENCE.open("rb") as f:
|
|
entries = tomllib.load(f)["cama"]["catalogos"][name]["valor"]
|
|
return {e["clave"]: (e["etiqueta"], e["color"]) for e in entries}
|
|
|
|
|
|
def no_data() -> tuple[str, str]:
|
|
with REFERENCE.open("rb") as f:
|
|
v = tomllib.load(f)["cama"]["catalogos"]["sin_dato"]["valor"]
|
|
return v["etiqueta"], v["color"]
|
|
|
|
|
|
def test_each_contract_publishes_the_figures_the_page_can_draw(measures):
|
|
"""Pins down: a distribution offers the donut and the bars, a duration its box plot, and the proportion and the
|
|
analysis' measure none. Fails if a figure is declared that the page has no builder for, or one is dropped."""
|
|
expected = {"CAMA-BIOPSIAS": ["fig_c2_donas", "fig_c2_barras"], "CAMA-BIRADS": ["fig_c2_donas", "fig_c2_barras"],
|
|
"CAMA-BIRADS456-MASTOGRAFIA": ["fig_c2_donas", "fig_c2_barras"],
|
|
"CAMA-BIRADS456-PACIENTE": ["fig_c2_donas", "fig_c2_barras"],
|
|
"CAMA-DIAS-TOMA-DIAGNOSTICO": ["fig_c3r_caja"], "CAMA-EU38A": ["fig_c3r_caja"],
|
|
"CAMA-CONFIRMACION-BIRADS45": [], "CAMA-BIRADS-CANCER-INVASOR": [],
|
|
"CAMA-DIAS-RESULTADO-DIAGNOSTICO": ["fig_c3r_caja"], "CAMA-TASA-DETECCION-HISTOPATOLOGICA": [],
|
|
"CAMA-HISTOPATOLOGIA-BIRADS": [], "CAMA-ESTADIO-CANCER-INVASOR": ["fig_c2_donas", "fig_c2_barras"],
|
|
"CAMA-TAMANO-CANCER-INVASOR": ["fig_c2_donas", "fig_c2_barras"]}
|
|
assert {i: figures(m) for i, m in measures.items()} == expected
|
|
|
|
|
|
def test_the_category_captions_are_the_reference_catalogs(measures):
|
|
"""Pins down: the captions, colours and order the dbt definitions publish equal `cama.catalogos.*` in the
|
|
reference parameters (BI-RADS, BI-RADS 4 to 6, biopsy grade, plus «Sin dato» where a unit can lack the value).
|
|
Fails if either side is edited without the other."""
|
|
sin_dato = {"SIN_DATO": no_data()}
|
|
assert captions(measures["CAMA-BIRADS"]) == {**catalog("birads"), **sin_dato}
|
|
assert captions(measures["CAMA-BIRADS456-PACIENTE"]) == catalog("birads_4_a_6")
|
|
assert captions(measures["CAMA-BIRADS456-MASTOGRAFIA"]) == catalog("birads_4_a_6")
|
|
assert captions(measures["CAMA-BIOPSIAS"]) == {**catalog("biopsia"), **sin_dato}
|
|
assert list(captions(measures["CAMA-BIRADS"])) == [*catalog("birads"), "SIN_DATO"]
|
|
# The histopathological result by BI-RADS: the biopsy grades, then the patients without a biopsy and without a grade.
|
|
assert captions(measures["CAMA-HISTOPATOLOGIA-BIRADS"]) == {**catalog("biopsia"), "SIN_BIOPSIA": ("Sin biopsia reportada", "#C9CDD2"), **sin_dato}
|
|
|
|
|
|
def test_the_measures_without_categories_declare_none(measures):
|
|
"""Pins down: proportions and durations carry no category captions. Fails if one is attached to a measure that
|
|
has no categories."""
|
|
for fhir_id in ("CAMA-CONFIRMACION-BIRADS45", "CAMA-DIAS-TOMA-DIAGNOSTICO", "CAMA-EU38A", "CAMA-DIAS-RESULTADO-DIAGNOSTICO",
|
|
"CAMA-TASA-DETECCION-HISTOPATOLOGICA"):
|
|
assert captions(measures[fhir_id]) == {}
|