def test_tie_goes_to_highest_priority_two_ties(): roster1 = Roster(id="1", name="team 1", rank=1, positions=deepcopy(league_positions)) roster2 = Roster(id="2", name="team 2", rank=2, positions=deepcopy(league_positions)) roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=player1, amount=5)) roster2.waiver_bids.append( WaiverBid(roster_id=roster2.id, player=player1, amount=5)) roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=player2, amount=5)) roster2.waiver_bids.append( WaiverBid(roster_id=roster2.id, player=player2, amount=5)) target = WaiverService(get_roster_player_service()) target.process_bids([roster1, roster2]) assert roster1.waiver_bids[ 0].result == WaiverBidResult.FailedLowerPriority, "Assert 1 failed" assert roster2.waiver_bids[ 0].result == WaiverBidResult.SuccessPending, "Assert 2 failed" assert roster1.waiver_bids[ 1].result == WaiverBidResult.SuccessPending, "Assert 3 failed" assert roster2.waiver_bids[ 1].result == WaiverBidResult.FailedLowerPriority, "Assert 4 failed"
def test_random_winner(): rosters = [Roster(name="one"), Roster(name="two")] winners = [] for x in range(1000): rosters = Roster.sort(rosters) sleep(0.001) winners.append(rosters[0].name) assert "one" in winners assert "two" in winners
def test_waiver_priority_is_reverse_rank(): roster1 = Roster(id="1", name="team 1", rank=1) roster2 = Roster(id="2", name="team 2", rank=3) roster3 = Roster(id="3", name="team 3", rank=4) roster4 = Roster(id="4", name="team 4", rank=2) rosters = [roster1, roster2, roster3, roster4] target = WaiverService(get_roster_player_service()) waiver_priority = target.get_initial_waiver_priority(rosters) assert waiver_priority[0] == "3" assert waiver_priority[1] == "2" assert waiver_priority[2] == "4" assert waiver_priority[3] == "1"
def test_drop_player_must_still_be_on_roster_already_dropped(): roster1 = Roster(id="1", name="team 1", rank=1, positions=deepcopy(league_positions)) roster1.positions["1"].player = player2 roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=player1, drop_player=player2, amount=25)) roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=player3, drop_player=player2, amount=15)) target = WaiverService(get_roster_player_service()) target.process_bids([roster1]) assert roster1.waiver_bids[ 1].result == WaiverBidResult.FailedDropPlayerNotOnRoster
def test_drop_transaction_is_created(): roster1 = Roster(id="1", name="team 1", rank=1, positions=deepcopy(league_positions)) roster1.positions["2"].player = rb2 roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=rb1, drop_player=rb2, amount=25)) command = ProcessWaiversCommand(league_id=league.id) executor = get_target([roster1]) executor.execute(command) transaction_repo = executor.waiver_service.roster_player_service.league_transaction_repo transactions = transaction_repo.firestore.get_all(None) transaction = next( (x for x in transactions if x.type == TransactionType.DROP_PLAYER and x.player_id == rb2.id), None) assert transaction, "No drop transaction found"
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_cannot_change_while_drafting(): league = League.construct(id="league1", draft_state=DraftState.IN_PROGRESS, 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=roster.id) result = command_executor.execute(command) assert not result.success
def test_teams_with_no_bids_work_ok(): roster1 = Roster(id="1", name="team 1", rank=1, positions=deepcopy(league_positions)) roster2 = Roster(id="2", name="team 2", rank=2, positions=deepcopy(league_positions)) roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=player1, amount=5)) target = WaiverService(get_roster_player_service()) target.process_bids([roster1, roster2]) assert roster1.waiver_bids[0].result == WaiverBidResult.SuccessPending
def test_roster_budget_not_updated_failed_bids(): roster1 = Roster(id="1", name="team 1", rank=1, positions=deepcopy(league_positions)) roster2 = Roster(id="2", name="team 2", rank=2, positions=deepcopy(league_positions)) roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=player1, amount=5)) roster2.waiver_bids.append( WaiverBid(roster_id=roster2.id, player=player1, amount=4)) target = WaiverService(get_roster_player_service()) target.process_bids([roster1, roster2]) assert roster2.waiver_budget == 100
def test_highest_bid_wins_second_team(): roster1 = Roster(id="1", name="team 1", rank=1, positions=deepcopy(league_positions)) roster2 = Roster(id="2", name="team 2", rank=2, positions=deepcopy(league_positions)) roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=player1, amount=4)) roster2.waiver_bids.append( WaiverBid(roster_id=roster2.id, player=player1, amount=5)) target = WaiverService(get_roster_player_service()) target.process_bids([roster1, roster2]) assert roster1.waiver_bids[0].result == WaiverBidResult.FailedOutBid assert roster2.waiver_bids[0].result == WaiverBidResult.SuccessPending
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_cannot_exceed_waiver_budget_one_bid(): roster1 = Roster(id="1", name="team 1", rank=1, waiver_budget=20, positions=deepcopy(league_positions)) roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=player1, amount=25)) target = WaiverService(get_roster_player_service()) target.process_bids([roster1]) assert roster1.waiver_bids[ 0].result == WaiverBidResult.FailedNotEnoughMoney
def test_bids_are_saved(): roster1 = Roster(id="1", name="team 1", rank=1, positions=deepcopy(league_positions)) roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=rb1, amount=25)) command = ProcessWaiversCommand(league_id=league.id) executor = get_target([roster1]) executor.execute(command) updated_week = executor.league_week_repo.get(league.id, str(state.current_week - 1)) assert updated_week and updated_week is not None, "Bids were not saved on week"
def test_roster_budget_updated(): # create two bids without drop players, no conflicts, ensure they both work roster1 = Roster(id="1", name="team 1", waiver_budget=100, rank=1, positions=deepcopy(league_positions)) roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=rb1, amount=25)) command = ProcessWaiversCommand(league_id=league.id) executor = get_target([roster1]) executor.execute(command) roster = executor.league_roster_repo.get(league.id, roster1.id) assert roster.waiver_budget == 75, "Waiver budget was not updated"
def test_drop_player_must_still_be_on_roster(): roster1 = Roster(id="1", name="team 1", rank=1, positions=deepcopy(league_positions)) # note that player 2 is not actually in a position on roster1 roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=player1, drop_player=player2, amount=25)) target = WaiverService(get_roster_player_service()) target.process_bids([roster1]) assert roster1.waiver_bids[ 0].result == WaiverBidResult.FailedDropPlayerNotOnRoster
def test_process_two_successful_bids(): # create two bids without drop players, no conflicts, ensure they both work roster1 = Roster(id="1", name="team 1", rank=1, positions=deepcopy(league_positions)) roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=rb1, amount=25)) roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=wr1, amount=15)) command = ProcessWaiversCommand(league_id=league.id) executor = get_target([roster1]) executor.execute(command) assert roster1.processed_waiver_bids[ 0].result == WaiverBidResult.Success, "assert 1 failed" assert roster1.processed_waiver_bids[ 1].result == WaiverBidResult.Success, "assert 2 failed"
def test_no_drop_player_no_space_on_roster(): # create two bids without drop players, same position, only one position available # first should pass, second should fail as there is no space on roster after processing the first one roster1 = Roster(id="1", name="team 1", rank=1, positions=deepcopy(league_positions)) roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=rb1, amount=25)) roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=rb3, amount=15)) command = ProcessWaiversCommand(league_id=league.id) executor = get_target([roster1]) executor.execute(command) assert roster1.processed_waiver_bids[ 0].result == WaiverBidResult.Success, "assert 1 failed" assert roster1.processed_waiver_bids[ 1].result == WaiverBidResult.FailedNoRosterSpace, "assert 2 failed"
def test_add_transaction_is_created(): # create two bids without drop players, no conflicts, ensure they both work roster1 = Roster(id="1", name="team 1", rank=1, positions=deepcopy(league_positions)) roster1.waiver_bids.append( WaiverBid(roster_id=roster1.id, player=rb1, amount=25)) command = ProcessWaiversCommand(league_id=league.id) executor = get_target([roster1]) executor.execute(command) transaction_repo = executor.waiver_service.roster_player_service.league_transaction_repo transactions = transaction_repo.firestore.get_all(None) transaction = next( (x for x in transactions if x.type == TransactionType.CLAIM_PLAYER and x.player_id == rb1.id), None) assert transaction, "No add transaction found"
from typing import List from api.app.domain.entities.roster import Roster from api.app.domain.entities.schedule import MatchupType, PlayoffType import pytest from api.app.domain.services.schedule_service import ScheduledMatchup, combine_schedule, generate_schedule, get_sequence_for_count from api.tests.asserts import are_equal SEASON_WEEKS = 21 # MANAGERS teams = [ Roster(id=1, name="team 1"), Roster(id=2, name="team 2"), Roster(id=3, name="team 3"), Roster(id=4, name="team 4"), Roster(id=5, name="team 5"), Roster(id=6, name="team 6"), Roster(id=7, name="team 7"), Roster(id=8, name="team 8"), ] teamCounts = [2, 4, 6, 8] playoffTypes = [ { "type": PlayoffType.TOP_2, "weeks": 1 }, { "type": PlayoffType.TOP_3, "weeks": 2 },
from typing import List from api.app.domain.entities.roster import Roster import pytest from time import sleep case1 = [ # purely by points ([Roster(name="one", wins=1), Roster(name="two", wins=0, losses=1)], "one"), ([Roster(name="two", wins=0, losses=1), Roster(name="one", wins=1)], "one"), # by points for ([Roster(name="one", points_for=100), Roster(name="two", points_for=50)], "one"), ([Roster(name="one", points_for=50), Roster(name="two", points_for=150)], "two"), # by point differential ([Roster(name="one", points_for=100, points_against=50), Roster(name="two", points_for=100, points_against=75)], "one"), ([Roster(name="one", points_for=100, points_against=50), Roster(name="two", points_for=100, points_against=25)], "two"), ] @pytest.mark.parametrize("rosters, first", case1) def test_sort_roster(rosters: List[Roster], first): for roster in rosters: roster.wl_points = Roster.calculate_wl_points(roster) rosters = Roster.sort(rosters) assert rosters[0].name == first
import pytest from api.tests.asserts import are_equal from api.app.domain.entities.roster import Roster from api.app.domain.entities.schedule import Matchup, PlayoffType, ScheduleWeek from api.app.domain.enums.matchup_type import MatchupType from api.app.domain.enums.week_type import WeekType from api.app.domain.services.schedule_service import generate_playoffs # MANAGERS team_1 = Roster(id=1, name="team 1", rank=1) team_2 = Roster(id=2, name="team 2", rank=2) team_3 = Roster(id=3, name="team 3", rank=3) team_4 = Roster(id=4, name="team 4", rank=4) team_5 = Roster(id=5, name="team 5", rank=5) team_6 = Roster(id=6, name="team 6", rank=6) team_7 = Roster(id=7, name="team 7", rank=7) team_8 = Roster(id=8, name="team 8", rank=8) teams = [ team_1, team_2, team_3, team_4, team_5, team_6, team_7, team_8, ]
def create_roster(user_id, display_name) -> Roster: return Roster(id=user_id, name=f"Team {display_name}")