Beispiel #1
0
        def set_schedule(transaction):
            rosters = self.league_roster_repo.get_all(command.league_id,
                                                      transaction)

            weeks = generate_schedule(self.season_weeks, rosters,
                                      command.first_playoff_week,
                                      command.playoff_type,
                                      command.enable_loser_playoff)

            schedule = Schedule(
                weeks=weeks,
                playoff_type=command.playoff_type,
                first_playoff_week=command.first_playoff_week,
                enable_loser_playoff=command.enable_loser_playoff)

            league = self.league_repo.get(command.league_id, transaction)

            if league.draft_state != DraftState.NOT_STARTED:
                return GenerateScheduleResult(
                    command=command,
                    error="Schedule cannot be changed after league has started"
                )

            league.schedule_generated = True
            league.registration_closed = True

            self.league_repo.update(league, transaction)
            self.league_config_repo.set_schedule_config(
                command.league_id, schedule, transaction)

            return GenerateScheduleResult(command=command, schedule=schedule)
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_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_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_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_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)