Files
WebApi/tests/test_live_estimators.py
edgar 2fe4101982 Cycle: estimate with every scenario in production, one batch per model
The scenarios come from the registry instead of a fixed list, and a cohort can carry several estimators.
2026-11-22 20:34:38 -06:00

63 lines
3.4 KiB
Python

"""The live estimators the recomputation worker hands the cycle (`webapi.orchestration.estimators`,
`webapi.recomputation`).
Fixtures: `base` (conftest) gives a migrated CaCu database with the synthetic staging; the registry and the live
estimators are stubbed so no MLflow store or model is needed. The stub estimator is the cycle tests' `EstimadorFijo`.
"""
import pytest
from sqlalchemy import text
from webapi.orchestration import estimators
from webapi.recomputation import RecomputationQueue
from tests.test_orquestacion_riesgos import URL, EstimadorFijo
def test_with_an_empty_registry_no_domain_has_a_live_estimator():
"""With no version in the registry, neither CaMa's nor TEA's cycle estimates anything. Fails if a domain without
a production model gets an estimator that would then fail resolving a version."""
assert estimators.live_estimators("cama") == {}
assert estimators.live_estimators("tea") == {}
def test_without_a_production_version_cacu_estimates_nothing(monkeypatch):
"""No version in production means no estimator: `estimate` then no-ops instead of failing the whole cycle.
Fails if `live_estimators` offers a scenario the registry does not list as in production."""
from models.io.registry import registry
monkeypatch.setattr(registry, "in_production", lambda domain: [])
assert estimators.live_estimators("cacu") == {}
def test_every_scenario_in_production_scores_its_cohort(monkeypatch):
"""Each scenario the registry has in production gets an estimator on the cohort its `model.toml` names, so two
scenarios on one cohort give that cohort two estimators; a registered name with no scenario in `models` is
skipped. An estimator reports nothing until it has estimated. Fails if the scenarios go back to a fixed list,
if one cohort keeps a single estimator, or if an unknown registered name breaks the cycle."""
from models.dags.training import Scenario
from models.io.registry import registry
monkeypatch.setattr(registry, "in_production", lambda domain: ["cancer_pim", "cin2_pim", "no_such_model"])
live = estimators.live_estimators("cacu")
cohort = Scenario.load("cacu", "cin2_pim").cohort
assert Scenario.load("cacu", "cancer_pim").cohort == cohort
assert list(live) == [cohort]
assert [e.scenario for e in live[cohort]] == ["cancer_pim", "cin2_pim"]
with pytest.raises(RuntimeError):
live[cohort][0].procedencia()
@pytest.mark.parametrize("proyecto", ["cacu"])
def test_the_worker_runs_the_cycle_with_the_live_estimators(base, tmp_path, monkeypatch):
"""After an import the worker's run leaves a preliminary risk batch for every cohort with a live estimator.
Fails if `RecomputationQueue.run` stops passing `live_estimators` to the context, or the batch is written
as anything but `preliminary`."""
monkeypatch.setattr(estimators, "live_estimators", lambda domain: {URL: [EstimadorFijo()]})
result = RecomputationQueue(tmp_path / "indicadores").run("cacu", desde=2025, hasta=2025)
assert result["estado"] == "ok", result
assert [p["paso"] for p in result["pasos"]][-2:] == ["cohorts", "estimate"]
with base.motor.connect() as con:
assert con.execute(text("SELECT COUNT(*) FROM evaluacion_riesgo_lote")).scalar_one() == 1
assert con.execute(text("SELECT MIN(estado) FROM evaluacion_riesgo")).scalar_one() == "preliminary"
assert con.execute(text("SELECT COUNT(*) FROM evaluacion_riesgo")).scalar_one() > 0