Files
WebApi/tests/test_date_of_death.py

63 lines
2.9 KiB
Python

"""What this module covers: a patient's date of death, from staging (`paciente.pac_fecha_defuncion`) to silver's
`persona.fecha_defuncion` in CaCu and CaMa, and in CaMa onto her timeline as a `defuncion` fact next to her birth.
Fixtures: `manifest`, dbt's `manifest.json` (`DBT_MANIFEST`; skipped without it); `base_con_defuncion`, the domain's
fixture database (`staging/<domain>.py`) with one patient given a date of death before it's loaded.
"""
import os
from datetime import date
from pathlib import Path
import pytest
from conftest import base_de
from sqlalchemy import text
DEATH = date(2026, 2, 1)
@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)
def with_death(domain: str, tmp_path):
"""The domain's fixture database with its first patient (lowest `pac_id`) dead on `DEATH`, loaded to silver."""
base = base_de(domain, tmp_path)
with base.motor.begin() as con:
con.execute(text("update paciente set pac_fecha_defuncion = :d where pac_id = (select min(pac_id) from paciente)"),
{"d": DEATH})
return base
@pytest.mark.parametrize("domain", ["cacu", "cama"])
def test_silver_persona_carries_the_date_of_death(domain, tmp_path, manifest):
"""Pins down: the dead patient's `persona` has her date of death and everyone else's is null. Fails if the
staging-to-silver copy (`persons.de_derechohabiente`) stops carrying `pac_fecha_defuncion`."""
base = with_death(domain, tmp_path)
base.run("catalogs", "load")
with base.motor.connect() as con:
rows = con.execute(text("select pac_id, fecha_defuncion from persona order by pac_id")).all()
base.motor.dispose()
assert [date.fromisoformat(str(rows[0].fecha_defuncion)[:10])] + [r.fecha_defuncion for r in rows[1:]] \
== [DEATH] + [None] * (len(rows) - 1)
def test_cama_timeline_has_birth_for_everyone_and_death_for_the_dead(tmp_path, manifest):
"""Pins down: every CaMa patient has a `nacimiento` fact and only the dead one a `defuncion` fact, on her date of
death, with class `persona`. Fails if the timeline stops reading `persona` or reads a null date as a death."""
base = with_death("cama", tmp_path)
base.run("catalogs", "load", "views", "event_models")
with base.motor.connect() as con:
people = con.execute(text("select count(*) from persona")).scalar_one()
facts = con.execute(text(
"select tipo, pac_id, fecha, clase from cama_linea_de_tiempo where tipo in ('nacimiento', 'defuncion')")).all()
first = con.execute(text("select min(pac_id) from persona")).scalar_one()
base.motor.dispose()
assert sum(f.tipo == "nacimiento" for f in facts) == people
assert [(f.pac_id, date.fromisoformat(str(f.fecha)[:10]), f.clase) for f in facts if f.tipo == "defuncion"] \
== [(first, DEATH, "persona")]