CaMa survival: live options and selection routes over the production model

This commit is contained in:
rockdrigo
2026-11-21 12:07:21 -06:00
parent 8b379db28b
commit eeb4c1fbd5
2 changed files with 133 additions and 2 deletions
+57
View File
@@ -77,3 +77,60 @@ def test_it_serves_the_development_record_and_is_a_404_when_the_version_has_none
assert found.status_code == 200 and found.get_json()["desarrollo"] == record
assert found.get_json()["modelo"]["version"] == "3"
assert missing.status_code == 404 and "desarrollo.json" in missing.get_json()["error"]
@pytest.fixture
def live_client(client, monkeypatch):
"""`client` with the live context replaced by stand-ins: a scenario, a model and the labelled members, so the routes run
without MLflow or a cohort build; `live.curves` answers from `holder.answer` or raises what the test sets."""
from models.cama.survival import live
from webapi import model_analysis
holder = SimpleNamespace(calls=0, error=None)
def context(domain, scenario):
return "spec", "model", "labelled", "3"
def curves(spec, model, labelled, query):
holder.calls += 1
if holder.error:
raise live.QueryError(holder.error)
return {"n": 100, "seleccion": query}
monkeypatch.setattr(model_analysis, "_live_context", context)
monkeypatch.setattr(live, "curves", curves)
monkeypatch.setattr(live, "options", lambda labelled, pac_id=None: {"n": 100, "paciente": {"grade": "G2"} if pac_id == 7 else None})
model_analysis._ANSWERS.clear()
client.holder = holder
return client
def test_the_live_figure_is_asked_with_a_selection_and_the_answer_is_kept(live_client):
"""Pins down: `POST /cama/modelo/curvas/consulta` returns the record for the selection with the version that scored it,
and the same selection a second time is answered without computing again. Fails if the answer lacks the version, or every
request recomputes (the figure would take seconds each time a reader goes back to a selection)."""
body = {"filtros": {"grade": ["G3"]}}
first = live_client.post("/cama/modelo/curvas/consulta", json=body)
second = live_client.post("/cama/modelo/curvas/consulta", json=body)
assert first.status_code == 200 and first.get_json()["modelo"]["version"] == "3"
assert first.get_json()["curvas"]["seleccion"] == body and second.get_json() == first.get_json()
assert live_client.holder.calls == 1
def test_a_selection_that_cannot_be_answered_is_a_422_with_the_reason(live_client):
"""Pins down: a selection too small or invalid is a 422 with the message, not a 500 or an empty record. Fails if the
reader sees a server error instead of why the selection says nothing."""
live_client.holder.error = "La selección tiene 3 pacientes"
answer = live_client.post("/cama/modelo/curvas/consulta", json={"filtros": {"er": ["NEGATIVO"]}})
assert answer.status_code == 422 and "3 pacientes" in answer.get_json()["error"]
def test_the_options_carry_the_patients_own_values_when_one_is_named(live_client):
"""Pins down: `GET /cama/modelo/curvas/opciones?paciente=7` carries patient 7's values, and without it none. Fails if the
patient parameter is ignored, which would leave the dashboard unable to offer her subgroup."""
named = live_client.get("/cama/modelo/curvas/opciones?paciente=7").get_json()
plain = live_client.get("/cama/modelo/curvas/opciones").get_json()
assert named["opciones"]["paciente"] == {"grade": "G2"} and plain["opciones"]["paciente"] is None