"""What this module covers: the timeline repository that `repositorios_de` wires into `ctx.repos.plata` reads the same rows as the kit's own query, in each project. Fixtures: `base` (from `conftest.py`), a project database for each of CaCu, CaMa and TEA; each test loads its catalogs and data with `base.correr("catalogos", "cargar")`. `kit_rows` reads the same timeline straight through the kit (`filas_de_linea`) to compare against. """ from datetime import date, datetime, timedelta, timezone from importlib import import_module from indicadores_kit.oro.cohortes.armado import filas_de_linea from indicadores_kit.tablas import tablas_de from sqlmodel import Session CORTE = date(2025, 12, 31) def kit_rows(base, **filters): """The project's timeline read through the kit with `filters` (the kit's Spanish keywords).""" fuentes = import_module(f"indicadores_kit.{base.proyecto}.linea_de_tiempo").FUENTES_PLATA with Session(base.motor) as session: return filas_de_linea(session, tablas_de(base.proyecto), fuentes, **filters) def test_the_whole_timeline_matches_the_kit(base): """Pins down: with only a clinical cut-off, the wired repository returns exactly the kit's rows, and there are some. Fails if the orchestration binds another query, another project's tables or sources, or `until` stops reaching the kit as `hasta`. """ base.correr("catalogos", "cargar") rows = base.ctx.repos.plata.timeline.read(until=CORTE) assert rows assert rows == kit_rows(base, hasta=CORTE) def test_the_patient_filter_matches_the_kit(base): """Pins down: `patients` reaches the kit as `pacientes`: only those patients come back, with the kit's rows. Fails if the patient filter is dropped or renamed on the way to the kit's query. """ base.correr("catalogos", "cargar") patients = sorted({row["pac_id"] for row in kit_rows(base)})[:2] rows = base.ctx.repos.plata.timeline.read(patients=patients, until=CORTE) assert {row["pac_id"] for row in rows} == set(patients) assert rows == kit_rows(base, pacientes=patients, hasta=CORTE) def test_known_until_cuts_at_what_was_loaded(base): """Pins down: `known_until` reaches the kit as `conocida_hasta`: before the load nothing is known, after it everything is, the same as reading without the cut. Fails if the load cut-off is ignored, or applied to the clinical date instead of the load time. """ before = datetime.now(timezone.utc) - timedelta(days=1) base.correr("catalogos", "cargar") after = datetime.now(timezone.utc) + timedelta(days=1) timeline = base.ctx.repos.plata.timeline assert timeline.read(until=CORTE, known_until=before) == [] assert timeline.read(until=CORTE, known_until=after) == kit_rows(base, hasta=CORTE, conocida_hasta=after) assert timeline.read(until=CORTE, known_until=after) == timeline.read(until=CORTE)