Exemple #1
0
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
Exemple #2
0
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_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"
Exemple #4
0
def test_cannot_exceed_waiver_budget_combined_bids():
    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=15))
    roster1.waiver_bids.append(
        WaiverBid(roster_id=roster1.id, player=player2, amount=10))

    target = WaiverService(get_roster_player_service())

    target.process_bids([roster1])

    assert roster1.waiver_bids[0].result == WaiverBidResult.SuccessPending
    assert roster1.waiver_bids[
        1].result == WaiverBidResult.FailedNotEnoughMoney
Exemple #5
0
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_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"
Exemple #7
0
def test_highest_bid_wins_first_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=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 roster1.waiver_bids[0].result == WaiverBidResult.SuccessPending
    assert roster2.waiver_bids[0].result == WaiverBidResult.FailedOutBid
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"
Exemple #9
0
def test_tie_bids_different_players_both_succeed():

    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=player2, amount=5))

    target = WaiverService(get_roster_player_service())

    target.process_bids([roster1, roster2])

    assert roster1.waiver_bids[0].result == WaiverBidResult.SuccessPending
    assert roster2.waiver_bids[0].result == WaiverBidResult.SuccessPending
Exemple #10
0
    def apply_winning_bid(self, league_id: str, bid: WaiverBid, roster: Roster,
                          transaction: Transaction):
        if bid.drop_player:
            target_position = roster.find_player_position(bid.drop_player.id)
        else:
            target_position = self.roster_player_service.find_position_for(
                bid.player, roster)

        if not target_position:
            bid.result = WaiverBidResult.FailedNoRosterSpace
        else:
            success, _ = self.roster_player_service.assign_player_to_roster(
                league_id,
                roster,
                bid.player,
                transaction,
                target_position=target_position,
                record_transaction=True,
                waiver_bid=bid.amount)

            if success:
                bid.result = WaiverBidResult.Success
            else:
                bid.result = WaiverBidResult.FailedNoRosterSpace
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"
Exemple #12
0
    def record_winning_bid(
        self,
        winning_bid: WaiverBid,
        rosters: Dict[str, Roster],
        waiver_priority: List[str],
        dropped_players_for_roster: Dict[str, List[str]],
    ):
        winning_bid.result = WaiverBidResult.SuccessPending

        index = waiver_priority.index(winning_bid.roster_id)
        waiver_priority.pop(index)
        waiver_priority.append(winning_bid.roster_id)

        rosters[winning_bid.roster_id].waiver_budget -= winning_bid.amount

        if winning_bid.drop_player:
            dropped_players_for_roster[winning_bid.roster_id].append(
                winning_bid.drop_player.id)
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"
Exemple #14
0
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_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"
Exemple #16
0
        def update(transaction):
            league = self.league_repo.get(command.league_id, transaction)
            roster = self.league_roster_repo.get(command.league_id,
                                                 command.roster_id,
                                                 transaction)

            if not roster:
                return AddPlayerResult(command=command,
                                       error="Roster not found")

            current_owner = self.league_owned_players_repo.get(
                command.league_id, command.player_id, transaction)

            if current_owner:
                return AddPlayerResult(
                    command=command,
                    error="That player is already on a roster")

            player = self.player_repo.get(self.season, command.player_id,
                                          transaction)

            if not player:
                return AddPlayerResult(command=command,
                                       error="Player not found")

            if state.locks.is_locked(player.team):
                return AddPlayerResult(
                    command=command,
                    error=f"{player.team.location} players are locked")

            target_position = None
            if command.drop_player_id:
                target_position = roster.find_player_position(
                    command.drop_player_id)

            if state.waivers_active:
                bid = WaiverBid(roster_id=command.roster_id,
                                player=player,
                                amount=command.bid)
                if command.drop_player_id:
                    bid.drop_player = target_position.player

                roster.waiver_bids.append(bid)
                roster.waiver_bids.sort(key=lambda x: x.amount, reverse=True)
                self.league_roster_repo.update(command.league_id, roster,
                                               transaction)

                return AddPlayerResult(command=command)

            elif not state.waivers_active and league.waivers_active:
                return AddPlayerResult(
                    command=command,
                    error=
                    "Waivers are still being processed for your league, please try again in a few minutes. "
                    +
                    "If you see this message for more than a few minutes, please contact the site administrator."
                )

            else:
                if not target_position:
                    target_position = self.roster_player_service.find_position_for(
                        player, roster)

                if not target_position:
                    return AddPlayerResult(
                        command=command,
                        error=
                        f"There is no space on roster for a {player.position.display_name()}"
                    )

                success, info = self.roster_player_service.assign_player_to_roster(
                    league_id=command.league_id,
                    roster=roster,
                    player=player,
                    target_position=target_position,
                    record_transaction=True,
                    transaction=transaction)

                if success:
                    return AddPlayerResult(command=command,
                                           league=league,
                                           transactions=info)
                else:
                    return AddPlayerResult(command=command, error=info)