Coverage for dao/coverage_dao.py: 100.00%

46 statements  

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

1"""Is MT actually tracking what it thinks it is? (SB-1021) 

2 

3Everything else in the product answers "what is here" — `get_competitions_present`, 

4`get_leagues_present`, `/api/match-types/available` — and every one of them 

5derives from fixtures that arrived. None of them can report a conference that 

6sent nothing, because absence produces no row to count. 

7 

8That blind spot cost a season. On 2026-09-06 MT held fixtures for 13 of 13 Flex 

9conferences and 4 of 4 Pro Player Pathway divisions, and 2 of the 8 Homegrown 

10League conferences. Nothing in the product said so. 

11 

12This reads the other half — `division_age_groups`, what the league is expected 

13to run — and diffs it against what arrived. 

14""" 

15 

16from __future__ import annotations 

17 

18from typing import Any 

19 

20import structlog 

21 

22from dao.base_dao import BaseDAO 

23 

24logger = structlog.get_logger() 

25 

26COVERED = "covered" 

27EMPTY = "empty" 

28UNEXPECTED = "unexpected" 

29UNDECLARED = "undeclared" 

30 

31 

32def _status(row: dict[str, Any], league_declared: bool) -> str: 

33 """Four outcomes, and the distinction between the last two matters. 

34 

35 `empty` is a conference the league runs that sent nothing — the finding 

36 this whole module exists for. 

37 

38 `unexpected` and `undeclared` both mean "fixtures arrived for something not 

39 on file", and collapsing them would raise an alarm about correct data. In a 

40 league with expectations, unlisted fixtures are a real surprise: the feed 

41 has changed shape. In a league with NO expectations, they are simply all we 

42 have — MT holds 49 real Academy fixtures and no statement of what the 

43 Academy Division runs, and calling those "unexpected" would report good 

44 data as a defect. 

45 """ 

46 if row.get("is_expected"): 

47 return COVERED if (row.get("fixtures") or 0) > 0 else EMPTY 

48 return UNEXPECTED if league_declared else UNDECLARED 

49 

50 

51class CoverageDAO(BaseDAO): 

52 """Expected competition coverage, diffed against reality.""" 

53 

54 def report(self, season_id: int) -> dict[str, Any]: 

55 """Coverage for one season: every row, plus totals, plus per league. 

56 

57 Returns an empty report rather than raising. This backs an admin screen 

58 and a CLI command; a diagnostic that takes down the page it reports on 

59 is worse than no diagnostic. 

60 """ 

61 try: 

62 response = self.client.rpc( 

63 "competition_coverage", {"p_season_id": season_id} 

64 ).execute() 

65 rows = response.data or [] 

66 except Exception: 

67 logger.exception("Could not read competition coverage", season_id=season_id) 

68 return { 

69 "season_id": season_id, 

70 "rows": [], 

71 "summary": { 

72 "expected": 0, 

73 "covered": 0, 

74 "empty": 0, 

75 "unexpected": 0, 

76 "undeclared": 0, 

77 }, 

78 "leagues": [], 

79 "available": False, 

80 } 

81 

82 # Which leagues have any expectations at all has to be known before a 

83 # row's status can be decided, so it is computed first over the whole 

84 # set rather than per row. 

85 declared = { 

86 row.get("league_name") 

87 for row in rows 

88 if row.get("is_expected") 

89 } 

90 for row in rows: 

91 row["status"] = _status(row, row.get("league_name") in declared) 

92 

93 return { 

94 "season_id": season_id, 

95 "rows": rows, 

96 "summary": self._summary(rows), 

97 "leagues": self._by_league(rows), 

98 "available": True, 

99 } 

100 

101 def empty_count(self, season_id: int) -> int: 

102 """How many expected conference/age-group pairs got nothing. 

103 

104 The single number worth putting on an admin badge. 

105 """ 

106 return self.report(season_id)["summary"]["empty"] 

107 

108 @staticmethod 

109 def _summary(rows: list[dict[str, Any]]) -> dict[str, int]: 

110 # `expected` is the denominator, and it ships with every summary so no 

111 # caller can state a coverage figure without it (CLAUDE.md rule 3). 

112 return { 

113 "expected": sum(1 for r in rows if r.get("is_expected")), 

114 "covered": sum(1 for r in rows if r["status"] == COVERED), 

115 "empty": sum(1 for r in rows if r["status"] == EMPTY), 

116 "unexpected": sum(1 for r in rows if r["status"] == UNEXPECTED), 

117 "undeclared": sum(1 for r in rows if r["status"] == UNDECLARED), 

118 } 

119 

120 @staticmethod 

121 def _by_league(rows: list[dict[str, Any]]) -> list[dict[str, Any]]: 

122 """Per-league totals, and whether the league declared anything at all. 

123 

124 `declared: false` is not the same as full coverage, and the difference 

125 matters: the Academy Division runs its own MLS NEXT Cup with separate 

126 U15-U19 finals, and MT holds one conference of it. Reporting that as 

127 "covered" because nothing contradicts it would be the report lying by 

128 omission — so a league with no expectations on file says so. 

129 """ 

130 leagues: dict[str, dict[str, Any]] = {} 

131 for row in rows: 

132 name = row.get("league_name") or "Unknown" 

133 entry = leagues.setdefault( 

134 name, 

135 { 

136 "league": name, 

137 "expected": 0, 

138 "covered": 0, 

139 "empty": 0, 

140 "unexpected": 0, 

141 "undeclared": 0, 

142 "divisions": set(), 

143 }, 

144 ) 

145 entry["divisions"].add(row.get("division_name")) 

146 if row.get("is_expected"): 

147 entry["expected"] += 1 

148 entry[row["status"]] += 1 

149 

150 out = [] 

151 for entry in leagues.values(): 

152 entry["divisions"] = len(entry["divisions"]) 

153 entry["declared"] = entry["expected"] > 0 

154 out.append(entry) 

155 return sorted(out, key=lambda e: e["league"])