Coverage for notifications/score_change.py: 100.00%

39 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2026-11-30 15:10 +0000

1"""Detect when a match write represents a *new final score* worth notifying. 

2 

3Used by the admin/skill match-write endpoints (tournament create/update, and 

4the regular league update/patch path) to decide whether to fan a "fulltime" 

5notification out to followers. (SB-77) 

6 

7This is a pure function with no I/O so it can be unit-tested directly. 

8 

9A write qualifies as a notify-worthy scoring event when, AFTER the write, the 

10match has both a home and away score, AND either: 

11 

12 - the (home, away) score pair actually changed vs. before the write 

13 (covers scheduled→scored, and an admin correcting a wrong score), or 

14 - the match newly transitioned into a completed status carrying a score 

15 (covers the rare case where the score was already present but the match 

16 only just got marked completed). 

17 

18It deliberately does NOT fire when: 

19 

20 - the write only touched metadata (round / group / order / kickoff / team 

21 identity) and left the score untouched — the common upsert-loop case, and 

22 - an identical score is re-submitted (idempotent re-runs of the skill), and 

23 - the match has no score yet, or is still in a non-completed state. 

24""" 

25 

26from __future__ import annotations 

27 

28from datetime import date, timedelta 

29 

30# A final score posted later than this is a backfill or a season load, not news. 

31FINAL_ALERT_WINDOW_DAYS = 3 

32 

33 

34def _status(match: dict) -> str | None: 

35 """Read the status under either key the row may expose. 

36 

37 Raw `matches` rows use `match_status`; some joined read shapes alias it to 

38 `status`. Accept both so detection is robust to which path produced the dict. 

39 """ 

40 return match.get("match_status") or match.get("status") 

41 

42 

43def _scores(match: dict) -> tuple[int | None, int | None]: 

44 return match.get("home_score"), match.get("away_score") 

45 

46 

47def is_new_final_score(old: dict | None, new: dict | None) -> bool: 

48 """Return True if `new` represents a fresh final score relative to `old`. 

49 

50 `old` is the match row before the write (None for a create); `new` is the 

51 row after the write. Both are plain dicts straight off the matches table. 

52 """ 

53 if not new: 

54 return False 

55 

56 new_home, new_away = _scores(new) 

57 if new_home is None or new_away is None: 

58 return False # no complete score yet — nothing to announce 

59 

60 new_status = _status(new) 

61 # If a status is present it must be 'completed'; a partial/in-progress 

62 # write is not a final score. (None status is tolerated — tournament rows 

63 # may omit it; the score-pair check below still gates the notification.) 

64 if new_status is not None and new_status != "completed": 

65 return False 

66 

67 old = old or {} 

68 old_home, old_away = _scores(old) 

69 old_status = _status(old) 

70 

71 if (old_home, old_away) != (new_home, new_away): 

72 return True # score newly set or corrected 

73 

74 # Same score, but only just marked completed → fire once. Otherwise it's an 

75 # unchanged score on an already-completed match — no-op. 

76 return old_status != "completed" and new_status == "completed" 

77 

78 

79def final_alert_blocker(match: dict | None, today: date) -> str | None: 

80 """Why a final-score alert must not go out for this match, or None if it may (SB-1619). 

81 

82 Used by POST /api/matches/{id}/final-score-alert, which the match-scraper 

83 worker calls after writing a new score. Pure, so the rules are unit-tested: 

84 

85 - `not_found`: no such match. 

86 - `no_score`: either score is missing. 

87 - `not_completed`: a status is set and it is not `completed`. 

88 - `too_old`: dated more than FINAL_ALERT_WINDOW_DAYS before today (a 

89 backfill or season load must not alert anyone), or in the future. 

90 """ 

91 if not match: 

92 return "not_found" 

93 home, away = _scores(match) 

94 if home is None or away is None: 

95 return "no_score" 

96 status = _status(match) 

97 if status is not None and status != "completed": 

98 return "not_completed" 

99 raw = str(match.get("match_date") or "")[:10] 

100 try: 

101 played = date.fromisoformat(raw) 

102 except ValueError: 

103 return "too_old" 

104 if played > today or today - played > timedelta(days=FINAL_ALERT_WINDOW_DAYS): 

105 return "too_old" 

106 return None