Skip to content

Commit

Permalink
fix: bug fixes and better test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
DesiPilla committed Dec 20, 2023
1 parent a5edc82 commit da3da43
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 70 deletions.
47 changes: 25 additions & 22 deletions espn_api/football/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def build_h2h_dict(team_data_list: List[Dict]) -> Dict:
# Create a dictionary with each team's head to head record
h2h_outcomes = {
team_data["team_id"]: {
opp["team_id"]: {"wins": 0, "h2h_games": 0} for opp in team_data_list
opp["team_id"]: {"h2h_wins": 0, "h2h_games": 0} for opp in team_data_list
}
for team_data in team_data_list
}
Expand All @@ -54,30 +54,26 @@ def build_h2h_dict(team_data_list: List[Dict]) -> Dict:

# Add the outcome to the dictionary
if outcome == "W":
h2h_outcomes[team.team_id][opp.team_id]["wins"] += 1
h2h_outcomes[team.team_id][opp.team_id]["h2h_wins"] += 1
if outcome == "T":
h2h_outcomes[team.team_id][opp.team_id]["wins"] += 0.5
h2h_outcomes[team.team_id][opp.team_id]["h2h_wins"] += 0.5

h2h_outcomes[team.team_id][opp.team_id]["h2h_games"] += 1

# Calculate the head to head record
h2h_record = {
team_data["team_id"]: {
opp_data["team_id"]: (
h2h_outcomes[team_data["team_id"]][opp_data["team_id"]]["wins"]
/ max(
h2h_outcomes[team_data["team_id"]][opp_data["team_id"]][
"h2h_games"
],
1,
)
)
for opp_data in team_data_list
}
for team_data in team_data_list
}
# # Calculate the head to head record
# for team_data in team_data_list:
# for opp_data in team_data_list:
# h2h_outcomes[team_data["team_id"]][opp_data["team_id"]]["h2h_record"] = (
# h2h_outcomes[team_data["team_id"]][opp_data["team_id"]]["h2h_wins"]
# / max(
# h2h_outcomes[team_data["team_id"]][opp_data["team_id"]][
# "h2h_games"
# ],
# 1,
# )
# )

return h2h_record
return h2h_outcomes


