From 9461dfbd202a1660f0352065f541d54c4f152cd6 Mon Sep 17 00:00:00 2001 From: edgar Date: Sat, 21 Nov 2026 19:35:44 -0600 Subject: [PATCH] test(cacu): cover the two times to treatment The CaCu catalog holds nine measures. A fixture adds H-004's colposcopy and treatment on top of the shared data, and a test pins the days of both intervals with a 60-day horizon. --- tests/test_cacu_measure_metadata.py | 3 +- tests/test_fhir_measures.py | 2 +- tests/test_kpis_route.py | 4 +-- tests/test_metrics_on_request.py | 44 ++++++++++++++++++++++++++++- tests/test_orquestacion_oro.py | 6 ++-- tests/test_reportes.py | 3 +- 6 files changed, 53 insertions(+), 9 deletions(-) diff --git a/tests/test_cacu_measure_metadata.py b/tests/test_cacu_measure_metadata.py index 19a50d4..f7ec225 100644 --- a/tests/test_cacu_measure_metadata.py +++ b/tests/test_cacu_measure_metadata.py @@ -70,12 +70,13 @@ def groups(measure: dict) -> dict[str, str]: def test_each_contract_publishes_the_figures_the_page_can_draw(measures): - """Pins down: the two distributions offer the donut and the bars and the two durations their box plot; the C1 + """Pins down: the two distributions offer the donut and the bars and the four durations their box plot; the C1 proportions and the risk bands none, because no figure is approved for them yet. Fails if a figure is declared that the page has no builder for, or one is dropped.""" assert {i: figures(m) for i, m in measures.items()} == { "CACU-EU7": ["fig_c2_donas", "fig_c2_barras"], "CACU-CONFIRMACION-CITOLOGIA": ["fig_c2_donas", "fig_c2_barras"], "CACU-EU-TIEMPO-REPORTE": ["fig_c3r_caja"], "CACU-DIAS-TOMA-DIAGNOSTICO": ["fig_c3r_caja"], + "CACU-DIAS-TOMA-TRATAMIENTO": ["fig_c3r_caja"], "CACU-DIAS-DIAGNOSTICO-TRATAMIENTO": ["fig_c3r_caja"], "CACU-EU13": [], "IND1": [], "CACU-RIESGO-BANDA": []} diff --git a/tests/test_fhir_measures.py b/tests/test_fhir_measures.py index 21f3542..cfc97a5 100644 --- a/tests/test_fhir_measures.py +++ b/tests/test_fhir_measures.py @@ -45,7 +45,7 @@ def test_every_measure_and_one_report_of_each_are_valid_fhir(domain_client): with strata included. Fails if the serialization of a scoring, a stratifier or a population breaks FHIR's shape.""" domain, client = domain_client measures = valid(client.get(f"/{domain}/fhir/Measure").get_json()) - assert measures["total"] == {"cacu": 7, "cama": 11}[domain] + assert measures["total"] == {"cacu": 9, "cama": 11}[domain] for item in measures["entry"]: response = client.get(f"/{domain}/fhir/Measure/{item['resource']['id']}/$evaluate-measure?periodo=2025-S1") assert response.status_code in (200, 409), response.get_json() diff --git a/tests/test_kpis_route.py b/tests/test_kpis_route.py index 5b705f5..3fe2b26 100644 --- a/tests/test_kpis_route.py +++ b/tests/test_kpis_route.py @@ -80,8 +80,8 @@ def test_the_list_names_what_is_answered_on_request(client): listed = {m["id"] for m in client.get("/cacu/indicadores").get_json()} assert listed == {IND1.split("/")[1], EU13.split("/")[1], EU7.split("/")[1], "CACU_EU_TIEMPO_REPORTE", - "CACU_DIAS_TOMA_DIAGNOSTICO", "CACU_CONFIRMACION_CITOLOGIA", - "CACU_RIESGO_BANDA"} + "CACU_DIAS_TOMA_DIAGNOSTICO", "CACU_DIAS_TOMA_TRATAMIENTO", "CACU_DIAS_DIAGNOSTICO_TRATAMIENTO", + "CACU_CONFIRMACION_CITOLOGIA", "CACU_RIESGO_BANDA"} def test_a_duration_comes_as_a_report_with_its_median_and_quartiles(client): diff --git a/tests/test_metrics_on_request.py b/tests/test_metrics_on_request.py index c1184fe..7d30402 100644 --- a/tests/test_metrics_on_request.py +++ b/tests/test_metrics_on_request.py @@ -19,7 +19,7 @@ from sqlalchemy import text from sqlmodel import Session, select from conftest import base_de -from core.cacu.models import BiopsiaCervical, CitologiaCervical +from core.cacu.models import BiopsiaCervical, CitologiaCervical, NotaMedica, Paciente from webapi.compositions import answer_composition, composition_definitions, composition_report from webapi.metrics import INTERVAL_HIGH, INTERVAL_LOW, Counts, Question, answer, measure_definitions, measure_report from webapi.orchestration import run @@ -47,6 +47,28 @@ def base(tmp_path, sin_supresion, manifest): base.motor.dispose() +def _con_tratamiento(sesion: Session) -> None: + """The fixture data and, for H-004, her colposcopy (August 20) and her treatment (August 28), after the + valuation visit of August 10: the timeline then holds a treatment to end an interval at.""" + from staging.cacu import COLPOSCOPIA, FAMILIAR, insertar + + insertar(sesion) + paciente = sesion.exec(select(Paciente).where(Paciente.pac_id_hospital == "H-004")).one() + sesion.add_all(NotaMedica(pac_id=paciente.pac_id, pac_id_hospital="H-004", nm_id_hospital=f"NM-9{n}", nm_fecha=fecha, + nm_cie_codigo=cie, nm_especialidad=especialidad) + for n, (fecha, cie, especialidad) in enumerate( + [(date(2025, 8, 20), "R87.6", COLPOSCOPIA), (date(2025, 8, 28), "D06.9", FAMILIAR)], start=1)) + sesion.commit() + + +@pytest.fixture +def base_con_tratamiento(tmp_path, sin_supresion, manifest): + base = base_de("cacu", tmp_path, datos=_con_tratamiento) + base.run(*HASTA_MODELOS, parameters=sin_supresion, allow_undecided=True) + yield replace(base, ctx=replace(base.ctx, parameters=sin_supresion, allow_undecided=True)) + base.motor.dispose() + + @pytest.fixture def kit(): return json.loads(SNAPSHOT.read_text(encoding="utf-8")) @@ -467,6 +489,26 @@ def test_a_sample_waiting_for_its_diagnosis_within_the_horizon_is_not_measured_y assert (result.initial, result.n, result.median, result.excluded) == (1, 1, 40.0, 0) +DIAS_TOMA_TRATAMIENTO = "cacu/CACU_DIAS_TOMA_TRATAMIENTO" +DIAS_DIAGNOSTICO_TRATAMIENTO = "cacu/CACU_DIAS_DIAGNOSTICO_TRATAMIENTO" + + +@pytest.mark.parametrize("url, expected", [(DIAS_TOMA_TRATAMIENTO, (4, 1, 58.0, 3)), (DIAS_DIAGNOSTICO_TRATAMIENTO, (1, 1, 18.0, 0))]) +def test_the_times_to_treatment_are_counted_from_their_start_to_the_first_treatment(base_con_tratamiento, manifest, url, expected): + """Pins down the two times to treatment on the fixture with H-004's colposcopy and treatment, 2025-S2 cut at the + end of the year, with a 60-day horizon. From the sample: H-004's July sample reaches her treatment of August 28 in + 58 days; the other three samples of the semester have no treatment and their horizon has passed, so they are in + the initial population and not measured. From the diagnosis: only that sample has one in the period, and its + diagnosis of August 10 reaches the treatment in 18 days. Fails if the end event, the start (sample or diagnosis), + the horizon rule or the split between not measured yet and without event changes.""" + from webapi.durations import answer_duration, duration_definitions + + definition = duration_definitions(manifest)[url] + result = answer_duration("cacu", definition, Question(url, "2025-S2", date(2025, 12, 31)), + **repositories(base_con_tratamiento)) + assert (result.initial, result.n, result.median, result.excluded) == expected + + CONFIRMACION = "cacu/CACU_CONFIRMACION_CITOLOGIA" diff --git a/tests/test_orquestacion_oro.py b/tests/test_orquestacion_oro.py index eb2f598..245ff07 100644 --- a/tests/test_orquestacion_oro.py +++ b/tests/test_orquestacion_oro.py @@ -84,11 +84,11 @@ def test_las_definiciones_son_las_del_manifiesto_de_dbt(base): measure the manifest declares stops reaching `cat_estrato` and `cat_measure`, or a second run deletes rows.""" resumen = base.run("definitions")["definitions"] - # CaCu: three strata and its seven measures; CaMa: the BI-RADS stratum of the histopathological result and its + # CaCu: three strata and its nine measures; CaMa: the BI-RADS stratum of the histopathological result and its # eleven measures; TEA: the age group and its ten measures. - assert resumen["definitions"] == {"cacu": 10, "cama": 12, "tea": 11}[base.proyecto] + assert resumen["definitions"] == {"cacu": 12, "cama": 12, "tea": 11}[base.proyecto] # Y el catálogo de publicación de §6f (T-19b): un `cat_measure` por measure, sincronizado. - assert base.dominio.cat_measure_repository.count() == {"cacu": 7, "cama": 11, "tea": 10}[base.proyecto] + assert base.dominio.cat_measure_repository.count() == {"cacu": 9, "cama": 11, "tea": 10}[base.proyecto] assert base.run("definitions")["definitions"]["borradas"] == 0 diff --git a/tests/test_reportes.py b/tests/test_reportes.py index ebf0949..76c9b52 100644 --- a/tests/test_reportes.py +++ b/tests/test_reportes.py @@ -108,7 +108,8 @@ def test_indicators_recompute_from_imported_rows(client): assert {p["paso"] for p in estado["pasos"]} >= {"load", "event_models"} assert {m["id"] for m in client.get("/cacu/indicadores").get_json()} == { "IND1", "CACU_EU13", "CACU_EU7", "CACU_CONFIRMACION_CITOLOGIA", "CACU_EU_TIEMPO_REPORTE", - "CACU_DIAS_TOMA_DIAGNOSTICO", "CACU_RIESGO_BANDA"} + "CACU_DIAS_TOMA_DIAGNOSTICO", "CACU_DIAS_TOMA_TRATAMIENTO", "CACU_DIAS_DIAGNOSTICO_TRATAMIENTO", + "CACU_RIESGO_BANDA"} assert client.post("/reportes/indicadores/cacu/recalcular").status_code == 202 assert client.post("/reportes/indicadores/xx/recalcular").status_code == 404