b89b630c2e
La base de prueba es la de demostración del kit (todas las definiciones calculadas en los periodos que publican) y hay una prueba de figura por indicador. El caso del mes sin reporte pasa a 2024-11: 2025-11 ya tiene. Pruebas: WebApi 79 passed. CSO5.5
123 lines
3.9 KiB
Python
123 lines
3.9 KiB
Python
"""Rutas de indicadores de CACU: 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 = "cacu"
|
|
INDICADOR = "cacu_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"),
|
|
[
|
|
("CACU_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=2024-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")
|
|
|
|
|
|
# --- Todos los indicadores del proyecto ---
|
|
|
|
|
|
@pytest.mark.parametrize("indicador", sorted(DEFINICIONES))
|
|
def test_figura_de_cada_indicador(client, indicador):
|
|
for figura in figuras_de(DEFINICIONES[indicador]):
|
|
response = _figura(client, indicador, figura)
|
|
|
|
assert response.status_code == 200, (indicador, figura)
|
|
assert set(response.get_json()) >= {"data", "layout"}
|