def sort_by_win_pct(team_data_list: List[Dict]) -> List[Dict]:
Expand Down Expand Up @@ -126,8 +122,12 @@ def sort_by_head_to_head(
# Filter the H2H DataFrame to only include the teams in question
h2h_dict = build_h2h_dict(team_data_list)

# Sum the H2H wins against all tied opponents
for team_data in team_data_list:
team_data["h2h_wins"] = h2h_dict[team_data["team_id"]]["h2h_wins"]
team_data["h2h_wins"] = sum(
h2h_dict[team_data["team_id"]][opp_id]["h2h_wins"]
for opp_id in h2h_dict.keys()
)
return sorted(team_data_list, key=lambda x: x["h2h_wins"], reverse=True)

# If there are more than two teams...
Expand All @@ -145,7 +145,10 @@ def sort_by_head_to_head(
# All teams have played each other an equal number of times
# Sort the teams by total H2H wins against each other
for team_data in team_data_list:
team_data["h2h_wins"] = h2h_dict[team_data["team_id"]]["h2h_wins"]
team_data["h2h_wins"] = sum(
h2h_dict[team_data["team_id"]][opp_id]["h2h_wins"]
for opp_id in h2h_dict.keys()
)
return sorted(team_data_list, key=lambda x: x["h2h_wins"], reverse=True)
else:
# All teams have not played each other an equal number of times
Expand Down
113 changes: 65 additions & 48 deletions tests/football/unit/test_league.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,57 +111,70 @@ def test_standings_weekly(self, m):
# self.assertEqual(week13_standings, final_standings)

@requests_mock.Mocker()
def test_standings_helpers(self, m):
def test_build_h2h_dict(self, m):
self.mock_setUp(m)

league = League(self.league_id, self.season)

def get_list_of_team_data(league, week):
list_of_team_data = []
for team in league.teams:
team_data = {
"team": team,
"team_id": team.team_id,
"division_id": team.division_id,
"wins": sum(
[1 for outcome in team.outcomes[:week] if outcome == "W"]
),
"ties": sum(
[1 for outcome in team.outcomes[:week] if outcome == "T"]
),
"losses": sum(
[1 for outcome in team.outcomes[:week] if outcome == "L"]
),
"points_for": sum(team.scores[:week]),
"points_against": sum(
[team.schedule[w].scores[w] for w in range(week)]
),
"schedule": team.schedule[:week],
"outcomes": team.outcomes[:week],
}
team_data["win_pct"] = (
team_data["wins"] + team_data["ties"] / 2
) / sum(
[
1
for outcome in team.outcomes[:week]
if outcome in ["W", "T", "L"]
]
)
list_of_team_data.append(team_data)
return list_of_team_data
# Test build_h2h_dict and build_division_record_dict
# Week 1
## Get data for teams 1 and 7
week1_teams_data = self.get_list_of_team_data(league, 1)
list_of_team_data = [
team for team in week1_teams_data if team["team_id"] in (1, 7)
]
h2h_dict = build_h2h_dict(list_of_team_data)

self.assertEqual(h2h_dict[1][7]["h2h_wins"], 0) # Team 1 is 0/1 vs Team 7
self.assertEqual(h2h_dict[7][1]["h2h_wins"], 1) # Team 7 is 1/1 vs Team 1
self.assertEqual(h2h_dict[1][7]["h2h_games"], 1) # Team 1 is 0/1 vs Team 7
self.assertEqual(h2h_dict[7][1]["h2h_games"], 1) # Team 7 is 1/1 vs Team 1

## Test 3 teams head-to-head
list_of_team_data = [
team for team in week1_teams_data if team["team_id"] in (1, 2, 3)
]
h2h_dict = build_h2h_dict(list_of_team_data)
self.assertEqual(h2h_dict[1][2]["h2h_games"], 0) # Teams have not played
self.assertEqual(h2h_dict[1][3]["h2h_games"], 0) # Teams have not played
self.assertEqual(h2h_dict[2][3]["h2h_games"], 0) # Teams have not played

# Week 10
## Get data for teams 1 and 7
week10_teams_data = self.get_list_of_team_data(league, 10)
list_of_team_data = [
team for team in week10_teams_data if team["team_id"] in (1, 7)
]
h2h_dict = build_h2h_dict(list_of_team_data)

self.assertEqual(h2h_dict[1][7]["h2h_wins"], 1) # Team 1 is 1/2 vs Team 7
self.assertEqual(h2h_dict[7][1]["h2h_wins"], 1) # Team 7 is 1/2 vs Team 1
self.assertEqual(h2h_dict[1][7]["h2h_games"], 2) # Team 1 is 0/1 vs Team 7
self.assertEqual(h2h_dict[7][1]["h2h_games"], 2) # Team 7 is 1/1 vs Team 1

# Test 3 teams head-to-head
list_of_team_data = [
team for team in week10_teams_data if team["team_id"] in (1, 2, 3)
]
h2h_dict = build_h2h_dict(list_of_team_data)
self.assertEqual(h2h_dict[1][2]["h2h_games"], 1) # Teams have played 1x
self.assertEqual(h2h_dict[1][3]["h2h_games"], 1) # Teams have played 1x
self.assertEqual(h2h_dict[2][3]["h2h_games"], 1) # Teams have played 1x

@requests_mock.Mocker()
def test_build_division_records_dict(self, m):
self.mock_setUp(m)

league = League(self.league_id, self.season)

# Test build_h2h_dict and build_division_record_dict
# Week 1 - get data for teams 1 and 7
week1_teams_data = get_list_of_team_data(league, 1)
week1_teams_data = self.get_list_of_team_data(league, 1)
list_of_team_data = [
team for team in week1_teams_data if team["team_id"] in (1, 7)
]
h2h_dict = build_h2h_dict(list_of_team_data)
division_record_dict = build_division_record_dict(list_of_team_data)

self.assertEqual(h2h_dict[1][7], 0) # Team 1 has 0 wins out of 1 against Team 7
self.assertEqual(h2h_dict[7][1], 1) # Team 7 has 1 win out of 1 against Team 1
self.assertEqual(division_record_dict[1], 0)
self.assertEqual(
division_record_dict[7],
Expand All @@ -173,19 +186,12 @@ def get_list_of_team_data(league, week):
)

# Week 10 - get data for teams 1 and 7
week10_teams_data = get_list_of_team_data(league, 10)
week10_teams_data = self.get_list_of_team_data(league, 10)
list_of_team_data = [
team for team in week10_teams_data if team["team_id"] in (1, 7)
]
h2h_dict = build_h2h_dict(list_of_team_data)
division_record_dict = build_division_record_dict(week10_teams_data)

self.assertEqual(
h2h_dict[1][7], 0.5
) # Team 1 has 1 win out of 2 against Team 7
self.assertEqual(
h2h_dict[7][1], 0.5
) # Team 7 has 1 win out of 2 against Team 1
self.assertEqual(division_record_dict[1], 0.6)
self.assertEqual(
division_record_dict[7],
Expand All @@ -196,7 +202,18 @@ def get_list_of_team_data(league, week):
][0],
)

# Test sorting functions
@requests_mock.Mocker()
def test_sort_functions(self, m):
self.mock_setUp(m)

league = League(self.league_id, self.season)

week10_teams_data = self.get_list_of_team_data(league, 10)
list_of_team_data = [
team for team in week10_teams_data if team["team_id"] in (1, 2)
]
division_record_dict = build_division_record_dict(week10_teams_data)

# Assert that sort_by_win_pct is correct
sorted_list_of_team_data = sort_by_win_pct(week10_teams_data)
for i in range(len(sorted_list_of_team_data) - 1):
Expand Down

0 comments on commit da3da43

Please sign in to comment.