def test_set_trump_invalid_player(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
    trump = choice(SUITS)
    player_id = str(uuid.uuid4())
    round_.update(round_id, {"bid_winner": uuid.uuid4()})
    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 == "404 NOT FOUND"
        response_str = response.get_data(as_text=True)
        assert "not found" 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}")
def test_game_round_list(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/game/{game_id}/round' page is requested (GET)
    THEN check that the response is successful
    """
    # Create a new game
    game_id = str(test_utils.create_game(4))

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

    with app.test_client() as test_client:
        # Attempt to access the get round api
        response = test_client.get(f"/api/game/{game_id}/round")
        assert response.status == "200 OK"
        response_str = response.get_data(as_text=True)
        response_data = json.loads(response_str)
        # print(f"response_data={response_data}")

    # Verify the database agrees.
    db_response = round_.read_one(round_id)
    # print(f"db_response={db_response}")

    db_response = gameround.read_one(game_id, round_id)
    # print(f"db_response={db_response}")
    assert response_data == db_response
def test_game_update_state(app, patch_ws_messenger):  # pylint: disable=unused-argument
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/game/{game_id}?state' page is requested (PUT)
    THEN check that the response is valid
    """
    # Create a new game
    game_id = test_utils.create_game(0)

    # Verify the database agrees.
    db_response = game.read_one(game_id)
    initial_state = db_response.get("state")
    assert initial_state == 0
    state = initial_state + 1

    with app.test_client() as test_client:
        # Attempt to access the advance state game api
        response = test_client.put(f"/api/game/{game_id}?state=true")
        assert response.status == "200 OK"

    # Verify the database agrees.
    db_response = game.read_one(game_id)
    assert db_response is not None
    assert game_id == db_response.get("game_id")
    assert state == db_response.get("state")
def test_round_create(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round' page is requested (POST)
    THEN check that the response is a UUID and contains the expected information
    """
    # Create a new game
    game_id = test_utils.create_game(4)

    with app.test_client() as test_client:
        # Attempt to access the create round api
        response = test_client.post(f"/api/game/{game_id}/round")
        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)
        round_id = response_data.get("round_id")
        assert round_id != ""
        assert test_utils.UUID_REGEX.match(round_id)

    # Verify the database agrees.
    db_response = round_.read_one(round_id)
    assert db_response is not None
    assert round_id == db_response.get("round_id")
    assert db_response.get("trump") == "NONE"
    assert db_response.get("bid_winner") is None
    assert isinstance(db_response.get("round_seq"), int)
    assert db_response.get("bid") == 20
def test_game_round_delete(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/game/{game_id}/{round_id}' page is requested (DELETE)
    THEN check that the response is successful
    """
    # Create a new game
    game_id = test_utils.create_game(4)

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

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

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

    # Verify the database agrees.
    with pytest.raises(exceptions.NotFound):
        db_response = round_.read_one(round_id)
    with pytest.raises(exceptions.NotFound):
        db_response = gameround.read_one(game_id, round_id)
def test_update_bid_invalid_player(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
    bid = 21
    player_id = str(uuid.uuid4())
    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 == "404 NOT FOUND"
        response_str = response.get_data(as_text=True)
        assert "Player" in response_str and "not found" 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}")
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}")
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}")
def test_round_read_one(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round/{round_id}' page is requested (GET)
    THEN check that the response is a UUID and 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)

    with app.test_client() as test_client:
        # Attempt to access the create round api
        response = test_client.get(f"/api/round/{round_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)
        r_round_id = response_data.get("round_id")
        assert r_round_id != ""
        assert r_round_id == round_id
        assert test_utils.UUID_REGEX.match(r_round_id)

    # Verify the database agrees.
    db_response = round_.read_one(round_id)
    assert db_response is not None
    assert round_id == db_response.get("round_id")
Exemple #10
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}")
Exemple #11
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_game_round_start_no_teams(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round/{round_id}/start' page is requested (POST)
    THEN check that the response is successful
    """
    # Create a new game
    game_id = str(test_utils.create_game(4))

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

    with app.test_client() as test_client:
        # Attempt to access the get round api
        response = test_client.post(f"/api/round/{round_id}/start")
        assert response.status == "409 CONFLICT"
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_round_read_all(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round' page is requested (GET)
    THEN check that the response is a list of UUID and contains the expected information
    """
    create_games = 2

    # Clear out ALL previous test data.
    db.drop_all()
    db.create_all()

    game_ids = []
    round_ids = []
    for _ in range(create_games):
        # Create a new game
        game_id = test_utils.create_game(4)
        game_ids.append(game_id)

        for _ in range(create_games):
            # Create a new round
            round_id = test_utils.create_round(game_id)
            round_ids.append(round_id)
    assert len(game_ids) == create_games
    assert len(round_ids) == create_games * 2

    with app.test_client() as test_client:
        # Attempt to access the GET game api
        response = test_client.get("/api/round")
        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)
        r_round_id = response_data  # List of dicts
        assert len(r_round_id) >= create_games
        for item in r_round_id:
            assert item["round_id"] != ""
            assert item["round_id"] in round_ids
            assert test_utils.UUID_REGEX.match(item.get("round_id"))

    # Verify the database agrees.
    db_response = round_.read_all()  # List of dicts
    assert db_response is not None
    for item in db_response:
        assert item["round_id"] in round_ids
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"]
Exemple #16
0
def test_game_update_kitty_size(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/game/{game_id}' page is requested (PUT)
    THEN check that the response is a UUID
    """
    new_kitty = 10
    # Create a new game
    game_id = test_utils.create_game(0)

    with app.test_client() as test_client:
        # Attempt to access the delete game api
        response = test_client.put(f"/api/game/{game_id}?kitty_size={new_kitty}")
        assert response.status == "200 OK"

    # Verify the database agrees.
    db_response = game.read_one(game_id)
    assert db_response is not None
    assert game_id == db_response.get("game_id")
    assert new_kitty == db_response.get("kitty_size")
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
Exemple #18
0
def test_game_delete(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/game' page is requested (DELETE)
    THEN check that the response is a UUID
    """
    # Create a new game
    game_id = test_utils.create_game(0)

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

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

    # Verify the database agrees.
    with pytest.raises(exceptions.NotFound):
        db_response = game.read_one(game_id)
        assert db_response is not None
def test_create_game_id_cookie(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/setcookie/game_id/<ident>' page is requested (GET)
    THEN check that the response contains the expected information
    """
    # Create a new game
    game_id = test_utils.create_game(kitty_size=0)

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

        response = test_client.get("/api/getcookie/game_id")
        assert response.status == "200 OK"
        response_str = response.get_data(as_text=True)
        assert "game_id" in response_str
        assert game_id in response_str
        print(f"response.headers={response.headers}")
        assert f"game_id={game_id}" in response.headers["Set-Cookie"]
def test_game_round_create_invalid(app):  # pylint: disable=unused-argument
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/game/{game_id}/{round_id}' page is requested (POST)
    THEN check that the response is successful
    """
    # Create a new game
    game_id = str(uuid.uuid4())

    with pytest.raises(exceptions.Conflict):
        gameround.create(game_id, {"round_id": game_id})

    game_id = test_utils.create_game(4)

    # Create a new round
    round_id = str(uuid.uuid4())

    with pytest.raises(exceptions.Conflict):
        gameround.create(game_id, {"round_id": round_id})

    round_id = test_utils.create_round(game_id)

    with pytest.raises(exceptions.Conflict):
        gameround.create(game_id, {"round_id": round_id})
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