Пример #1
0
def test_players_delete_one(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/player/{player_id}' page is requested (DELETE)
    THEN check that the response contains the expected information
    """
    player_ids = {}
    # Create a new player
    for player_name in test_utils.PLAYER_NAMES:
        # Create a new game
        db_response, status = player.create({"name": player_name})
        assert status == 201
        assert db_response is not None
        player_id = db_response.get("player_id")
        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 delete player api
        for player_id in player_ids:
            response = test_client.delete(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)
            assert response_str is not None
            assert response_str != ""
            assert player_id in response_str

            # Verify the database agrees.
            with pytest.raises(exceptions.NotFound):
                db_response = player.read_one(player_id)
Пример #2
0
def test_players_read_all(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/player' 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()

    player_ids = {}
    # Create a new player
    for player_name in test_utils.PLAYER_NAMES:
        # Create a new game
        db_response, status = player.create({"name": player_name})
        assert status == 201
        assert db_response is not None
        player_id = db_response.get("player_id")
        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
        response = test_client.get("/api/player")
        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(test_utils.PLAYER_NAMES) == len(response_data)

        for response_item in response_data:
            player_uuid = response_item.get("player_id")
            assert player_uuid is not None
            assert player_uuid != ""
            assert player_uuid in list(player_ids.keys())
            player_name = response_item.get("name")
            assert player_name is not None
            assert player_name != ""
            assert player_name in list(player_ids.values())

            # Verify the database agrees.
            db_response = player.read_one(player_uuid)
            assert db_response is not None
            assert player_uuid == db_response.get("player_id")
            assert db_response.get("meld_score") == 0
            assert player_name == db_response.get("name")
Пример #3
0
def create_player(player_name: str) -> str:
    """
    Create a player in the database.

    :param player_name: Name of the player to be created
    :type player_name: str
    :return: UUID of the created player (player_id)
    :rtype: str
    """
    # Create a new player
    db_response, status = player.create({"name": player_name})
    assert status == 201
    assert db_response is not None
    player_id = db_response.get("player_id")
    assert UUID_REGEX.match(player_id)

    return player_id