"""Las dependencias entre los pasos de cada proyecto, como las lee Hamilton de los parámetros.""" import json import pytest from hamilton import driver from webapi.orchestration import cycle, steps_of def dependencias(proyecto: str) -> dict[str, set[str]]: dr = driver.Builder().with_modules(cycle).with_config({"with_cohorts": proyecto == "cacu"}).build() return {n.name: {d.name for d in n.dependencies} - {"ctx"} for n in dr.graph.get_nodes() if n.name != "ctx"} def antes(deps: dict[str, set[str]], paso: str) -> set[str]: """Todos los pasos que corren antes de `paso`.""" previos = set() pending = list(deps[paso]) while pending: anterior = pending.pop() if anterior not in previos: previos.add(anterior) pending.extend(deps[anterior]) return previos @pytest.mark.parametrize("proyecto", ["cacu", "cama", "tea"]) def test_cada_paso_corre_despues_de_lo_que_necesita(proyecto): deps = dependencias(proyecto) assert deps["migrate"] == set() assert {"migrate", "catalogs"} <= antes(deps, "load") # decisión 6 de §9 assert {"load"} <= antes(deps, "event_models") assert {"registry"} <= antes(deps, "observe") assert {"load", "catalogs"} <= antes(deps, "check_silver") def test_las_cohortes_son_las_del_manifiesto_de_dbt(tmp_path, monkeypatch): """Pins down: a project builds cohorts and estimates only if dbt's manifest has a cohort of it (`meta.cohort` of a population query), today only CaCu's; without a manifest no project does. Fails if the cycle reads cohort definitions from anywhere else than dbt.""" manifest = tmp_path / "manifest.json" nodo = {"config": {"meta": {"cohort": "cacu/cohorte_vigilancia", "kind": "population"}}} manifest.write_text(json.dumps({"nodes": {"model.p.cacu_surveillance_all_patients": nodo}})) assert {"cohorts", "estimate"} <= set(steps_of("cacu", manifest)) for proyecto in ("cama", "tea"): assert not {"cohorts", "estimate"} & set(steps_of(proyecto, manifest)) monkeypatch.delenv("DBT_MANIFEST", raising=False) assert not {"cohorts", "estimate"} & set(steps_of("cacu"))