"""What this module covers: `models.io.storage`: a kit build read from the database and the same build read from its exported file give the dataflow the same frame, each with its own reference, and a build that doesn't exist is named. Fixtures: `frame`, a long frame of two members with one event after the cut-off; `kit_stand_ins` (conftest). """ from datetime import date, datetime, timezone import pandas as pd import pyarrow as pa import pyarrow.parquet as pq import pytest from models.io.contracts import COHORT_SCHEMA from models.io.storage import CohortFile, KitBuild from models.steps.cohort_file import cohort_events CUTOFF = date(2025, 12, 31) def _row(pac_id, index, fecha, fuente_id): return {"pac_id": pac_id, "fecha_indice": index, "fecha": fecha, "conocida_en": datetime(2026, 1, 1, tzinfo=timezone.utc), "clase": "medicion", "tipo": "bethesda", "codigo": "bethesda", "valor_codigo": "NORMAL", "valor_numerico": None, "fuente_tabla": "t", "fuente_id": fuente_id} @pytest.fixture def frame(): """Members 10 and 20; member 10 has an event after the cut-off, which no source may return.""" return pd.DataFrame([_row(10, date(2025, 1, 1), date(2025, 1, 1), "a"), _row(20, date(2025, 2, 1), date(2025, 2, 1), "b"), _row(10, date(2025, 1, 1), date(2026, 3, 1), "late")], columns=COHORT_SCHEMA.names) def test_the_database_and_the_file_give_the_same_frame(frame, kit_stand_ins, tmp_path): """Pins down that the reading is interchangeable: the kit build read through the repositories and its exported file are the same frame, so the dataflow can't tell them apart. Fails if one source cuts, orders or types the rows differently.""" cohorts, timeline = kit_stand_ins(frame, 3, CUTOFF) from_database = KitBuild("cacu", 3, cohorts=cohorts, timeline=timeline).frame() population = frame[["pac_id", "fecha_indice"]].drop_duplicates() exported = cohort_events(kit_stand_ins(frame, 3, CUTOFF)[1].read(), population, CUTOFF) path = tmp_path / "3.parquet" pq.write_table(pa.Table.from_pandas(exported, schema=COHORT_SCHEMA, preserve_index=False).replace_schema_metadata( {"url": "cacu/cohorte_vigilancia", "armado_id": "3", "datos_as_of": CUTOFF.isoformat()}), path) pd.testing.assert_frame_equal(from_database, CohortFile(path).frame()) assert "late" not in from_database["fuente_id"].tolist() def test_a_kit_build_reads_its_members_up_to_its_cutoffs(frame, kit_stand_ins): """Pins down: the timeline is asked for the build's members, cut at its clinical date and, since this build has no `conocida_hasta`, at no load cut-off (the lineage records when it was read); the reference names the build without a file hash. Fails if the read takes everyone, loses the clinical cut-off, invents a load cut-off, or the reference claims a hash it doesn't have.""" cohorts, timeline = kit_stand_ins(frame, 3, CUTOFF) source = KitBuild("cacu", 3, cohorts=cohorts, timeline=timeline) source.frame() assert timeline.reads[-1]["patients"] == [10, 20] and timeline.reads[-1]["until"] == CUTOFF assert timeline.reads[-1]["known_until"] is None and source.lineage()["read_at"] assert source.lineage()["datos_as_of"] == "2025-12-31" and source.armado_id == 3 assert source.reference() == {"cohort.url": "cacu/cohorte_vigilancia", "cohort.source": "kit", "cohort.build": "3"} def test_a_missing_kit_build_is_named(frame, kit_stand_ins): """Pins down: asking for a build the database doesn't have fails with its number before any read. Fails if it reads an empty frame instead.""" cohorts, timeline = kit_stand_ins(frame, 3, CUTOFF) with pytest.raises(LookupError, match="kit build 9"): KitBuild("cacu", 9, cohorts=cohorts, timeline=timeline).frame()