"""What this module covers: `webapi.fhir.risk.risk_assessment` naming what the model's number is (`[estimand]` of its scenario's `model.toml`) in the extension `estimando` and `method.text`, and leaving both out for a model whose scenario can't be read. Fixtures: none; the patient's risk is a stand-in with the fields `PatientRisk` has. """ from datetime import date, datetime, timezone from types import SimpleNamespace from webapi.fhir import risk def _risk(modelo_nombre): horizon = SimpleNamespace(horizonte_anios=1.0, probabilidad=0.1, ic_bajo=0.05, ic_alto=0.2) return SimpleNamespace(lote_id=1, estado_lote="final", evaluado_en=datetime(2026, 1, 1, tzinfo=timezone.utc), modelo_nombre=modelo_nombre, modelo_version="2", fecha_indice=date(2025, 1, 1), horizontes=[horizon], desenlace="cin2") def test_the_risk_says_what_its_number_is(): """Pins down: a PIM risk carries its scenario's quantity, outcome and horizons' unit. Fails if the extension's url or keys change, which the WebApp reads, or the scenario stops being found by the registered name.""" found = risk.risk_assessment(7, _risk("cacu-cin2_pim"), True, {}, "cin2_mas") extension = {e["url"]: e["valueString"] for e in found["extension"][0]["extension"]} assert found["extension"][0]["url"] == risk.ESTIMAND and found["method"]["text"] == "Riesgo acumulado" assert extension["horizons_unit"] == "aƱos" and extension["outcome"].startswith("CIN2+") def test_a_model_without_a_scenario_has_no_estimand(): """Pins down: a registered name with no scenario behind it gives the resource without the extension. Fails if a missing scenario breaks the patient's risk.""" found = risk.risk_assessment(7, _risk("cacu-no_existe"), True, {}, "cin2_mas") assert "text" not in found["method"] and not any(e.get("url") == risk.ESTIMAND for e in found.get("extension", [])) def test_a_cacu_risk_names_the_results_of_its_index_cytology(monkeypatch): """Pins down: CaCu's RiskAssessment carries the squamous, glandular and HPV results of the cytology sampled on the risk's index date (the most complete if there are several), which the record words its sentence with; a risk with none that day has no such extension. Fails if the extension's url or keys change, or another day's sample is read.""" from persistance import cacu as repos from webapi.fhir import cacu asked = [] def rows(**filters): asked.append(filters) return [SimpleNamespace(cit_celulas_escamosas="LIEBG", cit_celulas_glandulares=None, cit_vph_resultado=None), SimpleNamespace(cit_celulas_escamosas="LIEBG", cit_celulas_glandulares=None, cit_vph_resultado="POSITIVO")] monkeypatch.setattr(repos.citologia_cervical_repository, "list", rows) found = cacu.risk_assessment(7, _risk("cacu-cin2_pim"), True) extension = next(e for e in found["extension"] if e["url"] == cacu.INDEX_RESULTS) assert {e["url"]: e["valueString"] for e in extension["extension"]} == {"escamosas": "LIEBG", "vph": "POSITIVO"} assert asked[0]["pac_id"] == 7 and asked[0]["cit_fecha_toma"] == date(2025, 1, 1) monkeypatch.setattr(repos.citologia_cervical_repository, "list", lambda **filters: []) without = cacu.risk_assessment(7, _risk("cacu-cin2_pim"), True)["extension"] assert not any(e["url"] == cacu.INDEX_RESULTS for e in without)