Esempio n. 1
0
def test_team_delete(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/team/{team_id}' page is requested (DELETE)
    THEN check that the response is successful
    """
    # Create a new player
    player_id = test_utils.create_player(choice(test_utils.PLAYER_NAMES))

    # Create a new team
    team_id = test_utils.create_team(choice(test_utils.TEAM_NAMES))

    # Create a new teamplayer
    test_utils.create_teamplayer(team_id, player_id)

    # Verify the database agrees.
    db_response = team.read_all()
    assert db_response is not None
    team_id_list = [response["team_id"] for response in db_response]
    assert team_id in team_id_list

    with app.test_client() as test_client:
        # Attempt to access the delete round api
        response = test_client.delete(f"/api/team/{team_id}")
        assert response.status == "200 OK"

        # Attempt to retrieve the now-deleted round id
        response = test_client.get(f"/api/team/{team_id}")
        assert response.status == "404 NOT FOUND"

    # Verify the database agrees.
    with pytest.raises(exceptions.NotFound):
        team.read_one(team_id)
Esempio n. 2
0
def test_update_bid_low(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/play/{round_id}/submit_bid' page is requested (PUT)
    THEN check that the response contains the expected information
    """
    # Create a new game
    game_id = test_utils.create_game(kitty_size=0)

    # Create a new round
    round_id = test_utils.create_round(game_id)

    # Create new players
    player_ids = []
    for __ in range(len(test_utils.PLAYER_NAMES)):
        player_id = test_utils.create_player(choice(test_utils.PLAYER_NAMES))
        player_ids.append(player_id)
        player.update(player_id, {"bidding": True})

    # Create a new team
    team_ids = []
    for __ in range(len(test_utils.TEAM_NAMES)):
        team_id = test_utils.create_team(choice(test_utils.TEAM_NAMES))
        team_ids.append(team_id)

    # Join the player to the team.
    for idx, player_id in enumerate(player_ids):
        test_utils.create_teamplayer(team_ids[idx % 2], player_id)

    # Join the teams to the round.
    test_utils.create_roundteam(round_id, team_ids)

    # Set the first dealer
    # Maybe...
    temp_list = utils.query_player_ids_for_round(round_id)
    assert len(temp_list) == 4

    # Populate the bid
    bid = -2
    player_id = choice(player_ids)
    with app.test_client() as test_client:
        # Attempt to access the get round api
        response = test_client.put(
            f"/api/play/{round_id}/submit_bid?player_id={player_id}&bid={bid}")
        assert response.status == "409 CONFLICT"
        response_str = response.get_data(as_text=True)
        assert "low" in response_str

        # Verify the database is unchanged
        temp_round = utils.query_round(round_id)
        assert temp_round.bid == 20
        assert temp_round.bid_winner is None
        print(f"score={temp_round.bid}")
Esempio n. 3
0
def test_update_bid(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/play/{round_id}/submit_bid' page is requested (PUT)
    THEN check that the response contains the expected information
    """
    # Create a new game
    game_id = test_utils.create_game(kitty_size=0)

    # Create a new round
    round_id = test_utils.create_round(game_id)

    # Create new players
    player_ids = []
    for __ in range(len(test_utils.PLAYER_NAMES)):
        player_id = test_utils.create_player(choice(test_utils.PLAYER_NAMES))
        player_ids.append(player_id)
        player.update(player_id, {"bidding": True})

    # Create new teams
    team_ids = []
    for __ in range(len(test_utils.TEAM_NAMES)):
        team_id = test_utils.create_team(choice(test_utils.TEAM_NAMES))
        team_ids.append(team_id)

    # Join the players to the teams.
    for idx, player_id in enumerate(player_ids):
        test_utils.create_teamplayer(team_ids[idx % 2], player_id)

    # Join the teams to the round.
    test_utils.create_roundteam(round_id, team_ids)

    # Populate the bid
    bid = 21
    player_id = choice(player_ids)
    with app.test_client() as test_client:
        # Attempt to access the get round api
        response = test_client.put(
            f"/api/play/{round_id}/submit_bid?player_id={player_id}&bid={bid}")
        assert response.status == "200 OK"
        response_str = response.get_data(as_text=True)
        assert "bid_winner" in response_str
        assert "bid" in response_str
        response_data = json.loads(response_str)
        db_bid = response_data.get("bid")
        db_player = response_data.get("bid_winner")
        assert db_bid == bid
        assert db_player == player_id
        assert isinstance(db_bid, int)
        print(f"score={db_bid}")
def test_roundteam_addcard(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round/{round_id}/{team_id}' page is requested (PUT)
    THEN check that the response contains the expected information
    """
    # Create a new game
    game_id = test_utils.create_game(4)

    # Create a new round
    round_id = test_utils.create_round(game_id)

    # Create a new teams
    team_ids = []
    for __ in range(2):
        team_id = test_utils.create_team(choice(test_utils.TEAM_NAMES))
        team_ids.append(team_id)

    # Create the roundteam association for the teams.
    roundteams.create(round_id=round_id, teams=team_ids)

    # Choose a team to receive the new card
    team_id = choice(team_ids)

    with app.test_client() as test_client:
        # Attempt to access the addcard roundteam api
        put_data = {}

        response = test_client.put(
            f"/api/round/{round_id}/{team_id}?card={choice(test_utils.CARD_LIST)}",
            data=json.dumps(put_data),
            content_type="application/json",
        )
        assert response.status == "201 CREATED"
        # This is a JSON formatted STRING
        response_str = response.get_data(as_text=True)
        assert response_str is not None
        response_data = json.loads(response_str)
        hand_id = response_data.get("hand_id")
        assert hand_id != ""
        assert test_utils.UUID_REGEX.match(hand_id)

    # Verify the database agrees.
    db_response = roundteams.read(round_id, team_id)
    assert db_response is not None
    assert round_id == db_response.get("round_id")
    assert hand_id == str(db_response.get("team_cards")[0].hand_id)
def test_roundteam_readall(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round/{round_id}/{team_id}' page is requested (GET)
    THEN check that the response contains the expected information
    """
    # Clear out ALL previous test data.
    db.drop_all()
    db.create_all()

    # Create a new game
    game_id = test_utils.create_game(4)

    # Create a new round
    round_id = test_utils.create_round(game_id)

    # Create a new teams
    team_ids = []
    for __ in range(2):
        team_id = test_utils.create_team(choice(test_utils.TEAM_NAMES))
        team_ids.append(team_id)

    # Create the roundteam association for the teams.
    roundteams.create(round_id=round_id, teams=team_ids)

    # Verify the association was created in the database.
    db_response = roundteams.read(round_id=round_id, team_id=team_id)
    assert db_response is not None

    with app.test_client() as test_client:
        # Attempt to access the readall roundteam api
        response = test_client.get(f"/api/round/{round_id}/{team_id}")
        assert response.status == "200 OK"

    # Verify the database agrees.
    db_response = roundteams.read_all()
    team_hand = None
    assert db_response is not None
    for item in db_response:
        assert round_id == item["round_id"]
        assert item["team_id"] in team_ids
        if team_hand is None:
            team_hand = item["hand_id"]
        else:
            assert team_hand != item["hand_id"]
Esempio n. 6
0
def test_team_add_player(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/team/{team_id}' page is requested (POST)
    THEN check that the response is a UUID and contains the expected information
    """
    # Create a new team and player
    player_name = choice(test_utils.PLAYER_NAMES)
    team_name = choice(test_utils.TEAM_NAMES)

    # Create a new player
    player_id = test_utils.create_player(player_name)

    # Create a new team
    team_id = test_utils.create_team(team_name)

    with app.test_client() as test_client:
        # Attempt to access the create player api
        post_data = {
            "player_id": player_id,
        }
        response = test_client.post(
            f"/api/team/{team_id}",
            data=json.dumps(post_data),
            content_type="application/json",
        )
        assert response.status == "201 CREATED"
        assert response.get_data(as_text=True) is not None
        # This is a JSON formatted STRING
        response_str = response.get_data(as_text=True)
        response_data = json.loads(response_str)
        team_id = response_data.get("team_id")
        assert team_id != ""
        assert test_utils.UUID_REGEX.match(team_id)

        # Verify the database agrees.
        db_response = team.read_one(team_id)
        assert db_response is not None
        assert team_id == db_response.get("team_id")
        assert db_response.get("score") == 0
        assert team_name == db_response.get("name")
def test_roundteam_delcard(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round/{round_id}/{team_id}/{card}' page is requested (DELETE)
    THEN check that the response contains the expected information
    """
    # Create a new game
    game_id = test_utils.create_game(4)
    print(f"game_id={game_id}")

    # Create a new round
    round_id = test_utils.create_round(game_id)
    print(f"round_id={round_id}")

    # Create a new teams
    team_ids = []
    for __ in range(2):
        team_id = test_utils.create_team(choice(test_utils.TEAM_NAMES))
        team_ids.append(team_id)

    # Create the roundteam association for the teams.
    roundteams.create(round_id=round_id, teams=team_ids)

    # Choose a team to receive the new card
    team_id = choice(team_ids)

    # Add a card to the team's collection.
    chosen_card = choice(test_utils.CARD_LIST)
    roundteams.addcard(round_id, team_id, chosen_card)

    with app.test_client() as test_client:
        # Attempt to access the deletecard roundteam api
        response = test_client.delete(
            f"/api/round/{round_id}/{team_id}/{chosen_card}")
        assert response.status == "200 OK"

    hand_id = test_utils.query_team_hand_id(round_id=round_id, team_id=team_id)
    # Verify the database agrees.
    db_response = hand.read_one(hand_id)
    assert db_response is None
def test_roundteam_delete(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round/{round_id}/{team_id}' page is requested (DELETE)
    THEN check that the response contains the expected information
    """
    # Create a new game
    game_id = test_utils.create_game(4)
    print(f"game_id={game_id}")

    # Create a new round
    round_id = test_utils.create_round(game_id)
    print(f"round_id={round_id}")

    # Create a new teams
    team_ids = []
    for __ in range(2):
        team_id = test_utils.create_team(choice(test_utils.TEAM_NAMES))
        team_ids.append(team_id)

    # Create the roundteam association for the teams.
    roundteams.create(round_id=round_id, teams=team_ids)

    # Verify the association was created in the database.
    db_response = roundteams.read(round_id=round_id, team_id=team_id)
    assert db_response is not None

    with app.test_client() as test_client:
        # Attempt to access the delete roundteam api
        response = test_client.delete(f"/api/round/{round_id}/{team_id}")
        assert response.status == "200 OK"

    # Verify the database agrees.
    with pytest.raises(exceptions.NotFound):
        db_response = roundteams.read(round_id, team_id)
        assert db_response is not None