~javiljoen/shopping-list

8c594aaf3c7985651c7c75f14dcf03caf7ee1afe — JA Viljoen 4 years ago 894d062
Add DELETE /items/<item> handler
2 files changed, 26 insertions(+), 0 deletions(-)

M src/groceries/web/api.py
M tests/test_rest_server.py
M src/groceries/web/api.py => src/groceries/web/api.py +12 -0
@@ 41,3 41,15 @@ def get_item(name):
        abort(404, description=f"No resource with name {name!r}")

    return jsonify(dictify(items[0]))


@bp.route("/items/<name>", methods=["DELETE"])
def delete_item(name):
    grocery_list = GroceryList(current_app.config["BACKEND"])
    items = [i for i in grocery_list.list_items() if i.name == name]

    if not items:
        abort(404, description=f"No resource with name {name!r}")

    grocery_list.delete_item(name)
    return jsonify(result=True), 204

M tests/test_rest_server.py => tests/test_rest_server.py +14 -0
@@ 66,3 66,17 @@ def test_get_an_unknown_item(client):
    assert response.get_json() == {
        "error": "404 Not Found: No resource with name 'jabberwocky'"
    }


def test_delete_item(client):
    response = client.delete("/api/items/apples")
    assert response.status_code == 204
    assert client.get("/api/items/apples").status_code == 404


def test_delete_unknown_item(client):
    response = client.delete("/api/items/jabberwocky")
    assert response.status_code == 404
    assert response.get_json() == {
        "error": "404 Not Found: No resource with name 'jabberwocky'"
    }