"""What this module covers: the cycle's `relate_ados2` step (TEA's ADOS-2 association, after the EEG analysis and before the silver load) and how the cycle gets the association (`webapi.orchestration.eeg.live_ados2_relation`). Fixtures: none; the graph is built from `webapi.orchestration.cycle` and the association is a stand-in. """ from types import SimpleNamespace import pytest from hamilton import driver from webapi.orchestration import cycle, run, steps_of from webapi.orchestration.context import Context from webapi.orchestration.eeg import RELATION_ENV, live_ados2_relation @pytest.mark.parametrize("proyecto", ["cacu", "cama", "tea"]) def test_the_association_runs_after_the_analysis_and_before_the_load(proyecto): """Pins down: `relate_ados2` waits for `analyze_eeg` only, and `load` waits for it, so the same cycle's gold views show the association fitted on that cycle's analysis. Fails if the association runs before the analysis (it would fit on the previous one) or after the load.""" dr = driver.Builder().with_modules(cycle).with_config({"with_cohorts": proyecto == "cacu"}).build() deps = {n.name: {d.name for d in n.dependencies} - {"ctx"} for n in dr.graph.get_nodes() if n.name != "ctx"} assert deps["relate_ados2"] == {"analyze_eeg"} assert "relate_ados2" in deps["load"] order = steps_of(proyecto) assert order.index("analyze_eeg") < order.index("relate_ados2") < order.index("load") def test_the_step_returns_the_associations_summary_or_nothing(): """Pins down: without an association the step is a no-op, and with one it runs it once and returns its summary as a dict. Fails if the step runs an association nobody configured, or stops reporting what it wrote.""" assert [(p.name, p.summary) for p in run(Context("cacu", 2025, 2025), only=["relate_ados2"])] == [ ("relate_ados2", {})] calls = [] def relation(): calls.append(1) from models.ados2_relation import Summary return Summary(version="tea-20261121T120000-abcd1234", level="domain", children=3, associations=2, scored=6) pasos = run(Context("tea", 2025, 2025, ados2_relation=relation, repos=SimpleNamespace()), only=["relate_ados2"]) assert calls == [1] assert pasos[0].summary["version"] == "tea-20261121T120000-abcd1234" and pasos[0].summary["scored"] == 6 def test_only_tea_has_the_association_and_it_can_be_turned_off(monkeypatch): """Pins down: TEA's cycle gets `models.ados2_relation.relate`, the other domains none, `ADOS2_RELATION=off` leaves it out and an unknown value is refused. Fails if another domain starts fitting it, `off` stops working, or a mistyped value silently counts as on.""" from models.ados2_relation import relate monkeypatch.delenv(RELATION_ENV, raising=False) assert live_ados2_relation("tea") is relate assert live_ados2_relation("cama") is None monkeypatch.setenv(RELATION_ENV, "off") assert live_ados2_relation("tea") is None monkeypatch.setenv(RELATION_ENV, "maybe") with pytest.raises(ValueError, match=RELATION_ENV): live_ados2_relation("tea")