- 22 tablas nuevas en app/models por dominio: cliente, screening, credito, riesgo, alertas (25 en total) - enumeraciones como String + CheckConstraint; JSON con sqlalchemy.JSON; sin ON DELETE CASCADE - valid_from/valid_to en documento, declaracion, participacion, beneficiario_controlador, pep, perfil_transaccional, agregado_cliente, modelo_riesgo, regla_alerta - app/services/riesgo.py con el contrato evaluar_cliente (implementación en Fase 2) - tests: 0002 crea las 22 tablas con las columnas de los modelos, downgrade a 0001; el test de 0001 migra a esa revisión
106 lines
3.1 KiB
Python
106 lines
3.1 KiB
Python
"""La migración 0002 crea las 22 tablas restantes (25 en total) y coincide con los modelos."""
|
|
|
|
from pathlib import Path
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import command
|
|
from alembic.autogenerate import compare_metadata
|
|
from alembic.config import Config
|
|
from alembic.runtime.migration import MigrationContext
|
|
|
|
from app.models import Base
|
|
|
|
RAIZ = Path(__file__).resolve().parents[1]
|
|
TABLAS_0001 = {"tenant", "usuario", "audit_trail"}
|
|
TABLAS_0002 = {
|
|
"cliente",
|
|
"documento",
|
|
"declaracion",
|
|
"participacion",
|
|
"beneficiario_controlador",
|
|
"pep",
|
|
"lista",
|
|
"coincidencia",
|
|
"credito",
|
|
"transaccion",
|
|
"agregado_cliente",
|
|
"perfil_transaccional",
|
|
"modelo_riesgo",
|
|
"indicador",
|
|
"nivel_indicador",
|
|
"umbral",
|
|
"evaluacion_riesgo",
|
|
"regla_alerta",
|
|
"alerta",
|
|
"caso",
|
|
"evento_caso",
|
|
"lote_carga",
|
|
}
|
|
HISTORICAS = {
|
|
"documento",
|
|
"declaracion",
|
|
"participacion",
|
|
"beneficiario_controlador",
|
|
"pep",
|
|
"perfil_transaccional",
|
|
"agregado_cliente",
|
|
"modelo_riesgo",
|
|
"regla_alerta",
|
|
}
|
|
|
|
|
|
def _config(url: str) -> Config:
|
|
cfg = Config(str(RAIZ / "alembic.ini"))
|
|
cfg.set_main_option("sqlalchemy.url", url)
|
|
return cfg
|
|
|
|
|
|
def _tablas(conn: sa.Connection) -> set[str]:
|
|
return set(sa.inspect(conn).get_table_names()) - {"alembic_version"}
|
|
|
|
|
|
def test_upgrade_head_crea_las_25_tablas_y_coincide_con_los_modelos(tmp_path: Path) -> None:
|
|
url = f"sqlite:///{tmp_path / 'm.db'}"
|
|
command.upgrade(_config(url), "head")
|
|
|
|
with sa.create_engine(url).connect() as conn:
|
|
assert _tablas(conn) == TABLAS_0001 | TABLAS_0002
|
|
assert len(TABLAS_0002) == 22
|
|
inspector = sa.inspect(conn)
|
|
for tabla in TABLAS_0002:
|
|
columnas_db = {c["name"] for c in inspector.get_columns(tabla)}
|
|
columnas_modelo = set(Base.metadata.tables[tabla].columns.keys())
|
|
assert columnas_db == columnas_modelo, tabla
|
|
ctx = MigrationContext.configure(conn, opts={"compare_type": True})
|
|
assert compare_metadata(ctx, Base.metadata) == []
|
|
|
|
|
|
def test_historicas_llevan_valid_from_y_valid_to() -> None:
|
|
for tabla in HISTORICAS:
|
|
columnas = set(Base.metadata.tables[tabla].columns.keys())
|
|
assert {"valid_from", "valid_to"} <= columnas, tabla
|
|
|
|
|
|
def test_sin_on_delete_cascade_y_sin_enum_nativo() -> None:
|
|
for tabla in Base.metadata.sorted_tables:
|
|
for fk in tabla.foreign_keys:
|
|
assert fk.ondelete is None, f"{tabla.name}: {fk}"
|
|
for columna in tabla.columns:
|
|
if tabla.name in TABLAS_0002:
|
|
assert not isinstance(columna.type, sa.Enum), f"{tabla.name}.{columna.name}"
|
|
|
|
|
|
def test_cliente_no_tiene_columna_de_nivel_de_riesgo() -> None:
|
|
columnas = set(Base.metadata.tables["cliente"].columns.keys())
|
|
assert not {"nivel", "nivel_riesgo", "grado_riesgo", "score"} & columnas
|
|
|
|
|
|
def test_downgrade_regresa_a_0001(tmp_path: Path) -> None:
|
|
url = f"sqlite:///{tmp_path / 'm.db'}"
|
|
cfg = _config(url)
|
|
command.upgrade(cfg, "head")
|
|
command.downgrade(cfg, "0001")
|
|
|
|
with sa.create_engine(url).connect() as conn:
|
|
assert _tablas(conn) == TABLAS_0001
|