7577c99c62
CaMa registra registrar_indicadores en su blueprint. Pruebas de CaMa con SQLite temporal sembrada con los fixtures del kit. README al día. Pruebas: Core 25 passed; Persistance 31 passed; indicadores_kit 564 passed, 2 skipped; WebApi 74 passed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
111 lines
3.6 KiB
Python
111 lines
3.6 KiB
Python
"""Rutas de indicadores de CAMA: base SQLite temporal sembrada con los fixtures del kit."""
|
|
|
|
import pytest
|
|
from conftest import MESES, SEMESTRES, cliente_de
|
|
from indicadores_kit.registry.measures import measures_de
|
|
from indicadores_kit.serving.figuras import figuras_de
|
|
|
|
PROYECTO = "cama"
|
|
INDICADOR = "cama_eu8"
|
|
DEFINICIONES = measures_de(PROYECTO)
|
|
(FIGURA,) = figuras_de(DEFINICIONES[INDICADOR])
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def client(tmp_path_factory):
|
|
yield from cliente_de(PROYECTO, tmp_path_factory)
|
|
|
|
|
|
def _figura(client, indicador: str = INDICADOR, figura: str = FIGURA, query: str = ""):
|
|
return client.get(f"/{PROYECTO}/indicadores/{indicador}/figuras/{figura}{query}")
|
|
|
|
|
|
def _x(respuesta) -> list[str]:
|
|
return [x for trazo in respuesta.get_json()["data"] for x in trazo["x"]]
|
|
|
|
|
|
def test_listar_indicadores(client):
|
|
response = client.get(f"/{PROYECTO}/indicadores")
|
|
|
|
assert response.status_code == 200
|
|
lista = response.get_json()
|
|
assert [fila["id"] for fila in lista] == sorted(DEFINICIONES)
|
|
fila = next(fila for fila in lista if fila["id"] == INDICADOR)
|
|
medida = DEFINICIONES[INDICADOR]
|
|
assert fila["titulo"] == medida.title
|
|
assert fila["contrato"] == medida.extension.contrato
|
|
assert fila["figuras"] == figuras_de(medida) == [FIGURA]
|
|
assert fila["tipos_periodo"] == ["semestre", "mes"]
|
|
|
|
|
|
def test_figura_con_semestres(client):
|
|
response = _figura(client, query=f"?periodos={','.join(SEMESTRES)}&tema=oscuro")
|
|
|
|
assert response.status_code == 200
|
|
assert response.mimetype == "application/json"
|
|
assert response.get_json()["layout"]["title"]["text"] == DEFINICIONES[INDICADOR].title
|
|
assert set(_x(response)) == set(SEMESTRES)
|
|
|
|
|
|
def test_figura_sin_periodos_usa_los_semestres_con_reporte(client):
|
|
response = _figura(client)
|
|
|
|
assert response.status_code == 200
|
|
assert set(_x(response)) == set(SEMESTRES) # el primer tipo que publica; los meses no entran
|
|
|
|
|
|
def test_figura_con_meses(client):
|
|
response = _figura(client, query=f"?periodos={','.join(MESES)}")
|
|
|
|
assert response.status_code == 200
|
|
assert set(_x(response)) == set(MESES)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"query",
|
|
[
|
|
"?tema=rojo",
|
|
"?periodos=2025-1",
|
|
"?periodos=2025-S3",
|
|
"?periodos=2025-13x",
|
|
"?periodos=2025-S1,",
|
|
"?periodos=abc",
|
|
"?periodos=2025-T1", # un tipo que el indicador no publica
|
|
"?periodos=2025-S1,2025-03", # tipos mezclados
|
|
],
|
|
)
|
|
def test_figura_con_parametros_mal_formados_da_400(client, query):
|
|
response = _figura(client, query=query)
|
|
|
|
assert response.status_code == 400
|
|
assert "error" in response.get_json()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("indicador", "figura", "query"),
|
|
[
|
|
("CAMA_NO_EXISTE", FIGURA, ""),
|
|
(INDICADOR, "fig_c2_apiladas", ""), # figura de otro contrato
|
|
(INDICADOR, "fig_no_existe", ""),
|
|
(INDICADOR, FIGURA, "?periodos=2030-S1"), # sin reporte
|
|
(INDICADOR, FIGURA, "?periodos=2025-S1,2030-S2"),
|
|
(INDICADOR, FIGURA, "?periodos=2025-11"), # un mes sin reporte
|
|
],
|
|
)
|
|
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_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, query="?periodos=2025-S1")
|