88 lines
4.4 KiB
Python
88 lines
4.4 KiB
Python
"""What this module covers: CaMa's tumour characteristics read from reports. dbt's `cama_estratificadores` (stage
|
|
group, early or advanced stage, T by size) as timeline facts; that no measure is served by clinical stage or tumour
|
|
size; `CAMA_BIRADS_CANCER_INVASOR` without tumour strata; and the labels a timeline stratum's values carry into FHIR.
|
|
|
|
Fixtures: `manifest`, dbt's `manifest.json` (`DBT_MANIFEST`; skipped without it); `base`, the CaMa fixture database
|
|
(`staging/cama.py`) through the cycle up to `event_models`. Only H-003 has an invasive cancer: biopsy reported on
|
|
2025-07-28 from her BI-RADS 4A mammogram (14 mm), and a note of 2025-08-04 with stage IIA.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
from dataclasses import replace
|
|
from datetime import date
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from conftest import base_de
|
|
from sqlalchemy import text
|
|
from webapi.compositions import answer_composition, composition_definitions
|
|
from webapi.fhir.measures import _label
|
|
from webapi.metrics import Question
|
|
|
|
BIRADS = "cama/CAMA_BIRADS_CANCER_INVASOR"
|
|
UNTIL_MODELS = ("catalogs", "load", "views", "registry", "definitions", "event_models")
|
|
|
|
|
|
@pytest.fixture
|
|
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
|
|
def base(tmp_path, sin_supresion, manifest):
|
|
base = base_de("cama", tmp_path)
|
|
base.run(*UNTIL_MODELS, parameters=sin_supresion, allow_undecided=True)
|
|
yield replace(base, ctx=replace(base.ctx, parameters=sin_supresion, allow_undecided=True))
|
|
base.motor.dispose()
|
|
|
|
|
|
def answered(base, manifest, url: str, period: str, event_cutoff: date | None = None):
|
|
d = base.dominio
|
|
definition = composition_definitions(manifest)[url]
|
|
return answer_composition("cama", definition, Question(url, period, event_cutoff=event_cutoff),
|
|
parameters=d.def_parametro_repository, periods=d.dim_periodo_repository,
|
|
history=d.history_repository, queries=d.dbt_query_repository, allow_undecided=True)
|
|
|
|
|
|
def stratifiers(base, manifest) -> set[str]:
|
|
"""The stratifiers' `tipo`s, from the view built here: no served measure reads it, so the cycle doesn't."""
|
|
node = next(n for n in json.loads(manifest.read_text(encoding="utf-8"))["nodes"].values()
|
|
if n["resource_type"] == "model" and n["name"] == "cama_estratificadores")
|
|
with base.motor.connect() as con:
|
|
return set(con.execute(text(f"select tipo from ({node['compiled_code']}) e")).scalars())
|
|
|
|
|
|
def test_the_stratifiers_come_from_h003_s_reports(base, manifest):
|
|
"""Pins down: the view derives the stage's group and early stage from the note and T1 from the 14 mm lesion, and
|
|
nothing else: there's no molecular subtype. Fails if a rule in `vars.cama.estratos` or a timeline `tipo` it reads
|
|
changes, or a subtype comes back."""
|
|
assert stratifiers(base, manifest) == {"estadio_ii", "etapa_temprana", "tamano_t1"}
|
|
|
|
|
|
def test_no_measure_is_served_by_stage_or_tumour_size(manifest):
|
|
"""Pins down: clinical stage and tumour size aren't served as measures; they come from reports read by text, not
|
|
yet validated. Fails if a measure on them is published again."""
|
|
metrics = json.loads(manifest.read_text(encoding="utf-8"))["metrics"].values()
|
|
urls = {m["config"].get("meta", {}).get("measure_url") for m in metrics} - {None}
|
|
assert BIRADS in urls and BIRADS in composition_definitions(manifest)
|
|
assert not {u for u in urls if "ESTADIO" in u or "TAMANO" in u}
|
|
|
|
|
|
def test_the_birads_of_the_invasive_cancers_has_no_tumour_strata(base, manifest):
|
|
"""Pins down: the BI-RADS of the mammogram before an invasive cancer isn't broken down by the tumour's
|
|
characteristics, which describe the cancer and not that mammogram. Fails if tumour strata come back."""
|
|
result = answered(base, manifest, BIRADS, "2025-S2")
|
|
assert (result.categories, result.strata) == ({"B4A": 1}, {})
|
|
|
|
|
|
def test_a_timeline_stratum_s_values_carry_their_labels():
|
|
"""Pins down: FHIR labels a timeline stratum's value from its entry in `valores`, and its `resto` from
|
|
`resto_etiqueta`. Fails if `_label` reads labels only from a map."""
|
|
spec = {"valores": [{"valor": "II", "etiqueta": "Estadio II", "si": []}], "resto": "SIN_DATO",
|
|
"resto_etiqueta": "Sin dato"}
|
|
assert (_label(spec, "II"), _label(spec, "SIN_DATO"), _label(spec, "IV")) == ("Estadio II", "Sin dato", None)
|