コード例 #1
0
def test_players_delete_card(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/player/{player_id}' page is requested (GET)
    THEN check that the response is a UUID and contains the expected information
    """
    # Create a new player
    player_name = choice(test_utils.PLAYER_NAMES)
    player_id = test_utils.create_player(player_name)
    assert test_utils.UUID_REGEX.match(player_id)
    card_choice = choice(test_utils.CARD_LIST)
    hand_id = test_utils.query_player_hand_id(player_id=player_id)
    hand.addcard(hand_id=hand_id, card=card_choice)

    # Verify the database agrees.
    db_response = hand.read_one(hand_id)
    assert db_response is not None
    assert card_choice in db_response["cards"]

    with app.test_client() as test_client:
        # Attempt to access the read player api
        response = test_client.delete(
            f"/api/player/{player_id}/hand/{card_choice}")
        assert response.status == "200 OK"

        # Verify the database agrees.
        p_player = Player.query.filter(
            Player.player_id == player_id).one_or_none()
        # Did we find a player?
        assert p_player is not None
        hand_id = p_player.hand_id

        db_response = hand.read_one(hand_id)
        assert db_response is None
コード例 #2
0
def test_players_read_one(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/player/{player_id}' page is requested (GET)
    THEN check that the response is a UUID and contains the expected information
    """
    player_ids = {}
    # Create a new player
    for player_name in test_utils.PLAYER_NAMES:
        # Create a new game
        player_id = test_utils.create_player(player_name)
        assert test_utils.UUID_REGEX.match(player_id)
        player_ids[player_id] = player_name

    with app.test_client() as test_client:
        # Attempt to access the read player api
        for player_id in player_ids:
            response = test_client.get(f"/api/player/{player_id}")
            assert response.status == "200 OK"
            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)
            player_name = response_data.get("name")
            assert player_name is not None
            assert player_name != ""
            assert player_name == player_ids[player_id]

            # Verify the database agrees.
            db_response = player.read_one(player_id)
            assert db_response is not None
            assert player_id == db_response.get("player_id")
            assert db_response.get("meld_score") == 0
            assert player_name == db_response.get("name")
コード例 #3
0
def test_players_read_hand(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/player/{player_id}/hand' page is requested (GET)
    THEN check that the response is a list of strs
    """
    # Create a new player
    player_name = choice(test_utils.PLAYER_NAMES)
    player_id = test_utils.create_player(player_name)
    assert test_utils.UUID_REGEX.match(player_id)
    player_data = utils.query_player(player_id=player_id)

    card_qty = 5
    card_choice = []
    for _ in range(card_qty):
        temp_card = choice(test_utils.CARD_LIST)
        card_choice.append(temp_card)
        player.addcard(player_id=player_id, card={"card": temp_card})

    with app.test_client() as test_client:
        # Attempt to access the read player api
        response = test_client.get(f"/api/player/{player_id}/hand")
        assert response.status == "200 OK"
        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)
        assert len(response_data) == card_qty

        # Verify the database agrees.
        db_response = hand.read_one(player_data.hand_id)
        assert db_response is not None
        assert card_choice == db_response["cards"]
コード例 #4
0
def test_players_add_card(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/player/{player_id}' page is requested (GET)
    THEN check that the response is a UUID and contains the expected information
    """
    # Create a new player
    player_name = choice(test_utils.PLAYER_NAMES)
    player_id = test_utils.create_player(player_name)
    assert test_utils.UUID_REGEX.match(player_id)
    card_choice = choice(test_utils.CARD_LIST)

    with app.test_client() as test_client:
        # Attempt to access the read player api
        put_data = {"card": card_choice}
        response = test_client.put(
            f"/api/player/{player_id}/hand",
            data=json.dumps(put_data),
            content_type="application/json",
        )
        assert response.status == "201 CREATED"

        # Verify the database agrees.
        p_player = Player.query.filter(
            Player.player_id == player_id).one_or_none()
        # Did we find a player?
        assert p_player is not None
        hand_id = p_player.hand_id

        db_response = hand.read_one(hand_id)
        assert db_response is not None
        assert card_choice in db_response["cards"]
コード例 #5
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)
コード例 #6
0
ファイル: test_api_play.py プロジェクト: andy31lewis/pinochle
def test_set_trump_bad_suit(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)

    # Populate the bid
    trump = "Unics"
    player_id = choice(player_ids)
    round_.update(round_id, {"bid_winner": player_id})
    with app.test_client() as test_client:
        # Attempt to access the get round api
        response = test_client.put(
            f"/api/play/{round_id}/set_trump?player_id={player_id}&trump={trump}"
        )
        assert response.status == "409 CONFLICT"
        response_str = response.get_data(as_text=True)
        assert "Clubs" in response_str

        # Verify the database is unchanged
        temp_round = utils.query_round(round_id)
        assert temp_round.trump == "NONE"
        print(f"trump={temp_round.trump}")
コード例 #7
0
ファイル: test_api_play.py プロジェクト: andy31lewis/pinochle
def test_set_trump(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/play/{round_id}/set_trump' 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)

    # Populate the bid
    trump = choice(SUITS).capitalize().rstrip("s")
    player_id = choice(player_ids)
    round_.update(round_id, {"bid_winner": player_id})
    with app.test_client() as test_client:
        # Attempt to access the get round api
        response = test_client.put(
            f"/api/play/{round_id}/set_trump?player_id={player_id}&trump={trump}"
        )
        assert response.status == "200 OK"
        response_str = response.get_data(as_text=True)
        assert "trump" in response_str
        response_data = json.loads(response_str)
        db_trump = response_data.get("trump")
        assert db_trump == trump
        print(f"trump={db_trump}")
コード例 #8
0
ファイル: test_api_play.py プロジェクト: andy31lewis/pinochle
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}")
コード例 #9
0
ファイル: test_api_play.py プロジェクト: andy31lewis/pinochle
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}")
コード例 #10
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")
コード例 #11
0
def test_create_player_id_cookie(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/setcookie/player_id/<ident>' page is requested (GET)
    THEN check that the response contains the expected information
    """
    # Create a new player
    player_id = test_utils.create_player("PlayerName")

    with app.test_client() as test_client:
        # Attempt to access the set cookie API
        response = test_client.get(f"/api/setcookie/player_id/{player_id}")
        assert response.status == "200 OK"
        print(f"response.headers={response.headers}")
        assert f"player_id={player_id}" in response.headers["Set-Cookie"]

        response = test_client.get("/api/getcookie/player_id")
        assert response.status == "200 OK"
        response_str = response.get_data(as_text=True)
        assert "player_id" in response_str
        assert player_id in response_str
        print(f"response.headers={response.headers}")
        assert f"player_id={player_id}" in response.headers["Set-Cookie"]