From 084797407cc3e4140c9f7c0adeafb84b547184af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorem=20Ju=C3=A1rez?= Date: Wed, 4 Nov 2026 14:06:30 -0600 Subject: [PATCH] =?UTF-8?q?fix(rules):=20el=20umbral=20en=20UMA=20sale=20s?= =?UTF-8?q?olo=20de=20la=20configuraci=C3=B3n=20de=20la=20regla?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- app/engine/rules_catalog.py | 26 ++++++++++++++++++++++---- tests/test_rules.py | 31 +++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/app/engine/rules_catalog.py b/app/engine/rules_catalog.py index 22a2f87..a1d03d9 100644 --- a/app/engine/rules_catalog.py +++ b/app/engine/rules_catalog.py @@ -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 diff --git a/tests/test_rules.py b/tests/test_rules.py index dc347a2..516c69e 100644 --- a/tests/test_rules.py +++ b/tests/test_rules.py @@ -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: