Coverage for mt_ai/tools/deps.py: 78.70%

62 statements  

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

1"""What the MT AI tools need from the data layer, and nothing more. 

2 

3The tools depend on these Protocols rather than on the DAO classes, so tests 

4pass small hand-written fakes and the real DAOs satisfy them unchanged. 

5""" 

6 

7from collections.abc import Iterable 

8from dataclasses import dataclass 

9from datetime import datetime 

10from typing import Protocol 

11 

12 

13class TeamSource(Protocol): 

14 def get_all_teams(self) -> list[dict]: ... 14 ↛ exitline 14 didn't return from function 'get_all_teams' because

15 

16 def resolve_team_by_name(self, name: str, league_id: int | None = None) -> dict | None: ... 16 ↛ exitline 16 didn't return from function 'resolve_team_by_name' because

17 

18 

19class ClubSource(Protocol): 

20 def get_all_clubs(self, include_team_counts: bool = True) -> list[dict]: ... 20 ↛ exitline 20 didn't return from function 'get_all_clubs' because

21 

22 

23class LeagueSource(Protocol): 

24 def get_all_leagues(self, include_test: bool = False) -> list[dict]: ... 24 ↛ exitline 24 didn't return from function 'get_all_leagues' because

25 

26 

27class MatchSource(Protocol): 

28 def get_all_matches( 28 ↛ exitline 28 didn't return from function 'get_all_matches' because

29 self, 

30 season_id: int | None = None, 

31 age_group_id: int | None = None, 

32 division_id: int | None = None, 

33 team_id: int | None = None, 

34 match_type: str | None = None, 

35 start_date: str | None = None, 

36 end_date: str | None = None, 

37 include_test: bool = False, 

38 *, 

39 raise_on_error: bool = False, 

40 ) -> list[dict]: ... 

41 

42 def get_season_results( 42 ↛ exitline 42 didn't return from function 'get_season_results' because

43 self, 

44 season_id: int, 

45 age_group_id: int, 

46 league_ids: list[int] | tuple[int, ...], 

47 match_type_ids: list[int] | tuple[int, ...], 

48 statuses: tuple[str, ...] = ("completed", "forfeit"), 

49 include_test: bool = False, 

50 ) -> list[dict]: ... 

51 

52 def team_has_matches_in_season(self, team_id: int, season_id: int, include_test: bool = False) -> bool: ... 52 ↛ exitline 52 didn't return from function 'team_has_matches_in_season' because

53 

54 # get_team_dossier's for_match_id (SB-1780). None for an unknown match; with 

55 # raise_on_error a failed read raises instead of looking like an unknown match. 

56 def get_match_by_id( 56 ↛ exitline 56 didn't return from function 'get_match_by_id' because

57 self, match_id: int, include_test: bool = False, *, raise_on_error: bool = False 

58 ) -> dict | None: ... 

59 

60 def get_standings( 60 ↛ exitline 60 didn't return from function 'get_standings' because

61 self, 

62 season_id: int | None = None, 

63 age_group_id: int | None = None, 

64 division_id: int | None = None, 

65 match_type: str = "League", 

66 include_test: bool = False, 

67 *, 

68 raise_on_error: bool = False, 

69 ) -> dict: ... 

70 

71 

72class LiveMatchSource(Protocol): 

73 def get_live_match_state(self, match_id: int, include_test: bool = False) -> dict | None: ... 73 ↛ exitline 73 didn't return from function 'get_live_match_state' because

74 

75 

76class MatchEventSource(Protocol): 

77 def get_key_events(self, match_id: int) -> list[dict] | None: ... 77 ↛ exitline 77 didn't return from function 'get_key_events' because

78 

79 

80class RosterSource(Protocol): 

81 def get_player_by_id(self, player_id: int) -> dict | None: ... 81 ↛ exitline 81 didn't return from function 'get_player_by_id' because

82 

83 

84class MotwSource(Protocol): 

85 def list_for_week(self, week_start: str, *, include_test: bool = False) -> list[dict]: ... 85 ↛ exitline 85 didn't return from function 'list_for_week' because

