reconcile_allowances already matches every member's Open Collective orders; it now records what those orders say the member currently pays: dues_amount / dues_frequency / dues_updated_at (added via the lazy ALTER pattern). Only a LIVE recurring order (MONTHLY/YEARLY, ACTIVE/PROCESSING) counts — one-time orders are credit purchases, cancelled/errored ones are not a current level, and anything unreadable is NULL rather than a guessed tier. /admin/overview carries the three fields so the admin panel can show a real Dues column instead of a credit figure. The write happens in report mode too: it records OC's own truth and makes no membership decision. tests/test_dues_level.py pins the extraction rules.
90 lines
3.9 KiB
Python
90 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Tests for app.main._current_dues (the member's CURRENT OC dues level).
|
|
|
|
The admin panel's "Dues" column shows what a member pays on Open Collective
|
|
(e.g. $10/mo). That figure is persisted by reconcile_allowances from the OC
|
|
orders it already fetches; this file pins the pure extraction rules:
|
|
|
|
* a live MONTHLY order -> its per-charge amount + 'MONTHLY';
|
|
* a live YEARLY order -> its amount + 'YEARLY' (no monthly normalization);
|
|
* ACTIVE and PROCESSING both count (a payment in flight is still the level);
|
|
* ONE-TIME orders never become dues — credit packs are purchases, not dues;
|
|
* CANCELLED / ERROR / PAST_DUE recurrences are not what the member pays now;
|
|
* no orders, an unparseable amount or a zero amount -> (None, None).
|
|
NULL is the ONLY honest answer; the code must never fall back to the $15
|
|
allowance or to a guessed tier.
|
|
|
|
python3 tests/test_dues_level.py
|
|
"""
|
|
import os
|
|
import sys
|
|
|
|
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 _current_dues # noqa: E402
|
|
|
|
FAILS = []
|
|
|
|
|
|
def check(cond, msg):
|
|
print((" ok " if cond else " FAIL ") + msg)
|
|
if not cond:
|
|
FAILS.append(msg)
|
|
|
|
|
|
def order(freq, status, amount, created="2026-11-17T01:52:56.450Z"):
|
|
return {"frequency": freq, "status": status,
|
|
"totalAmount": {"value": amount, "currency": "USD"},
|
|
"createdAt": created, "lastChargedAt": created}
|
|
|
|
|
|
print("1. a live monthly subscription is the dues level")
|
|
check(_current_dues([order("MONTHLY", "ACTIVE", 10)]) == (10.0, "MONTHLY"),
|
|
"$10/month ACTIVE monthly -> (10.0, 'MONTHLY')")
|
|
check(_current_dues([order("MONTHLY", "PROCESSING", 12.5)]) == (12.5, "MONTHLY"),
|
|
"a PROCESSING monthly is still the current level (12.5, 'MONTHLY')")
|
|
|
|
print("2. yearly dues are reported as charged — not divided into months")
|
|
check(_current_dues([order("YEARLY", "ACTIVE", 120)]) == (120.0, "YEARLY"),
|
|
"$120/year ACTIVE yearly -> (120.0, 'YEARLY') (the panel appends '/yr')")
|
|
|
|
print("3. one-time orders are purchases, never a dues level")
|
|
check(_current_dues([order("ONETIME", "PAID", 10)]) == (None, None),
|
|
"a PAID $10 one-time order does not become dues")
|
|
check(_current_dues([order("ONETIME", "PAID", 10),
|
|
order("MONTHLY", "ACTIVE", 10)]) == (10.0, "MONTHLY"),
|
|
"a member with BOTH a one-time pack and a monthly subscription reads the monthly")
|
|
|
|
print("4. ended subscriptions do not report a stale level")
|
|
check(_current_dues([order("MONTHLY", "CANCELLED", 10)]) == (None, None),
|
|
"a CANCELLED monthly -> (None, None)")
|
|
check(_current_dues([order("MONTHLY", "ERROR", 10)]) == (None, None),
|
|
"an ERROR (failed charge) monthly -> (None, None)")
|
|
|
|
print("5. nothing readable -> NULL, never a guess")
|
|
check(_current_dues([]) == (None, None), "no orders -> (None, None)")
|
|
check(_current_dues(None) == (None, None), "a missing order list -> (None, None)")
|
|
check(_current_dues([{"frequency": "MONTHLY", "status": "ACTIVE"}]) == (None, None),
|
|
"a monthly with no totalAmount -> (None, None)")
|
|
check(_current_dues([order("MONTHLY", "ACTIVE", 0)]) == (None, None),
|
|
"a $0 monthly -> (None, None), not dues of zero")
|
|
check(_current_dues([{"frequency": "MONTHLY", "status": "ACTIVE",
|
|
"totalAmount": {"value": "not-a-number"}}]) == (None, None),
|
|
"an unparseable amount -> (None, None)")
|
|
|
|
print("6. the most recent live recurrence wins when a member has history")
|
|
old = order("MONTHLY", "ACTIVE", 5, "2024-01-01T00:00:00.000Z")
|
|
new = order("MONTHLY", "ACTIVE", 10, "2026-11-17T01:52:56.450Z")
|
|
check(_current_dues([new, old]) == (10.0, "MONTHLY"),
|
|
"the newest live monthly ($10) wins over an older $5 monthly")
|
|
|
|
print()
|
|
if FAILS:
|
|
print(f"dues level: {len(FAILS)} FAILURE(S)")
|
|
sys.exit(1)
|
|
print("dues level: all checks passed")
|