Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions backend/espn/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ def _members_by_id(payload: dict) -> dict:
return members


# Per-team waiver priority lives on the team object in ESPN's payload
# (it is state, not a setting, so it is NOT in the acquisitionSettings
# block). No recorded payload in this repo carries it yet, so the read
# is defensive: the canonical key first, then the plausible variants,
# each accepted only as a plain int. Anything else degrades to None —
# the rank is ESPN state and is never derived from standings/record.
_WAIVER_RANK_KEYS = ("waiverRank", "currentWaiverRank", "waiver_rank")


def _waiver_rank(team: dict):
for key in _WAIVER_RANK_KEYS:
value = team.get(key)
if isinstance(value, int) and not isinstance(value, bool):
return value
return None


def _slim_team(team: dict, members: dict) -> dict:
record = ((team.get("record") or {}).get("overall")) or {}
streak_raw = record.get("streak") or {}
Expand All @@ -84,6 +101,7 @@ def _slim_team(team: dict, members: dict) -> dict:
"points_for": record.get("pointsFor"),
"points_against": record.get("pointsAgainst"),
"streak": streak,
"waiver_rank": _waiver_rank(team),
}


Expand Down
26 changes: 26 additions & 0 deletions backend/tests/test_espn_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,32 @@ def test_get_league_acquisition_settings_absent(client):
}


def _payload_with_teams(teams):
return dict(LEAGUE_PAYLOAD, teams=teams)


def test_get_league_waiver_rank_parsed():
teams = [dict(t) for t in LEAGUE_PAYLOAD["teams"]]
teams[0]["waiverRank"] = 2
teams[1]["currentWaiverRank"] = 1
# teams[2] carries no waiver field at all
res = logic.get_league(FakeClient(payload=_payload_with_teams(teams)))
assert [t["waiver_rank"] for t in res["teams"]] == [2, 1, None]
# pre-existing keys are untouched
alpha = res["teams"][0]
assert alpha["name"] == "Alpha"
assert alpha["wins"] == 8
assert alpha["streak"] == "W2"


def test_get_league_waiver_rank_absent_is_none(client):
# the default fixture carries no per-team waiver field: the rank
# degrades to None on every team instead of disappearing (and is
# never invented from standings or record)
res = logic.get_league(client)
assert [t["waiver_rank"] for t in res["teams"]] == [None, None, None]


def test_get_standings_order_and_rank(client):
res = logic.get_standings(client)
assert res["count"] == 3
Expand Down
Loading