221 lines
6.9 KiB
Python
221 lines
6.9 KiB
Python
from tests.conftest import make_game
|
|
from app.games import (
|
|
calculate_game_priority,
|
|
convert_game_state,
|
|
format_record,
|
|
get_game_outcome,
|
|
get_period,
|
|
get_power_play_info,
|
|
get_start_time,
|
|
get_time_remaining,
|
|
parse_games,
|
|
utc_to_eastern,
|
|
)
|
|
|
|
|
|
class TestConvertGameState:
|
|
def test_off_maps_to_final(self):
|
|
assert convert_game_state("OFF") == "FINAL"
|
|
|
|
def test_crit_maps_to_live(self):
|
|
assert convert_game_state("CRIT") == "LIVE"
|
|
|
|
def test_fut_maps_to_pre(self):
|
|
assert convert_game_state("FUT") == "PRE"
|
|
|
|
def test_unknown_state_passes_through(self):
|
|
assert convert_game_state("LIVE") == "LIVE"
|
|
|
|
|
|
class TestProcessRecord:
|
|
def test_na_returns_na(self):
|
|
assert format_record("N/A") == "N/A"
|
|
|
|
def test_pads_single_digit_parts(self):
|
|
assert format_record("5-3-1") == "05-03-01"
|
|
|
|
def test_already_padded_unchanged(self):
|
|
assert format_record("40-25-10") == "40-25-10"
|
|
|
|
|
|
class TestProcessPeriod:
|
|
def test_pre_game_returns_zero(self):
|
|
game = make_game(game_state="PRE")
|
|
assert get_period(game) == 0
|
|
|
|
def test_fut_game_returns_zero(self):
|
|
game = make_game(game_state="FUT")
|
|
assert get_period(game) == 0
|
|
|
|
def test_final_game_returns_na(self):
|
|
game = make_game(game_state="OFF")
|
|
assert get_period(game) == "N/A"
|
|
|
|
def test_live_game_returns_period_number(self):
|
|
game = make_game(game_state="LIVE", period=2)
|
|
assert get_period(game) == 2
|
|
|
|
|
|
class TestProcessTimeRemaining:
|
|
def test_pre_game_returns_2000(self):
|
|
game = make_game(game_state="FUT")
|
|
assert get_time_remaining(game) == "20:00"
|
|
|
|
def test_final_game_returns_0000(self):
|
|
game = make_game(game_state="OFF")
|
|
assert get_time_remaining(game) == "00:00"
|
|
|
|
def test_live_game_returns_clock(self):
|
|
game = make_game(game_state="LIVE", seconds_remaining=305)
|
|
assert get_time_remaining(game) == "05:05"
|
|
|
|
def test_live_game_at_zero_returns_end(self):
|
|
game = make_game(game_state="LIVE", seconds_remaining=0)
|
|
assert get_time_remaining(game) == "END"
|
|
|
|
|
|
class TestProcessStartTime:
|
|
def test_pre_game_returns_est_time(self):
|
|
game = make_game(game_state="FUT", start_time_utc="2024-04-10T23:00:00Z")
|
|
result = get_start_time(game)
|
|
assert result == "7:00 PM"
|
|
|
|
def test_pre_game_strips_leading_zero(self):
|
|
game = make_game(game_state="FUT", start_time_utc="2024-04-10T22:00:00Z")
|
|
result = get_start_time(game)
|
|
assert not result.startswith("0")
|
|
|
|
def test_live_game_returns_na(self):
|
|
game = make_game(game_state="LIVE")
|
|
assert get_start_time(game) == "N/A"
|
|
|
|
|
|
class TestGetGameOutcome:
|
|
def test_final_game_returns_last_period_type(self):
|
|
game = make_game(game_state="OFF")
|
|
assert get_game_outcome(game, "FINAL") == "REG"
|
|
|
|
def test_live_game_returns_na(self):
|
|
game = make_game(game_state="LIVE")
|
|
assert get_game_outcome(game, "LIVE") == "N/A"
|
|
|
|
|
|
class TestUtcToEstTime:
|
|
def test_converts_utc_to_est(self):
|
|
result = utc_to_eastern("2024-04-10T23:00:00Z")
|
|
assert result == "07:00 PM"
|
|
|
|
|
|
class TestParseGames:
|
|
def test_returns_empty_list_for_none(self):
|
|
assert parse_games(None) == []
|
|
|
|
def test_returns_empty_list_for_empty_dict(self):
|
|
assert parse_games({}) == []
|
|
|
|
|
|
class TestGetPowerPlayInfo:
|
|
def test_returns_empty_when_no_situation(self):
|
|
game = make_game()
|
|
assert get_power_play_info(game, "Maple Leafs") == ""
|
|
|
|
def test_returns_pp_info_for_away_team(self):
|
|
game = make_game(away_name="Bruins")
|
|
game["situation"] = {
|
|
"situationDescriptions": ["PP"],
|
|
"timeRemaining": "1:30",
|
|
}
|
|
assert get_power_play_info(game, "Bruins") == "PP 1:30"
|
|
|
|
def test_returns_pp_info_for_home_team(self):
|
|
game = make_game(home_name="Maple Leafs", away_name="Bruins")
|
|
game["situation"] = {
|
|
"situationDescriptions": ["PP"],
|
|
"timeRemaining": "0:45",
|
|
}
|
|
assert get_power_play_info(game, "Maple Leafs") == "PP 0:45"
|
|
|
|
|
|
class TestCalculateGamePriority:
|
|
def _live_game(
|
|
self,
|
|
period=3,
|
|
seconds_remaining=300,
|
|
home_score=2,
|
|
away_score=1,
|
|
in_intermission=False,
|
|
):
|
|
return make_game(
|
|
game_state="LIVE",
|
|
period=period,
|
|
seconds_remaining=seconds_remaining,
|
|
home_score=home_score,
|
|
away_score=away_score,
|
|
in_intermission=in_intermission,
|
|
)
|
|
|
|
def test_returns_zero_for_final(self):
|
|
game = make_game(game_state="OFF")
|
|
assert calculate_game_priority(game) == 0
|
|
|
|
def test_returns_zero_for_pre(self):
|
|
game = make_game(game_state="FUT")
|
|
assert calculate_game_priority(game) == 0
|
|
|
|
def test_intermission_returns_negative(self, mocker):
|
|
mocker.patch(
|
|
"app.games.get_team_standings",
|
|
return_value={"league_sequence": 0, "league_l10_sequence": 0},
|
|
)
|
|
game = self._live_game(in_intermission=True, seconds_remaining=0)
|
|
assert calculate_game_priority(game) < 0
|
|
|
|
def test_score_diff_greater_than_3(self, mocker):
|
|
mocker.patch(
|
|
"app.games.get_team_standings",
|
|
return_value={"league_sequence": 0, "league_l10_sequence": 0},
|
|
)
|
|
game = self._live_game(home_score=5, away_score=0)
|
|
result = calculate_game_priority(game)
|
|
assert isinstance(result, int)
|
|
|
|
def test_score_diff_greater_than_2(self, mocker):
|
|
mocker.patch(
|
|
"app.games.get_team_standings",
|
|
return_value={"league_sequence": 0, "league_l10_sequence": 0},
|
|
)
|
|
game = self._live_game(home_score=4, away_score=1)
|
|
result = calculate_game_priority(game)
|
|
assert isinstance(result, int)
|
|
|
|
def test_score_diff_greater_than_1(self, mocker):
|
|
mocker.patch(
|
|
"app.games.get_team_standings",
|
|
return_value={"league_sequence": 0, "league_l10_sequence": 0},
|
|
)
|
|
game = self._live_game(home_score=3, away_score=1)
|
|
result = calculate_game_priority(game)
|
|
assert isinstance(result, int)
|
|
|
|
def test_late_3rd_tied_bonus(self, mocker):
|
|
mocker.patch(
|
|
"app.games.get_team_standings",
|
|
return_value={"league_sequence": 0, "league_l10_sequence": 0},
|
|
)
|
|
game = self._live_game(
|
|
period=3, seconds_remaining=600, home_score=2, away_score=2
|
|
)
|
|
result = calculate_game_priority(game)
|
|
assert isinstance(result, int)
|
|
|
|
def test_final_6_minutes_tied_bonus(self, mocker):
|
|
mocker.patch(
|
|
"app.games.get_team_standings",
|
|
return_value={"league_sequence": 0, "league_l10_sequence": 0},
|
|
)
|
|
game = self._live_game(
|
|
period=3, seconds_remaining=300, home_score=2, away_score=2
|
|
)
|
|
result = calculate_game_priority(game)
|
|
assert isinstance(result, int)
|