def test_can_update_when_not_started(): league = League.construct(id="league1") league_repo = LeagueRepository(MockFirestoreProxy()) league_repo.create(league) scoring = ScoringSettings.create_default() league_config_repo = LeagueConfigRepository(MockFirestoreProxy()) league_config_repo.set_scoring_config(league.id, scoring) state_repo = StateRepository(MockFirestoreProxy()) transaction_repo = LeagueTransactionRepository(MockFirestoreProxy()) locks = Locks() state = State.construct(locks=locks) state_repo.set(state) command_executor = UpdateLeagueScoringCommandExecutor(league_repo, league_config_repo, state_repo, transaction_repo) command = UpdateLeagueScoringCommand(league_id=league.id, **scoring.dict()) result = command_executor.execute(command) expected = True actual = result.success are_equal(expected, actual)
def test_update_by_commisssioner(): league = League.construct(id="league1", draft_state=DraftState.NOT_STARTED, name="Test League", commissioner_id="commish") roster = Roster(id="roster1", name="Mock Roster") user_league = UserLeaguePreview.create(roster, league) roster_repo = get_league_roster_repo(roster) league_repo = get_league_repo(league) user_league_repo = get_user_league_repo(user_league) command_executor = UpdateRosterNameCommandExecutor( league_config_repo=get_league_config_repo, league_repo=league_repo, league_roster_repo=roster_repo, league_week_matchup_repo=get_league_week_matchup_repo(), user_league_repo=user_league_repo, publisher=get_publisher(), league_transaction_repo=get_league_transaction_repo()) new_name = "Update Roster Name" command = UpdateRosterNameCommand(league_id=league.id, roster_id=roster.id, roster_name=new_name, current_user_id=league.commissioner_id) result = command_executor.execute(command) assert result.success expected = new_name actual = roster_repo.get(league.id, roster.id).name are_equal(expected, actual)
def test_assign_winners_week2_away_win_home_win(): previous_week = ScheduleWeek(week_number=1, week_type=WeekType.PLAYOFFS) previous_week.matchups = [ Matchup(id="01", away=team_3, home=team_2, type=MatchupType.PLAYOFF, away_score=100, home_score=50), Matchup(id="02", away=team_4, home=team_1, type=MatchupType.PLAYOFF, away_score=50, home_score=100) ] current_week = ScheduleWeek(week_number=2, week_type=WeekType.CHAMPIONSHIP) current_week.matchups = [Matchup(id="01", type=MatchupType.CHAMPIONSHIP)] current_week.assign_playoff_matchups(PlayoffType.TOP_4, teams, previous_week) matchup = current_week.matchups[0] are_equal(matchup.away, team_3) are_equal(matchup.home, team_1)
def test_league_preview_has_matchup(): commissioner = get_commissioner() user_repo = get_user_repo() league_repo = get_league_repo() user_league_repo = get_user_league_repo() league_roster_repo = get_league_roster_repo() league_config_repo = get_league_config_repo() publisher = VirtualPubSubPublisher("test_project") command_executor = CreateLeagueCommandExecutor(user_repo, league_repo, user_league_repo, league_roster_repo, league_config_repo, publisher) command = CreateLeagueCommand(commissioner_id=commissioner.id, name="Test League", private=False) result = command_executor.execute(command) user_league = user_league_repo.get(commissioner.id, result.league.id) user_roster = league_roster_repo.get(result.league.id, commissioner.id) expected = user_roster.name actual = user_league.matchup.home.name assert expected is not None are_equal(expected, actual)
def test_position_count_is_updated(position): mock_league = League(id="league1", name="test league", commissioner_id="user1", created=datetime.now(), draft_state=DraftState.NOT_STARTED, draft_type=DraftType.SNAKE, private=False, positions=LeaguePositionsConfig().create_positions(), draft_order=[DraftOrder(roster_id="user1")]) league_repo = LeagueRepository(MockFirestoreProxy()) league_repo.create(mock_league) league_config_repo = LeagueConfigRepository(MockFirestoreProxy()) league_config_repo.set_positions_config(mock_league.id, LeaguePositionsConfig()) d = {e: 1 for e in PositionType.all()} d[position] = 0 d["league_id"] = mock_league.id command = UpdateLeaguePositionsCommand.parse_obj(d) command_executor = UpdateLeaguePositionsCommandExecutor( league_repo, league_config_repo) command_executor.execute(command) updated_config = league_config_repo.get_positions_config(mock_league.id) expected = 0 actual = updated_config.dict()[position] are_equal(expected, actual)
def test_league_added_to_user(): commissioner = get_commissioner() user_repo = get_user_repo() league_repo = get_league_repo() user_league_repo = get_user_league_repo() league_roster_repo = get_league_roster_repo() league_config_repo = get_league_config_repo() publisher = VirtualPubSubPublisher("test_project") command_executor = CreateLeagueCommandExecutor(user_repo, league_repo, user_league_repo, league_roster_repo, league_config_repo, publisher) command = CreateLeagueCommand(commissioner_id=commissioner.id, name="Test League", private=False) result = command_executor.execute(command) user_league = user_league_repo.get(commissioner.id, result.league.id) expected = result.league.id actual = user_league.id are_equal(expected, actual)
def test_combine_schedule(): season_weeks = 5 regular_season = [1, 2, 3, 4, 5] playoffs = [1, 2] expected = 5 # max for entire season actual = len(combine_schedule(season_weeks, regular_season, playoffs)) are_equal(expected, actual)
def test_correct_week_count_for_playoff_type(team_count, playoff_type): active_teams = teams[0:team_count] first_playoff_week = 7 enable_loser_playoff = False expected = first_playoff_week - 1 + playoff_type["weeks"] schedule = generate_schedule(SEASON_WEEKS, active_teams, first_playoff_week, playoff_type["type"], enable_loser_playoff) actual = len(schedule) are_equal(expected, actual)
def test_1_match_per_team_per_week(team_count): weeks = get_sequence_for_count(team_count) bad_weeks = [] for week in weeks: for x in range(0, team_count): matchups_for_team = [m for m in week if m.home == x or m.away == x] if len(matchups_for_team) != 1: bad_weeks.append(week) are_equal(0, len(bad_weeks))
def test_week_ids_match(team_count, playoff_type): active_teams = teams[0:team_count] first_playoff_week = 20 enable_loser_playoff = False schedule = generate_schedule(SEASON_WEEKS, active_teams, first_playoff_week, playoff_type["type"], enable_loser_playoff) week_number = 1 for week in schedule: are_equal(week_number, week.week_number) week_number += 1
def test_cannot_exceed_season_length(): active_teams = teams[0:6] schedule = generate_schedule(season_weeks=21, rosters=active_teams, first_playoff_week=21, playoff_type=PlayoffType.TOP_4, enable_loser_playoff=False) expected = 21 actual = len(schedule) are_equal(expected, actual)
def test_week1_assign_playoff_matchups_top2_no_loser_game(team_count: int): rosters = teams[0:team_count] previous_week = ScheduleWeek(week_number=0, week_type=WeekType.REGULAR) playoffs = generate_playoffs(team_count, PlayoffType.TOP_2, first_playoff_week=2, enable_loser_playoff=False) week = playoffs[0] week.assign_playoff_matchups(PlayoffType.TOP_2, rosters, previous_week) championship = week.matchups[0] are_equal(team_2, championship.away) are_equal(team_1, championship.home)
def test_week3_assign_playoff_matchups_top6(home_wins: bool): previous_week = ScheduleWeek(week_number=0, week_type=WeekType.REGULAR) playoffs = generate_playoffs(teams, PlayoffType.TOP_6, first_playoff_week=2, enable_loser_playoff=False) week1 = playoffs[0] # setup week 1 week1.assign_playoff_matchups(PlayoffType.TOP_6, teams, previous_week) for matchup in week1.matchups: if matchup.type != MatchupType.PLAYOFF_BYE: matchup.home_score = 100 if home_wins else 0 matchup.away_score = 100 if not home_wins else 0 week2 = playoffs[1] week2.assign_playoff_matchups(PlayoffType.TOP_6, teams, week1) for matchup in week2.matchups: matchup.home_score = 100 if home_wins else 0 matchup.away_score = 100 if not home_wins else 0 week3 = playoffs[2] week3.assign_playoff_matchups(PlayoffType.TOP_6, teams, week2) matchup = week3.matchups[0] if home_wins: are_equal(team_1.id, matchup.home.id) are_equal(team_2.id, matchup.away.id) else: are_equal(team_5.id, matchup.home.id) are_equal(team_6.id, matchup.away.id)
def test_update_roster_name_updates_schedule_config_home(): week_number = 1 league = League.construct(id="league1", draft_state=DraftState.NOT_STARTED, name="Test League", schedule_generated=True, commissioner_id="commish") roster = Roster(id="roster1", name="Mock Roster") user_league = UserLeaguePreview.create(roster, league) schedule = create_mock_schedule(week_number) roster_repo = get_league_roster_repo(roster) league_repo = get_league_repo(league) user_league_repo = get_user_league_repo(user_league) matchup_repo = get_league_week_matchup_repo() config_repo = get_league_config_repo() matchup = Matchup(id="1", home=roster, type=MatchupType.REGULAR) matchup_repo.set(league.id, week_number, matchup) schedule.weeks[0].matchups.append(matchup) config_repo.set_schedule_config(league.id, schedule) command_executor = UpdateRosterNameCommandExecutor( league_config_repo=config_repo, league_repo=league_repo, league_roster_repo=roster_repo, league_week_matchup_repo=matchup_repo, user_league_repo=user_league_repo, publisher=get_publisher(), league_transaction_repo=get_league_transaction_repo()) new_name = "Update Roster Name" command = UpdateRosterNameCommand(league_id=league.id, roster_id=roster.id, roster_name=new_name, current_user_id=roster.id) result = command_executor.execute(command) assert result.success expected = new_name updated_schedule = config_repo.get_schedule_config(league.id) actual = updated_schedule.weeks[0].matchups[0].home.name are_equal(expected, actual)
def test_correct_matchup_counts_per_week(team_count): active_teams = teams[0:team_count] # irrelevant for the test, but required for the function playoff_type = PlayoffType.TOP_2 first_playoff_week = 5 enable_loser_playoff = False expected = team_count / 2 schedule = generate_schedule(SEASON_WEEKS, active_teams, first_playoff_week, playoff_type, enable_loser_playoff) actual = len(schedule[0].matchups) are_equal(expected, actual)
def test_week1_assign_playoff_matchups_top3_loser_game(team_count: int): rosters = teams[0:team_count] previous_week = ScheduleWeek(week_number=0, week_type=WeekType.REGULAR) playoffs = generate_playoffs(team_count, PlayoffType.TOP_3, first_playoff_week=2, enable_loser_playoff=True) week = playoffs[0] week.assign_playoff_matchups(PlayoffType.TOP_3, rosters, previous_week) loser_game = week.matchups[2] last_place = rosters[-1] second_last = rosters[-2] are_equal(last_place, loser_game.away) are_equal(second_last, loser_game.home)
def test_update_display_name(): user = User(id="user1", display_name="Test Guy", email="*****@*****.**", login_type=LoginType.EMAIL) user_repo = get_user_repo(user) publisher = VirtualPubSubPublisher("test_project") command_executor = UpdateProfileCommandExecutor(user_repo, publisher) command = UpdateProfileCommand(uid=user.id, display_name="Test Guy Updated", current_user_id=user.id) result = command_executor.execute(command) assert result.success expected = command.display_name actual = user_repo.get(user.id).display_name are_equal(expected, actual)
def test_last_game_start(): scoreboard = Scoreboard( games={ "1": ScoreboardGame.construct( date_start=datetime(2021, 7, 1, 12, 00)), "2": ScoreboardGame.construct( date_start=datetime(2021, 7, 1, 16, 00)), "3": ScoreboardGame.construct( date_start=datetime(2021, 7, 2, 19, 00)), "4": ScoreboardGame.construct( date_start=datetime(2021, 7, 3, 18, 00)), }) expected = scoreboard.games["4"].date_start actual = scoreboard.last_game_start_time() are_equal(expected, actual)
def test_can_generate_loser_league_game(): active_teams = teams[0:4] schedule = generate_schedule(season_weeks=21, rosters=active_teams, first_playoff_week=5, playoff_type=PlayoffType.TOP_2, enable_loser_playoff=True) playoff_week = schedule[-1] loser_game = [ m for m in playoff_week.matchups if m.type == MatchupType.LOSER ] expected = 1 actual = len(loser_game) are_equal(expected, actual)
def test_no_duplicate_matchups(team_count): weeks = get_sequence_for_count(team_count) checked_matchups = [] # type:List[ScheduledMatchup] duplicates_found = [] for week in weeks: for game in week: matches = [ m for m in checked_matchups if m.home == game.home and m.away == game.away ] if matches: duplicates_found.extend(matches) else: checked_matchups.append(game) are_equal(0, len(duplicates_found))
def test_returns_error_when_no_league(): league_repo = LeagueRepository(MockFirestoreProxy()) league_config_repo = LeagueConfigRepository(MockFirestoreProxy()) state_repo = StateRepository(MockFirestoreProxy()) transaction_repo = LeagueTransactionRepository(MockFirestoreProxy()) locks = Locks() state = State.construct(locks=locks) state_repo.set(state) command_executor = UpdateLeagueScoringCommandExecutor(league_repo, league_config_repo, state_repo, transaction_repo) command = UpdateLeagueScoringCommand.construct(league_id="league1") result = command_executor.execute(command) expected = False actual = result.success are_equal(expected, actual)
def test_no_loser_game_if_not_enough_teams(): active_teams = teams[0:4] # all four make the playoffs, can't have a loser game schedule = generate_schedule(season_weeks=21, rosters=active_teams, first_playoff_week=5, playoff_type=PlayoffType.TOP_4, enable_loser_playoff=True) playoff_week = schedule[-1] loser_game = [ m for m in playoff_week.matchups if m.type == MatchupType.LOSER ] expected = 0 actual = len(loser_game) are_equal(expected, actual)
def test_equal_number_of_matchups_per_team(team_count): weeks = get_sequence_for_count(team_count) game_count = {} for week in weeks: for x in range(0, team_count): matchups_for_team = [m for m in week if m.home == x or m.away == x] if x not in game_count: game_count[x] = 0 game_count[x] += len(matchups_for_team) expected_count = game_count[0] all_equal = True for team_id in range(0, team_count): count = game_count[team_id] if count != expected_count: all_equal = False are_equal(True, all_equal)
def test_week1_assign_playoff_matchups_top3_no_loser_game(team_count: int): rosters = teams[0:team_count] previous_week = ScheduleWeek(week_number=0, week_type=WeekType.REGULAR) weeks = generate_playoffs(team_count, PlayoffType.TOP_3, first_playoff_week=2, enable_loser_playoff=False) week1 = weeks[0] week1.assign_playoff_matchups(PlayoffType.TOP_3, rosters, previous_week) matchup = week1.matchups[0] are_equal(team_3, matchup.away) are_equal(team_2, matchup.home) bye = week1.matchups[1] are_equal(None, bye.away) are_equal(team_1, bye.home)
def test_week1_assign_playoff_matchups_top6_no_loser_game(team_count: int): rosters = teams[0:team_count] previous_week = ScheduleWeek(week_number=0, week_type=WeekType.REGULAR) weeks = generate_playoffs(team_count, PlayoffType.TOP_6, first_playoff_week=2, enable_loser_playoff=False) week1 = weeks[0] week1.assign_playoff_matchups(PlayoffType.TOP_6, rosters, previous_week) matchup1 = week1.matchups[0] are_equal(team_6, matchup1.away) are_equal(team_3, matchup1.home) matchup2 = week1.matchups[1] are_equal(team_5, matchup2.away) are_equal(team_4, matchup2.home) matchup3 = week1.matchups[2] are_equal(None, matchup3.away) are_equal(team_1, matchup3.home) matchup4 = week1.matchups[3] are_equal(None, matchup4.away) are_equal(team_2, matchup4.home)
def test_hours_since(start, end, expected): actual = hours_since(start, end) are_equal(expected, actual)
def test_any_locks_works_with_all_teams(locked_team: Team, expected: bool): locks = Locks(**locked_team) actual = locks.any_locks() are_equal(expected, actual)
def test_is_game_complete(expected: bool, game: ScoreboardGame): actual = game.is_complete() are_equal(expected, actual)
def test_by_id(team_id, expected): actual = Team.by_id(team_id).abbreviation are_equal(expected, actual)
def test_by_abbreviation(abbreviation, expected): actual = Team.by_abbreviation(abbreviation).abbreviation are_equal(expected, actual)