allowances: add a would_deactivate PREVIEW (deactivation stays gated off)
Report mode (and a send run while ALLOWANCE_DEACTIVATE is off) now reports a member whose reminder is past the grace window as would_deactivate, with the reminder date, grace length and deadline in the evidence. The real action is unchanged and still needs BOTH ALLOWANCE_POLICY=on AND ALLOWANCE_DEACTIVATE=on. The lapse decision is extracted into a pure function (allowance_lapse_decision) so it is unit-testable against a synthetic reminder time: tests/test_allowance_lapse.py (would_deactivate past grace; in_grace inside).
This commit is contained in:
1 parent
ee7ed77697
commit
8c85c912e8
2 files changed
+125
-11
No files matched your search
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Regression tests for app.main.allowance_lapse_decision (grace/deactivation).
|
||||
|
||||
Self-contained and runnable:
|
||||
|
||||
* from the repo root: python3 tests/test_allowance_lapse.py
|
||||
* inside the portal app: PORTAL_CODE_DIR=/app/code python3 /tmp/test_allowance_lapse.py
|
||||
(the container has the app's own dependencies; the host usually does not)
|
||||
|
||||
WHY THIS EXISTS
|
||||
Deactivation is gated OFF (ALLOWANCE_DEACTIVATE=off) and the flag must stay
|
||||
off until the preview is trusted. That makes the live preview legitimately
|
||||
show ZERO past-grace members right after a reminder, so this file proves the
|
||||
decision logic on a SYNTHETIC reminder time instead of waiting a week — and
|
||||
without altering any real member's rows.
|
||||
|
||||
The decision is a pure function (no DB, no network, no clock), so a past
|
||||
reminder, a grace boundary and the "armed" case are all exercised directly:
|
||||
|
||||
- reminder 10 days ago, NOT authorised -> ("would_deactivate", deadline)
|
||||
[the preview this change adds]
|
||||
- reminder 3 days ago (inside grace) -> ("in_grace", None)
|
||||
- reminder exactly at the grace edge -> ("would_deactivate", deadline)
|
||||
- reminder one second inside the edge -> ("in_grace", None)
|
||||
- reminder 10 days ago, AUTHORISED -> ("deactivated", deadline)
|
||||
[the real path, unchanged: needs policy=on AND deactivate=on]
|
||||
- no reminder at all -> ("in_grace", None)
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
# Locate the app package: explicit env, the deployed path, or the repo root.
|
||||
for _cand in (os.environ.get("PORTAL_CODE_DIR"), "/app/code",
|
||||
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))):
|
||||
if _cand and os.path.isfile(os.path.join(_cand, "app", "main.py")):
|
||||
sys.path.insert(0, _cand)
|
||||
break
|
||||
|
||||
from app.main import allowance_lapse_decision # noqa: E402
|
||||
|
||||
NOW = datetime(2026, 11, 4, 12, 0, tzinfo=timezone.utc)
|
||||
GRACE = 7.0
|
||||
|
||||
# (name, reminder_sent_at, acting, expected_decision, expected_deadline)
|
||||
CASES = [
|
||||
("reminder 10d ago, not authorised -> would_deactivate (preview)",
|
||||
NOW - timedelta(days=10), False, "would_deactivate", NOW - timedelta(days=10) + timedelta(days=7)),
|
||||
("reminder 3d ago (inside grace) -> in_grace",
|
||||
NOW - timedelta(days=3), False, "in_grace", None),
|
||||
("reminder exactly 7d ago (at the edge) -> would_deactivate",
|
||||
NOW - timedelta(days=7), False, "would_deactivate", NOW),
|
||||
("reminder 6d23h ago (one hour inside grace) -> in_grace",
|
||||
NOW - timedelta(days=6, hours=23), False, "in_grace", None),
|
||||
("reminder 10d ago, AUTHORISED -> deactivated (real path)",
|
||||
NOW - timedelta(days=10), True, "deactivated", NOW - timedelta(days=10) + timedelta(days=7)),
|
||||
("no reminder ever sent -> in_grace",
|
||||
None, False, "in_grace", None),
|
||||
]
|
||||
|
||||
failures = 0
|
||||
for name, sent_at, acting, expect_dec, expect_dl in CASES:
|
||||
decision, deadline = allowance_lapse_decision(sent_at, NOW, GRACE, acting)
|
||||
dl_ok = (deadline == expect_dl)
|
||||
ok = (decision == expect_dec) and dl_ok
|
||||
if not ok:
|
||||
failures += 1
|
||||
print(f"[{'PASS' if ok else 'FAIL'}] {name}\n"
|
||||
f" decision={decision} (expected {expect_dec}) "
|
||||
f"deadline={deadline} (expected {expect_dl}) acting={acting}")
|
||||
|
||||
print(f"\n{len(CASES) - failures}/{len(CASES)} passed")
|
||||
sys.exit(1 if failures else 0)
|
||||
Reference in new issue
Block a user