"""What this module covers: listing and describing a cohort's builds (`models.io.storage.list_builds`, `describe_build`) and the `models cohorts list` and `cohorts show` commands. Fixtures: `root`, a cohorts folder with dbt builds 1 and 2 and kit build 7's file; `kit`, a stand-in of the cohort repository with kit builds 7 (exported) and 8 (not exported). """ import json from datetime import date, datetime, timezone from types import SimpleNamespace import pyarrow as pa import pyarrow.parquet as pq import pytest from models.__main__ import main from models.io.storage import cohort_path, dbt_build_path, describe_build, list_builds URL = "cacu/cohorte_vigilancia" ATTRITION = [{"step": "all_patients", "n": 3, "excluded": None}, {"step": "indexed", "n": 2, "excluded": 1}] def _file(path, **metadata): path.parent.mkdir(parents=True, exist_ok=True) pq.write_table(pa.table({"pac_id": [1]}).replace_schema_metadata({k: str(v) for k, v in metadata.items()}), path) @pytest.fixture def root(tmp_path): for number in (1, 2): _file(dbt_build_path(tmp_path, URL, number), producer="dbt", build=number, datos_as_of="2026-12-31", n_members=2, built_at=f"2026-11-0{number}T00:00:00+00:00", rules=json.dumps(["prior_event"]), parametros=json.dumps({"cutoff": "2026-12-31"}), attrition=json.dumps(ATTRITION)) _file(cohort_path(tmp_path, URL, 7), producer="kit", armado_id=7, datos_as_of="2026-12-31", n_members=2) return tmp_path def _kit_build(armado_id): return SimpleNamespace(armado_id=armado_id, url=URL, version="0.2.0", datos_as_of=date(2026, 12, 31), n_miembros=2, huella="h", armado_en=datetime(2026, 10, armado_id, tzinfo=timezone.utc)) @pytest.fixture def kit(): builds = {8: _kit_build(8), 7: _kit_build(7)} return SimpleNamespace(builds=lambda url: list(builds.values()) if url == URL else [], build=builds.get) def test_both_sources_are_listed_newest_first_with_their_files(root, kit): """Pins down: dbt's builds come from their files, newest first, and the kit's from the repository, each saying whether its file exists. Fails if a source is dropped, the order is by name, or an unexported build shows a file.""" rows = list_builds(root, URL, kit) assert [(r["source"], r["build"]) for r in rows] == [("dbt", 2), ("dbt", 1), ("kit", 8), ("kit", 7)] assert rows[2]["file"] is None and rows[3]["file"].endswith("7.parquet") assert rows[0]["created"] == "2026-11-02T00:00:00+00:00" def test_without_the_repository_only_files_are_read(root): """Pins down: listing dbt's builds needs no database. Fails if `list_builds` reaches for the kit's builds anyway.""" assert [r["source"] for r in list_builds(root, URL)] == ["dbt", "dbt"] def test_a_dbt_build_is_described_with_its_lineage_decoded(root): """Pins down: `describe_build` gives the file, its hash, and the parameters, rules and attrition as JSON values, not strings. Fails if the hash is missing or the lineage stays encoded.""" described = describe_build(root, URL, "dbt", 2) assert len(described["file_sha256"]) == 64 assert described["lineage"]["attrition"] == ATTRITION and described["lineage"]["rules"] == ["prior_event"] assert described["lineage"]["parametros"] == {"cutoff": "2026-12-31"} def test_an_unexported_kit_build_is_described_from_the_database(root, kit): """Pins down: a kit build without a file still has its lineage, from the repository; an unknown build is a `LookupError`. Fails if an unexported build is reported as missing, or a missing one as empty.""" described = describe_build(root, URL, "kit", 8, kit) assert described["file"] is None and described["lineage"]["n_members"] == "2" with pytest.raises(LookupError): describe_build(root, URL, "kit", 9, kit) with pytest.raises(LookupError): describe_build(root, URL, "dbt", 9) def test_the_commands_print_the_dbt_builds(root, monkeypatch, capsys): """Pins down: `cohorts list --source dbt` and `cohorts show` work from the files alone. Fails if either opens the database for dbt's builds, or `show` prints something other than JSON.""" monkeypatch.setenv("COHORTS_DIR", str(root)) assert main(["cohorts", "list", "cacu", "cohorte_vigilancia", "--source", "dbt"]) == 0 listed = capsys.readouterr().out assert "dbt/2.parquet" in listed and "dbt/1.parquet" in listed assert main(["cohorts", "show", "cacu", "cohorte_vigilancia", "--build", "1"]) == 0 assert json.loads(capsys.readouterr().out)["build"] == 1