Live survival: the scenarios of a patient and the figure of one scenario

This commit is contained in:
rockdrigo
2026-11-21 13:14:06 -06:00
parent 3b192207fd
commit a53179aa2e
2 changed files with 58 additions and 24 deletions
+31 -16
View File
@@ -82,7 +82,7 @@ def test_it_serves_the_development_record_and_is_a_404_when_the_version_has_none
@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."""
without MLflow or a cohort build; `live.curves` answers from the stand-in or raises what the test sets."""
from models.cama.survival import live
from webapi import model_analysis
@@ -91,36 +91,51 @@ def live_client(client, monkeypatch):
def context(domain, scenario):
return "spec", "model", "labelled", "3"
def curves(spec, model, labelled, query):
def curves(spec, model, labelled, name):
holder.calls += 1
if holder.error:
raise live.QueryError(holder.error)
return {"n": 100, "seleccion": query}
return {"n": 100, "escenario": name or "Toda la cohorte"}
def patient_scenarios(labelled, pac_id):
if pac_id == 404:
raise live.QueryError("La paciente no está en la cohorte del modelo.")
return ["< 45 años"] if pac_id == 7 else ["Toda la cohorte"]
monkeypatch.setattr(model_analysis, "_live_context", context)
monkeypatch.setattr(live, "curves", curves)
monkeypatch.setattr(live, "patient_scenarios", patient_scenarios)
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)
def test_a_scenarios_figure_is_asked_by_name_and_the_answer_is_kept(live_client):
"""Pins down: `POST /cama/modelo/curvas/consulta` returns the record of the named scenario with the version that scored it,
and the same scenario a second time is answered without computing again. Fails if the answer lacks the version, or every
request recomputes (the figure takes seconds each time a patient's record opens)."""
first = live_client.post("/cama/modelo/curvas/consulta", json={"escenario": "< 45 años"})
second = live_client.post("/cama/modelo/curvas/consulta", json={"escenario": "< 45 años"})
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 first.get_json()["curvas"]["escenario"] == "< 45 años" 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"]}})
def test_a_scenario_that_cannot_be_answered_is_a_422_with_the_reason(live_client):
"""Pins down: an unknown scenario or one too small 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 scenario says nothing."""
live_client.holder.error = "El escenario tiene 3 pacientes"
answer = live_client.post("/cama/modelo/curvas/consulta", json={"escenario": "Estadio IV"})
assert answer.status_code == 422 and "3 pacientes" in answer.get_json()["error"]
def test_the_scenarios_of_a_patient_are_listed_and_a_stranger_is_refused(live_client):
"""Pins down: `GET /cama/modelo/escenarios?paciente=7` lists her scenarios, one in none gets the whole cohort, and a patient
outside the cohort or no patient is a 422. Fails if the patient parameter is ignored, which would draw the same figure for
every patient."""
assert live_client.get("/cama/modelo/escenarios?paciente=7").get_json()["escenarios"] == ["< 45 años"]
assert live_client.get("/cama/modelo/escenarios?paciente=8").get_json()["escenarios"] == ["Toda la cohorte"]
assert live_client.get("/cama/modelo/escenarios?paciente=404").status_code == 422
assert live_client.get("/cama/modelo/escenarios").status_code == 422