86 

87 

88class PowerRatingSource(Protocol): 

89 """The stored MT Power Rating runs (`dao.power_rating_dao.PowerRatingDAO`, SB-1808).""" 

90 

91 def latest_run(self, season_id: int, age_group_id: int, league_id: int) -> dict | None: ... 91 ↛ exitline 91 didn't return from function 'latest_run' because

92 

93 def run_as_of(self, season_id: int, age_group_id: int, league_id: int, at: datetime) -> dict | None: ... 93 ↛ exitline 93 didn't return from function 'run_as_of' because

94 

95 def run_payload(self, run_id: int) -> dict | None: ... 95 ↛ exitline 95 didn't return from function 'run_payload' because

96 

97 def runs(self, season_id: int, age_group_id: int, league_id: int) -> list[dict]: ... 97 ↛ exitline 97 didn't return from function 'runs' because

98 

99 def team_ratings(self, team_id: int, run_ids: Iterable[int]) -> list[dict]: ... 99 ↛ exitline 99 didn't return from function 'team_ratings' because

100 

101 def team_deltas(self, run_id: int, team_id: int) -> list[dict]: ... 101 ↛ exitline 101 didn't return from function 'team_deltas' because

102 

103 def matches_by_ids(self, match_ids: Iterable[int]) -> list[dict]: ... 103 ↛ exitline 103 didn't return from function 'matches_by_ids' because

104 

105 def massey_ranks(self, run_id: int) -> dict[int, int]: ... 105 ↛ exitline 105 didn't return from function 'massey_ranks' because

106 

107 

108class SeasonSource(Protocol): 

109 def get_current_season(self) -> dict | None: ... 109 ↛ exitline 109 didn't return from function 'get_current_season' because

110 

111 def get_all_seasons(self) -> list[dict]: ... 111 ↛ exitline 111 didn't return from function 'get_all_seasons' because

112 

113 

114@dataclass(frozen=True) 

115class ToolDeps: 

116 teams: TeamSource 

117 clubs: ClubSource 

118 leagues: LeagueSource 

119 matches: MatchSource 

120 seasons: SeasonSource 

121 # get_live_match only (SB-1584). Optional so callers that never ask about a live 

122 # match don't need them; the tool reports "unavailable" without them. 

123 live: LiveMatchSource | None = None 

124 events: MatchEventSource | None = None 

125 roster: RosterSource | None = None 

126 # suggest_motw_candidates only (SB-1551): the week's pick and the recent ones. 

127 motw: MotwSource | None = None 

128 # get_power_rating / get_power_rating_team only (SB-1808): the stored runs. 

129 power_rating: PowerRatingSource | None = None 

130 

131 

132def dao_tool_deps() -> ToolDeps: 

133 """The production data sources: one shared Supabase connection, real DAOs.""" 

134 # Imported here: the DAOs pull in Supabase and Redis, which the tool tests never need. 

135 from dao.club_dao import ClubDAO 

136 from dao.league_dao import LeagueDAO 

137 from dao.match_dao import MatchDAO, SupabaseConnection 

138 from dao.match_event_dao import MatchEventDAO 

139 from dao.motw_dao import MotwDAO 

140 from dao.power_rating_dao import PowerRatingDAO 

141 from dao.roster_dao import RosterDAO 

142 from dao.season_dao import SeasonDAO 

143 from dao.team_dao import TeamDAO 

144 

145 conn = SupabaseConnection() 

146 matches = MatchDAO(conn) 

147 return ToolDeps( 

148 teams=TeamDAO(conn), 

149 clubs=ClubDAO(conn), 

150 leagues=LeagueDAO(conn), 

151 matches=matches, 

152 seasons=SeasonDAO(conn), 

153 live=matches, 

154 events=MatchEventDAO(conn), 

155 roster=RosterDAO(conn), 

156 motw=MotwDAO(conn), 

157 power_rating=PowerRatingDAO(conn), 

158 )