integration layer; merge work on indicators from forks with existing ingestion pipeline
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
"""Endpoint tests for `/tea/indicadores`, `/cacu/indicadores` and
|
||||
`/cama/indicadores`: the read side of the bronze/silver/gold pipeline
|
||||
(Persistance/persistance/aristas, indicadores_kit). Seeds one patient,
|
||||
runs the real `cargar`/`registro`/`definiciones`/`calcular` steps, then
|
||||
hits the HTTP layer."""
|
||||
from datetime import date
|
||||
from functools import partial
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def client(wire_domain):
|
||||
wire_domain("tea")
|
||||
|
||||
from core.tea.models import Pacientes
|
||||
from persistance import db as persistance_db
|
||||
from persistance.tea import (
|
||||
staging_repository, carga_repository, def_parametro_repository,
|
||||
dim_periodo_repository, indicador_definicion_repository, calculo_repository,
|
||||
)
|
||||
from indicadores_kit.tea import etl
|
||||
from indicadores_kit.registry.parametros import leer_parametros, RUTA_PARAMETROS
|
||||
from indicadores_kit.registry.periodos import periodos_de_tipos
|
||||
from indicadores_kit.registry.measures import measures_de, rutas_de_measures
|
||||
from indicadores_kit.serving.definiciones import leer_definiciones
|
||||
from indicadores_kit.mart.calculo import medir_con_unidades
|
||||
from indicadores_kit.tablas import tablas_de
|
||||
|
||||
with __import__("sqlmodel").Session(persistance_db.get_tea_engine()) as session:
|
||||
session.add(Pacientes(pac_id_hospital="H-1", pac_fecha_nacimiento=date(2020, 1, 1), pac_sexo="F"))
|
||||
session.commit()
|
||||
|
||||
staging = staging_repository.leer(etl.STAGING)
|
||||
lote = etl.lote_de_estudios(**staging)
|
||||
carga_repository.guardar_lote(**lote.para_guardar())
|
||||
|
||||
tablas = tablas_de("tea")
|
||||
measures = measures_de("tea")
|
||||
tipos = sorted({t for m in measures.values() for t in m.extension.publicacion.tipos_periodo})
|
||||
def_parametro_repository.reemplazar(leer_parametros(RUTA_PARAMETROS, tablas=tablas))
|
||||
dim_periodo_repository.agregar_faltantes(periodos_de_tipos(tipos, 2024, 2024, tablas=tablas))
|
||||
indicador_definicion_repository.reemplazar(leer_definiciones(rutas_de_measures("tea"), tablas=tablas))
|
||||
nombre, medida = next(iter(measures.items()))
|
||||
for tipo in medida.extension.publicacion.tipos_periodo:
|
||||
for periodo_id in calculo_repository.periodos(tipo, date(2024, 1, 1), date(2025, 1, 1)):
|
||||
medir = partial(medir_con_unidades, measure=medida, periodo_id=periodo_id, tablas=tablas,
|
||||
corte=None, permitir_sin_definir=True)
|
||||
calculo_repository.calcular(medir)
|
||||
|
||||
from webapi import create_app
|
||||
|
||||
app = create_app()
|
||||
app.config.update(TESTING=True)
|
||||
with app.test_client() as test_client:
|
||||
yield test_client
|
||||
|
||||
|
||||
def test_list_indicadores(client):
|
||||
"""Only the 5 TEA measures not marked `oculto` in their own TOML publish
|
||||
to the catalog (the K01-K08/M02-M19 family is draft/internal); a hidden
|
||||
id still answers `/datos` directly, see `test_indicador_datos_...`."""
|
||||
response = client.get("/tea/indicadores")
|
||||
assert response.status_code == 200
|
||||
indicadores = response.get_json()
|
||||
assert isinstance(indicadores, list)
|
||||
assert len(indicadores) == 5
|
||||
assert {"id", "titulo", "contrato", "unidad", "sentido", "tipos_periodo", "estratos", "figuras"} <= indicadores[0].keys()
|
||||
|
||||
|
||||
def test_indicador_datos_returns_valid_json_no_bare_nan(client):
|
||||
response = client.get("/tea/indicadores/K01_evaluacion_3_meses/datos?periodos=2024-S1,2024-S2")
|
||||
assert response.status_code == 200
|
||||
# Pandas renders missing scores as NaN; the response must not leak the
|
||||
# bare (invalid-JSON) token -- it has to come through as `null`.
|
||||
assert "NaN" not in response.get_data(as_text=True)
|
||||
body = response.get_json()
|
||||
assert body["id"] == "K01_evaluacion_3_meses"
|
||||
assert set(body["periodos"]) == {"2024-S1", "2024-S2"}
|
||||
assert {row["periodo"] for row in body["filas"]} == {"2024-S1", "2024-S2"}
|
||||
|
||||
|
||||
def test_indicador_datos_without_periodos_defaults_to_available_ones(client):
|
||||
"""Unlike the old endpoint, omitting `periodos` is not an error: it
|
||||
reports whatever periods already have a computed report."""
|
||||
response = client.get("/tea/indicadores/K01_evaluacion_3_meses/datos")
|
||||
assert response.status_code == 200
|
||||
assert response.get_json()["periodos"]
|
||||
|
||||
|
||||
def test_indicador_datos_unknown_indicador_is_404(client):
|
||||
response = client.get("/tea/indicadores/NOPE/datos?periodos=2024-S1")
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_indicador_datos_malformed_periodo_is_400(client):
|
||||
response = client.get("/tea/indicadores/K01_evaluacion_3_meses/datos?periodos=no-es-un-periodo")
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
def test_indicador_periodos_endpoint(client):
|
||||
response = client.get("/tea/indicadores/K01_evaluacion_3_meses/periodos")
|
||||
assert response.status_code == 200
|
||||
body = response.get_json()
|
||||
assert body["id"] == "K01_evaluacion_3_meses"
|
||||
assert isinstance(body["periodos"], list)
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def cacu_client(wire_domain):
|
||||
"""CaCu reproduces a real id mismatch TEA's fixture doesn't: some of its
|
||||
TOML files are named in lowercase (`cacu_biopsias.toml`, the
|
||||
`measures_de` dict key) while their `measure.url` ends in uppercase
|
||||
(`cacu/CACU_BIOPSIAS`, the id `indicador_definicion.indicador` and the
|
||||
catalog publish) -- caught live by driving the WebApp dashboard against
|
||||
a real CaCu indicator, not by the TEA-only fixture above."""
|
||||
wire_domain("cacu")
|
||||
|
||||
from sqlmodel import Session
|
||||
from persistance import db as persistance_db
|
||||
from persistance.cacu import (
|
||||
staging_repository, carga_repository, def_parametro_repository as cacu_def_parametro_repository,
|
||||
dim_periodo_repository, indicador_definicion_repository, calculo_repository, vistas_repository,
|
||||
)
|
||||
from indicadores_kit.cacu import etl, fixtures
|
||||
from indicadores_kit.cdm.etl import lote_de_resultados
|
||||
from indicadores_kit.esquema import ddl_de_vistas
|
||||
from indicadores_kit.registry.parametros import leer_parametros, RUTA_PARAMETROS
|
||||
from indicadores_kit.registry.periodos import periodos_de_tipos
|
||||
from indicadores_kit.registry.measures import measures_de, rutas_de_measures
|
||||
from indicadores_kit.serving.definiciones import leer_definiciones
|
||||
from indicadores_kit.mart.calculo import medir_con_unidades
|
||||
from indicadores_kit.tablas import tablas_de
|
||||
|
||||
with Session(persistance_db.get_cacu_engine()) as session:
|
||||
fixtures.insertar(session)
|
||||
session.commit()
|
||||
|
||||
tablas = tablas_de("cacu")
|
||||
staging = staging_repository.leer(etl.STAGING)
|
||||
lote = etl.lote_de_estudios(**staging)
|
||||
carga_repository.guardar_lote(**lote.para_guardar())
|
||||
pacientes = staging_repository.leer({"pacientes": etl.STAGING["pacientes"]})["pacientes"]
|
||||
lote_resultados = lote_de_resultados(
|
||||
pacientes, staging_repository.leer(etl.STAGING_DE_RESULTADOS), etl.FUENTES_DE_RESULTADOS, tablas=tablas,
|
||||
)
|
||||
carga_repository.guardar_lote(**lote_resultados.para_guardar())
|
||||
vistas_repository.construir(ddl_de_vistas("cacu"))
|
||||
|
||||
measures = measures_de("cacu")
|
||||
tipos = sorted({t for m in measures.values() for t in m.extension.publicacion.tipos_periodo})
|
||||
cacu_def_parametro_repository.reemplazar(leer_parametros(RUTA_PARAMETROS, tablas=tablas))
|
||||
dim_periodo_repository.agregar_faltantes(periodos_de_tipos(tipos, 2025, 2025, tablas=tablas))
|
||||
indicador_definicion_repository.reemplazar(leer_definiciones(rutas_de_measures("cacu"), tablas=tablas))
|
||||
for medida in measures.values():
|
||||
for tipo in medida.extension.publicacion.tipos_periodo:
|
||||
for periodo_id in calculo_repository.periodos(tipo, date(2025, 1, 1), date(2026, 1, 1)):
|
||||
medir = partial(medir_con_unidades, measure=medida, periodo_id=periodo_id, tablas=tablas,
|
||||
corte=None, permitir_sin_definir=True)
|
||||
calculo_repository.calcular(medir)
|
||||
|
||||
from webapi import create_app
|
||||
|
||||
app = create_app()
|
||||
app.config.update(TESTING=True)
|
||||
with app.test_client() as test_client:
|
||||
yield test_client
|
||||
|
||||
|
||||
def test_indicador_datos_uses_the_toml_key_not_measure_url(cacu_client):
|
||||
"""`cacu_biopsias`'s own `measure.url` ends in `CACU_BIOPSIAS` (uppercase),
|
||||
but `registrar_indicadores` publishes and looks measures up by
|
||||
`measures_de()`'s dict key (the lowercase TOML file stem) throughout, so
|
||||
the mismatch never reaches a client -- unlike the old, since-replaced
|
||||
`webapi.indicadores`, which published the uppercase `measure.url` id and
|
||||
needed its own `_measure_por_indicador` lookup to reconcile the two."""
|
||||
catalogo = cacu_client.get("/cacu/indicadores").get_json()
|
||||
assert any(i["id"] == "cacu_biopsias" for i in catalogo)
|
||||
assert not any(i["id"] == "CACU_BIOPSIAS" for i in catalogo)
|
||||
|
||||
response = cacu_client.get("/cacu/indicadores/cacu_biopsias/datos?periodos=2025-S1,2025-S2")
|
||||
assert response.status_code == 200
|
||||
assert response.get_json()["filas"]
|
||||
Reference in New Issue
Block a user