fix(rules): el umbral en UMA sale solo de la configuración de la regla

A-01, A-02 y B-03 ya no traen 1605 por omisión: sin factor_uma la regla no
dispara y lo reporta como sin datos por cliente.
This commit is contained in:
Jorem Juárez committed 2026-11-04 19:56:17 -06:00
1 parent 6cbb912ccc
commit afbecf5984
2 files changed
+51 -6

No files matched your search

+22 -4
View File
@@ -62,6 +62,18 @@ def _dec(valor: Any, por_defecto: str) -> Decimal:
return Decimal(str(valor if valor is not None else por_defecto))
def _factor_uma(p: dict[str, Any]) -> Decimal | None:
"""El factor del umbral en UMA sale solo de la configuración de la regla: sin él, la regla no
dispara (no hay un umbral por omisión en el código)."""
valor = p.get("factor_uma")
return Decimal(str(valor)) if valor is not None else None
def _sin_factor_uma(ctx: Contexto) -> Iterator[SinDatos]:
for cliente_id in ctx.clientes:
yield SinDatos(cliente_id, "sin parámetro factor_uma")
def _pep_vigente(cliente: ClienteCtx, tipos: set[str]) -> frozenset[str]:
return frozenset(cliente.pep_tipos & tipos)
@@ -70,8 +82,10 @@ def _pep_vigente(cliente: ClienteCtx, tipos: set[str]) -> frozenset[str]:
@regla("A-01")
def a01_umbral_credito(ctx: Contexto, p: dict[str, Any]) -> Iterator[AlertaCandidata]:
factor = _dec(p.get("factor_uma"), "1605")
def a01_umbral_credito(ctx: Contexto, p: dict[str, Any]) -> Iterator[AlertaCandidata | SinDatos]:
if (factor := _factor_uma(p)) is None:
yield from _sin_factor_uma(ctx)
return
umbral = factor * ctx.uma
for credito in ctx.creditos.values():
if credito.monto >= umbral:
@@ -93,7 +107,9 @@ def a01_umbral_credito(ctx: Contexto, p: dict[str, Any]) -> Iterator[AlertaCandi
def a02_acumulacion_en_ventana(
ctx: Contexto, p: dict[str, Any]
) -> Iterator[AlertaCandidata | SinDatos]:
factor = _dec(p.get("factor_uma"), "1605")
if (factor := _factor_uma(p)) is None:
yield from _sin_factor_uma(ctx)
return
umbral = factor * ctx.uma
creditos_por_cliente: dict[int, list] = {}
for credito in ctx.creditos.values():
@@ -164,7 +180,9 @@ def b02_capacidad_de_pago(ctx: Contexto, p: dict[str, Any]) -> Iterator[AlertaCa
@regla("B-03")
def b03_estructuracion(ctx: Contexto, p: dict[str, Any]) -> Iterator[AlertaCandidata | SinDatos]:
n = int(p.get("num_creditos_mayor_que", 3))
factor = _dec(p.get("factor_uma"), "1605")
if (factor := _factor_uma(p)) is None:
yield from _sin_factor_uma(ctx)
return
umbral = factor * ctx.uma
for cliente in ctx.clientes.values():
agg = cliente.agregado
+29 -2
View File
@@ -2,8 +2,10 @@
from __future__ import annotations
import json
from datetime import date
from decimal import Decimal
from pathlib import Path
import pytest
@@ -53,12 +55,37 @@ def pago(id_, fecha, metodo="TRANSFERENCIA", tipo="PAGO", total="5000", **kw) ->
)
# El umbral en UMA sale solo de la configuración de la regla (el código no trae uno por omisión,
# Pieza M): las pruebas toman el `factor_uma` del JSON v2 y cada una sobreescribe lo que necesita.
PARAMETROS_V2 = {
r["clave"]: {"factor_uma": r["parametros"]["factor_uma"]}
for r in json.loads(
(Path(__file__).resolve().parents[1] / "seed" / "reglas_alerta_v2.json").read_text("utf-8")
)["reglas"]
if "factor_uma" in r["parametros"]
}
def correr(clave: str, ctx: Contexto, **parametros) -> list[AlertaCandidata]:
return [x for x in REGISTRO[clave](ctx, parametros) if not isinstance(x, SinDatos)]
p = {**PARAMETROS_V2.get(clave, {}), **parametros}
return [x for x in REGISTRO[clave](ctx, p) if not isinstance(x, SinDatos)]
def sin_datos(clave: str, ctx: Contexto, **parametros) -> list[int]:
return [x.cliente_id for x in REGISTRO[clave](ctx, parametros) if isinstance(x, SinDatos)]
p = {**PARAMETROS_V2.get(clave, {}), **parametros}
return [x.cliente_id for x in REGISTRO[clave](ctx, p) if isinstance(x, SinDatos)]
@pytest.mark.parametrize("clave", ["A-01", "A-02", "B-03"])
def test_sin_factor_uma_en_la_configuracion_la_regla_no_dispara(clave: str) -> None:
"""Sin `factor_uma` no hay umbral: el código no trae uno por omisión."""
ctx = contexto(
[cliente(1, agregado=agregado(acumulado_creditos_ventana=Decimal(10**9)))],
[credito(monto=Decimal(10**9))],
)
resultado = list(REGISTRO[clave](ctx, {}))
assert resultado and all(isinstance(x, SinDatos) for x in resultado)
assert {x.motivo for x in resultado} == {"sin parámetro factor_uma"}
def agregado(**kw) -> AgregadoCtx: