ceca8a03b5
KeyError es subclase de LookupError. El except LookupError de la ruta convertía cualquier error interno del kit en un 404 con su mensaje. Ahora solo un periodo sin reporte da 404; lo demás es un error del servidor.
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_tea import FIGURAS_TEA
|
|
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"] == sorted(FIGURAS_TEA[medida.extension.contrato])
|
|
|
|
|
|
@pytest.mark.parametrize("indicador", sorted(DEFINICIONES))
|
|
def test_figura_de_cada_indicador(client, indicador):
|
|
(figura,) = FIGURAS_TEA[DEFINICIONES[indicador].extension.contrato]
|
|
|
|
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_TEA["C1"]
|
|
|
|
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.blueprints import tea
|
|
|
|
def falla(*args, **kwargs):
|
|
raise KeyError("columna")
|
|
|
|
monkeypatch.setattr(tea, "figura_json_tea", falla)
|
|
with pytest.raises(KeyError):
|
|
_figura(client, "K01_evaluacion_3_meses", "fig_c1_grupos", "?periodos=2025-S1")
|