89 lines
4.8 KiB
Python
89 lines
4.8 KiB
Python
"""What this module covers: the group each CaMa patient's predicted risks are read in, inferred by dbt
|
|
(`cama_procedimientos_inferidos`, rules in `cama.escenarios`) and carried by her timeline with the kind of evidence
|
|
that started it: follow-up from a benign biopsy with no malignant diagnosis before, post-diagnosis from a note with a
|
|
malignant breast tumour ICD-10 or an invasive biopsy, post-treatment from a later such note by a treating specialty.
|
|
|
|
Fixtures: `manifest`, dbt's `manifest.json` (`DBT_MANIFEST`; skipped without it); `base`, the CaMa fixture database
|
|
(`staging/cama.py`) loaded, with dbt's views. Its records: H-003 an invasive biopsy (2025-07-28) and a C50.4 note by
|
|
oncological surgery (08-04); H-103 (C50.9, 07-20) and H-102 (C50.9, 09-30) one such note each; H-001 an in situ
|
|
carcinoma note (D05.1, by family medicine, 03-01) and a benign biopsy (04-05); H-104 a C50 note without a date.
|
|
"""
|
|
|
|
import os
|
|
from datetime import date
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from conftest import base_de
|
|
from sqlalchemy import text
|
|
|
|
|
|
@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, manifest):
|
|
base = base_de("cama", tmp_path)
|
|
base.run("catalogs", "load", "views", "event_models")
|
|
yield base
|
|
base.motor.dispose()
|
|
|
|
|
|
def scenarios(base) -> dict[str, set[tuple[str, date, str]]]:
|
|
"""Patient (hospital id) -> the group facts of her timeline, (tipo, fecha, evidence)."""
|
|
with base.motor.connect() as con:
|
|
rows = con.execute(text(
|
|
"select p.pac_id_hospital, l.tipo, l.fecha, l.valor_codigo from cama_linea_de_tiempo l "
|
|
"join paciente p on p.pac_id = l.pac_id where l.tipo like 'escenario%'")).all()
|
|
found: dict[str, set[tuple[str, date, str]]] = {}
|
|
for r in rows:
|
|
found.setdefault(r.pac_id_hospital, set()).add(
|
|
(r.tipo, date.fromisoformat(str(r.fecha)[:10]), r.valor_codigo))
|
|
return found
|
|
|
|
|
|
def test_each_patient_s_group_starts_at_its_first_evidence(base):
|
|
"""Pins down: an invasive biopsy or a dated C50 note, whoever wrote it, starts post-diagnosis; a C50 note by oncological
|
|
surgery after that diagnosis starts post-treatment, one on the diagnosis' own day doesn't (H-103, H-102); an in
|
|
situ carcinoma (D05) is no diagnosis, so H-001's benign biopsy starts her follow-up; a note without a date starts
|
|
nothing. Fails if `cama.escenarios` changes an evidence list, a grade or a code, or treatment stops requiring a
|
|
later date than the diagnosis."""
|
|
assert scenarios(base) == {
|
|
"H-003": {("escenario_postdiagnostico", date(2025, 7, 28), "biopsia"),
|
|
("escenario_postratamiento", date(2025, 8, 4), "nota")},
|
|
"H-103": {("escenario_postdiagnostico", date(2025, 7, 20), "cie")},
|
|
"H-102": {("escenario_postdiagnostico", date(2025, 9, 30), "cie")},
|
|
"H-001": {("escenario_seguimiento", date(2025, 4, 5), "biopsia")},
|
|
}
|
|
|
|
|
|
def test_a_benign_biopsy_after_a_malignant_diagnosis_starts_no_follow_up(base):
|
|
"""Pins down: a benign biopsy reported after a malignant diagnosis doesn't start a follow-up, and a C50 note by a
|
|
specialty that doesn't treat (family medicine) is a diagnosis, not a treatment. Fails if the follow-up rule stops
|
|
looking at earlier diagnoses or `tratamiento_especialidades` stops filtering."""
|
|
with base.motor.begin() as con:
|
|
con.execute(text(
|
|
"update observacion_clinica set valor_texto = 'C509' where resultado_id = 'nota_medica.diagnostico' and "
|
|
"pac_id = (select pac_id from paciente where pac_id_hospital = 'H-001')"))
|
|
assert scenarios(base)["H-001"] == {("escenario_postdiagnostico", date(2025, 3, 1), "cie")}
|
|
|
|
|
|
def test_a_diagnosis_after_a_follow_up_keeps_both_groups(base):
|
|
"""Pins down: a patient in follow-up whose malignant diagnosis comes later keeps both facts, so her timeline reads
|
|
the progression: follow-up from the benign biopsy, post-diagnosis from the later note. Fails if a later diagnosis
|
|
erases the follow-up that preceded it."""
|
|
with base.motor.begin() as con:
|
|
con.execute(text(
|
|
"update observacion_clinica set valor_texto = 'C509' where resultado_id = 'nota_medica.diagnostico' and "
|
|
"pac_id = (select pac_id from paciente where pac_id_hospital = 'H-001')"))
|
|
con.execute(text(
|
|
"update procedimiento set fecha = '2025-06-01' where estudio_id = 'nota_medica' and pac_id = "
|
|
"(select pac_id from paciente where pac_id_hospital = 'H-001')"))
|
|
assert scenarios(base)["H-001"] == {("escenario_seguimiento", date(2025, 4, 5), "biopsia"),
|
|
("escenario_postdiagnostico", date(2025, 6, 1), "cie")}
|