830d978a1b
GET /<proyecto>/indicadores/<id>/periodos, /datos y /metadatos entregan en JSON lo que arma indicadores_kit.serving.datos, para mostrar el número o graficar desde el frontend. El catálogo agrega version, unidad, sentido y estratos. Las figuras siguen igual. Pruebas: WebApi 133 passed. CSO5.5
153 lines
5.5 KiB
Python
153 lines
5.5 KiB
Python
import pytest
|
|
from indicadores_kit.registry.measures import measures_de
|
|
from indicadores_kit.serving.figuras import figuras_de
|
|
from indicadores_kit.tea import fixtures
|
|
|
|
DEFINICIONES = measures_de("tea")
|
|
PERIODOS = ",".join(fixtures.PERIODOS)
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def client(tmp_path_factory):
|
|
"""Una base de TEA temporal, con los 18 indicadores calculados, por `TEA_DATABASE_URL`."""
|
|
# Dentro de la fixture: la configuración de Persistance se lee al importarla,
|
|
# y test_app.py debe alcanzar a fijar CACU_DATABASE_URL antes.
|
|
from persistance.db import get_tea_engine
|
|
from persistance.settings import settings
|
|
|
|
ruta = tmp_path_factory.mktemp("tea") / "tea.db"
|
|
with pytest.MonkeyPatch.context() as mp:
|
|
mp.setattr(settings, "tea_database_url", f"sqlite:///{ruta}")
|
|
get_tea_engine.cache_clear()
|
|
try:
|
|
fixtures.base_de_prueba(get_tea_engine())
|
|
|
|
from webapi import create_app
|
|
|
|
app = create_app()
|
|
app.config.update(TESTING=True)
|
|
with app.test_client() as cliente:
|
|
yield cliente
|
|
finally:
|
|
get_tea_engine().dispose()
|
|
get_tea_engine.cache_clear()
|
|
|
|
|
|
def _figura(client, indicador: str, figura: str, query: str = ""):
|
|
return client.get(f"/tea/indicadores/{indicador}/figuras/{figura}{query}")
|
|
|
|
|
|
def test_listar_indicadores(client):
|
|
response = client.get("/tea/indicadores")
|
|
assert response.status_code == 200
|
|
|
|
lista = response.get_json()
|
|
assert [fila["id"] for fila in lista] == sorted(DEFINICIONES)
|
|
for fila in lista:
|
|
medida = DEFINICIONES[fila["id"]]
|
|
assert fila["titulo"] == medida.title
|
|
assert fila["contrato"] == medida.extension.contrato
|
|
assert fila["figuras"] == figuras_de(medida)
|
|
|
|
|
|
@pytest.mark.parametrize("indicador", sorted(DEFINICIONES))
|
|
def test_figura_de_cada_indicador(client, indicador):
|
|
(figura,) = figuras_de(DEFINICIONES[indicador])
|
|
|
|
response = _figura(client, indicador, figura, f"?periodos={PERIODOS}&tema=oscuro")
|
|
|
|
assert response.status_code == 200
|
|
assert response.mimetype == "application/json"
|
|
assert "data" in response.get_json()
|
|
|
|
|
|
def test_figura_sin_periodos_usa_todos_los_reportes(client):
|
|
(figura,) = figuras_de(DEFINICIONES["K01_evaluacion_3_meses"])
|
|
|
|
response = _figura(client, "K01_evaluacion_3_meses", figura)
|
|
|
|
assert response.status_code == 200
|
|
assert response.get_json()["layout"]["title"]["text"] == DEFINICIONES["K01_evaluacion_3_meses"].title
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"query",
|
|
[
|
|
"?tema=rojo",
|
|
"?periodos=2025-1",
|
|
"?periodos=2025-S1,",
|
|
"?periodos=abc",
|
|
"?periodos=,",
|
|
],
|
|
)
|
|
def test_figura_con_parametros_mal_formados_da_400(client, query):
|
|
response = _figura(client, "K01_evaluacion_3_meses", "fig_c1_grupos", query)
|
|
|
|
assert response.status_code == 400
|
|
assert "error" in response.get_json()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("indicador", "figura", "query"),
|
|
[
|
|
("K99_no_existe", "fig_c1_grupos", ""),
|
|
("K01_evaluacion_3_meses", "fig_c2_apiladas", ""), # figura de otro contrato
|
|
("K01_evaluacion_3_meses", "fig_no_existe", ""),
|
|
("K01_evaluacion_3_meses", "fig_c1_grupos", "?periodos=2030-S1"), # sin reporte
|
|
("K01_evaluacion_3_meses", "fig_c1_grupos", f"?periodos={PERIODOS},2030-S2"),
|
|
],
|
|
)
|
|
def test_figura_desconocida_o_sin_reporte_da_404(client, indicador, figura, query):
|
|
response = _figura(client, indicador, figura, query)
|
|
|
|
assert response.status_code == 404
|
|
assert "error" in response.get_json()
|
|
|
|
|
|
def test_paciente_por_id_entero(client):
|
|
response = client.get("/tea/pacientes/1")
|
|
assert response.status_code == 200
|
|
assert response.get_json()["pac_id_hospital"] == "T-01"
|
|
|
|
assert client.get("/tea/pacientes/abc").status_code == 404
|
|
|
|
|
|
def test_un_keyerror_interno_no_se_vuelve_404(client, monkeypatch):
|
|
from webapi import indicadores
|
|
|
|
def falla(*args, **kwargs):
|
|
raise KeyError("columna")
|
|
|
|
monkeypatch.setattr(indicadores, "figura_json", falla)
|
|
with pytest.raises(KeyError):
|
|
_figura(client, "K01_evaluacion_3_meses", "fig_c1_grupos", "?periodos=2025-S1")
|
|
|
|
|
|
# --- Los periodos, los datos y los metadatos ---
|
|
|
|
|
|
@pytest.mark.parametrize("indicador", sorted(DEFINICIONES))
|
|
def test_periodos_datos_y_metadatos_de_cada_indicador(client, indicador):
|
|
medida = DEFINICIONES[indicador]
|
|
|
|
periodos = client.get(f"/tea/indicadores/{indicador}/periodos")
|
|
datos = client.get(f"/tea/indicadores/{indicador}/datos?periodos={PERIODOS}")
|
|
metadatos = client.get(f"/tea/indicadores/{indicador}/metadatos")
|
|
|
|
assert (periodos.status_code, datos.status_code, metadatos.status_code) == (200, 200, 200)
|
|
assert [p["periodo"] for p in periodos.get_json()["periodos"]] == sorted(fixtures.PERIODOS)
|
|
cuerpo = datos.get_json()
|
|
assert (cuerpo["id"], cuerpo["contrato"]) == (indicador, medida.extension.contrato)
|
|
assert cuerpo["filas"] and {fila["periodo"] for fila in cuerpo["filas"]} == set(fixtures.PERIODOS)
|
|
assert {fila["grupo"] for fila in cuerpo["filas"]} <= set(cuerpo["grupos"])
|
|
assert "data" not in cuerpo and "layout" not in cuerpo # no es una figura
|
|
assert metadatos.get_json()["ficha"]["codigo"] == medida.extension.ficha.codigo
|
|
|
|
|
|
def test_los_grupos_de_los_datos_van_en_el_orden_de_la_ficha(client):
|
|
response = client.get(f"/tea/indicadores/K01_evaluacion_3_meses/datos?periodos={PERIODOS}")
|
|
|
|
grupos = list(response.get_json()["grupos"])
|
|
assert grupos != sorted(grupos) # el orden de la ficha, no el alfabético
|
|
assert grupos[-1] == "TOTAL"
|