8e5a7e3b81
CaCu's timeline now holds the patient's birth and death like CaMa's, so the comparison with the kit's rows leaves them out along with the inferred procedures.
75 lines
3.5 KiB
Python
75 lines
3.5 KiB
Python
"""What this module covers: the timeline `repositories_of` wires into `ctx.repos.silver` is dbt's view
|
|
`cacu_linea_de_tiempo`, the one the models' pipelines, the cohort builds and the measures' strata read: silver's
|
|
facts as the indicator kit's own timeline gave them, plus the procedures dbt infers from them.
|
|
|
|
Fixtures: `base` (from `conftest.py`), the CaCu fixture database, loaded and with dbt's views created
|
|
(`catalogs`, `load`, `event_models`; needs `DBT_MANIFEST`); `kit_rows`, the rows the kit's timeline query gave on
|
|
that data (`fixtures/kit_cacu_timeline.json`, without `conocida_en`, which is each run's load time).
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
from datetime import date, datetime, timedelta, timezone
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
CORTE = date(2025, 12, 31)
|
|
# What dbt adds to the kit's timeline: the procedures it infers and the patient's birth and death, from `persona`.
|
|
ADDED = {"colposcopia", "tratamiento", "citologia_de_vigilancia", "diagnostico", "antecedente", "orden_biopsia",
|
|
"nacimiento", "defuncion"}
|
|
KIT_ROWS = Path(__file__).parent / "fixtures" / "kit_cacu_timeline.json"
|
|
|
|
|
|
@pytest.fixture
|
|
def proyecto():
|
|
return "cacu"
|
|
|
|
|
|
@pytest.fixture
|
|
def loaded(base):
|
|
if not os.environ.get("DBT_MANIFEST"):
|
|
pytest.skip("DBT_MANIFEST isn't set: `dbt compile` in Transformation writes it")
|
|
base.run("catalogs", "load", "event_models")
|
|
return base
|
|
|
|
|
|
@pytest.fixture
|
|
def kit_rows():
|
|
return json.loads(KIT_ROWS.read_text(encoding="utf-8"))
|
|
|
|
|
|
def as_kit(rows) -> list[dict]:
|
|
"""Rows as the snapshot keeps them: text values, no load time, without the rows dbt adds."""
|
|
return [{k: None if v is None else str(v) for k, v in row.items() if k != "conocida_en"}
|
|
for row in rows if row["tipo"] not in ADDED]
|
|
|
|
|
|
def test_the_timeline_is_the_kit_s_plus_the_inferred_procedures(loaded, kit_rows):
|
|
"""Pins down: the wired repository reads dbt's view, which holds every row the kit's timeline held, in its
|
|
order and with a date as `fecha`, plus the inferred procedures and the patient's birth and death. Fails if the
|
|
orchestration binds another query, or the view stops giving one of the kit's rows."""
|
|
rows = loaded.ctx.repos.silver.timeline.read(until=CORTE)
|
|
assert as_kit(rows) == kit_rows
|
|
assert {row["tipo"] for row in rows} & ADDED
|
|
assert all(isinstance(row["fecha"], date) for row in rows)
|
|
|
|
|
|
def test_the_patient_filter_keeps_only_those_patients(loaded, kit_rows):
|
|
"""Pins down: `patients` keeps those patients' rows, all of them. Fails if the filter is dropped or misapplied."""
|
|
patients = sorted({int(row["pac_id"]) for row in kit_rows})[:2]
|
|
rows = loaded.ctx.repos.silver.timeline.read(patients=patients, until=CORTE)
|
|
assert {row["pac_id"] for row in rows} == set(patients)
|
|
assert as_kit(rows) == [row for row in kit_rows if int(row["pac_id"]) in patients]
|
|
|
|
|
|
def test_known_until_cuts_at_what_was_loaded(loaded):
|
|
"""Pins down: `known_until` compares with the load's time as a datetime: before the load nothing is known,
|
|
after it everything is. Fails if the cut is ignored, applied to the clinical date, or compared as text."""
|
|
timeline = loaded.ctx.repos.silver.timeline
|
|
everything = timeline.read(until=CORTE)
|
|
known = {row["conocida_en"] for row in everything}
|
|
assert all(isinstance(at, datetime) and at.tzinfo is not None for at in known)
|
|
assert timeline.read(until=CORTE, known_until=min(known) - timedelta(seconds=1)) == []
|
|
assert timeline.read(until=CORTE, known_until=datetime.now(timezone.utc) + timedelta(days=1)) == everything
|