cca67c837d
registrar_indicadores(blueprint, proyecto, get_session) agrega GET /indicadores (con tipos_periodo) y GET /indicadores/<id>/figuras/<figura>. TEA pasa a ese módulo; los periodos se validan contra los tipos que publica cada definición (semestre, mes, ...) y las figuras salen de figuras_de(medida). Pruebas de CaCu con SQLite temporal sembrada con los fixtures del kit. Pruebas: Core 25 passed; Persistance 31 passed; indicadores_kit 564 passed, 2 skipped; WebApi 55 passed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
124 lines
4.1 KiB
Python
124 lines
4.1 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")
|