Files
WebApi/tests/test_model_analysis.py
T
rockdrigo e2ecaa027c CaMa: individual risk and population curves served, and the cycle estimates with any scenario in production
webapi/fhir/risk.py builds a patient's RiskAssessment for CaCu and CaMa; GET /cama/modelo/curvas serves the production
version's survival curves; the cycle's estimator is generic by scenario; silver persona carries the date of death for CaMa;
the import schema accepts it; the CaMa risk batches can be confirmed and voided.
2026-11-21 02:07:34 -06:00

65 lines
3.0 KiB
Python

"""What this module covers: `GET /cama/modelo/curvas` (`webapi.model_analysis`): it serves the production version's
population record with the version it comes from, and answers 404 while there is no version in production or the
version has no such record.
Fixtures: `client`, the app with the registry's `resolve` and `artifact` replaced by what the test says a production
version holds, so no MLflow is needed; the CaMa survival record is a small stand-in for what a run publishes.
"""
import json
from types import SimpleNamespace
import pytest
RECORD = {"horizons_months": [6, 12, 18], "points": {"COMB": {"S": [0.9, 0.8, 0.7], "low": [0.8, 0.7, 0.6], "high": [0.95, 0.9, 0.8]}}}
@pytest.fixture
def client(monkeypatch):
from flask import Flask
from models.io import registry as registry_module
from webapi.model_analysis import model_analysis_blueprint
app = Flask(__name__)
app.register_blueprint(model_analysis_blueprint("cama", "survival"), url_prefix="/cama")
monkeypatch.setenv("DEV_REVISION", "pruebas")
holder = SimpleNamespace(files={"analisis/curvas.json": json.dumps(RECORD).encode()}, production="3")
def resolve(domain, scenario, version=None):
if holder.production is None:
raise LookupError("no hay una versión en producción")
return SimpleNamespace(version=holder.production, run_id="run-1")
def artifact(domain, scenario, version, path):
if path not in holder.files:
raise LookupError(f"la corrida no tiene el artefacto {path!r}")
return path.rsplit("/", 1)[-1], holder.files[path]
monkeypatch.setattr(registry_module.registry, "resolve", resolve)
monkeypatch.setattr(registry_module.registry, "artifact", artifact)
with app.test_client() as test_client:
test_client.holder = holder
yield test_client
def test_it_serves_the_production_versions_record_and_names_the_version(client):
"""Pins down: the answer carries the record unchanged, the model and version it comes from and the simulated-data
flag. Fails if the record is altered on the way or the answer stops saying which version it is."""
body = client.get("/cama/modelo/curvas").get_json()
assert body["curvas"] == RECORD and body["simulado"] is True
assert body["modelo"] == {"nombre": "cama-survival", "version": "3", "mlflow_run_id": "run-1"}
def test_without_a_version_in_production_or_a_record_it_is_a_404(client):
"""Pins down: no production version, and a version whose run published no `analisis/curvas.json`, are both a 404
with the reason, never an empty record. Fails if either becomes a 200 or a 500."""
client.holder.files.clear()
missing_record = client.get("/cama/modelo/curvas")
client.holder.production = None
no_version = client.get("/cama/modelo/curvas")
assert missing_record.status_code == 404 and "curvas.json" in missing_record.get_json()["error"]
assert no_version.status_code == 404 and "producción" in no_version.get_json()["error"]