Ejemplo n.º 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
Ejemplo n.º 2
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"]
Ejemplo n.º 3
0
def test_roundkitty_delete_missing(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round/{round_id}/kitty' page is requested (DELETE)
    THEN check that the response is successful
    """
    # Create a new round
    round_id = str(uuid.uuid4())

    # Retrieve the generated hand_id (as a UUID object).
    hand_id = str(uuid.uuid4())

    # Verify the database agrees.
    db_response = hand.read_one(hand_id)
    assert db_response is None

    with app.test_client() as test_client:
        # Attempt to access the delete round api
        response = test_client.delete(f"/api/round/{round_id}/kitty")
        assert response.status == "204 NO CONTENT"

        # Attempt to retrieve the now-deleted round id
        response = test_client.get(f"/api/round/{round_id}/kitty")
        assert response.status == "204 NO CONTENT"

        assert response.data is not None
        response_str = response.get_data(as_text=True)
        assert response_str == ""

    # Verify the database agrees.
    db_response = roundkitty.read(round_id)
    assert db_response is None
    with pytest.raises(exceptions.NotFound):
        round_.read_one(round_id)
Ejemplo n.º 4
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"]
Ejemplo n.º 5
0
def test_roundteam_delcard_missing(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 round
    round_id = str(uuid.uuid4())

    # Create a new teams
    team_id = str(uuid.uuid4())

    # Create the roundteam association for the teams.
    with pytest.raises(exceptions.Conflict):
        roundteams.create(round_id=round_id, teams=[team_id])

    # Add a card to the team's collection.
    chosen_card = choice(test_utils.CARD_LIST)
    with pytest.raises(exceptions.NotFound):
        roundteams.addcard(round_id, team_id, {"card": 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 == "404 NOT FOUND"

    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
Ejemplo n.º 6
0
def test_roundkitty_delete(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round/{round_id}/kitty' page is requested (DELETE)
    THEN check that the response is successful
    """
    # Create a new game
    game_id = test_utils.create_game(4)
    assert test_utils.UUID_REGEX.match(game_id)

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

    # Retrieve the generated hand_id (as a UUID object).
    hand_uuid = Round.query.filter(Round.round_id == round_id).all()

    assert hand_uuid is not None
    assert hand_uuid != []
    hand_id = str(hand_uuid[0].hand_id)
    print(f"round_id={round_id} hand_id={hand_id}")

    # Populate Hand with cards.
    for card in test_utils.CARD_LIST:
        hand.addcard(hand_id, card)

    # Verify the database agrees.
    db_response = hand.read_one(hand_id)
    assert db_response is not None
    assert hand_id == db_response.get("hand_id")
    assert db_response.get("cards") == test_utils.CARD_LIST

    with app.test_client() as test_client:
        # Attempt to access the delete round api
        response = test_client.delete(f"/api/round/{round_id}/kitty")
        assert response.status == "204 NO CONTENT"

        # Attempt to retrieve the now-deleted round id
        response = test_client.get(f"/api/round/{round_id}/kitty")
        assert response.status == "200 OK"

        assert response.data is not None
        response_str = response.get_data(as_text=True)
        response_data = json.loads(response_str)
        cards = response_data.get("cards")
        assert cards is not None
        assert cards == []

    # Verify the database agrees.
    db_response = roundkitty.read(round_id)
    assert db_response == {"cards": []}
    db_response = round_.read_one(round_id)
    assert db_response.get("hand_id") is None
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
def test_roundkitty_read(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round/{round_id}/kitty' page is requested (GET)
    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)
    assert test_utils.UUID_REGEX.match(round_id)

    # Retrieve the generated hand_id (as a UUID object).
    hand_id = Round.query.filter(Round.round_id == round_id).all()[0].hand_id

    # Populate Hand with cards.
    for card in test_utils.CARD_LIST:
        hand.addcard(str(hand_id), card)

    with app.test_client() as test_client:
        # Attempt to access the create round api
        response = test_client.get(f"/api/round/{round_id}/kitty")
        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)
        cards = response_data.get("cards")
        assert cards is not None
        assert cards != ""
        assert cards == test_utils.CARD_LIST

    # Verify the database agrees.
    db_response = hand.read_one(hand_id)
    assert db_response is not None
    assert hand_id == db_response.get("hand_id")
    assert db_response.get("cards") == test_utils.CARD_LIST
Ejemplo n.º 9
0
def test_roundkitty_read_missing(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/round/{round_id}/kitty' page is requested (GET)
    THEN check that the response contains the expected information
    """
    # Create a new round
    round_id = str(uuid.uuid4())

    # Retrieve the generated hand_id (as a UUID object).
    hand_id = str(uuid.uuid4())

    with app.test_client() as test_client:
        # Attempt to access the get round api
        response = test_client.get(f"/api/round/{round_id}/kitty")
        assert response.status == "204 NO CONTENT"
        assert response.get_data(as_text=True) is not None
        # This is a JSON formatted STRING
        assert response.get_data(as_text=True) == ""

    # Verify the database agrees.
    db_response = hand.read_one(hand_id)
    assert db_response is None