~javiljoen/shopping-list

1814c15a3ee7089eb3551de6869bae1512370462 — JA Viljoen 4 years ago e1619f0
Use PATCH instead of PUT for updating items

The item doesn't get replaced wholesale; it is only certain fields that
get updated, so PATCH is more appropriate.
2 files changed, 4 insertions(+), 4 deletions(-)

M src/groceries/web/api.py
M tests/test_rest_server.py
M src/groceries/web/api.py => src/groceries/web/api.py +1 -1
@@ 66,7 66,7 @@ def get_item(name):
    return dictify(item)


@bp.route("/items/<name>", methods=["PUT"])
@bp.route("/items/<name>", methods=["PATCH"])
def update_item(name):
    grocery_list = GroceryList(current_app.config["BACKEND"])


M tests/test_rest_server.py => tests/test_rest_server.py +3 -3
@@ 124,7 124,7 @@ def test_create_existing_item(client):

def test_update_item(client):
    item = client.get("/api/items/apples").get_json()
    response = client.put("/api/items/apples", json={"section": "fruit"})
    response = client.patch("/api/items/apples", json={"section": "fruit"})
    assert response.status_code == 200
    expected = dict(item, section="fruit")
    assert response.get_json() == expected


@@ 132,7 132,7 @@ def test_update_item(client):


def test_update_unknown_item(client):
    response = client.put("/api/items/jabberwocky", json={"section": "pets"})
    response = client.patch("/api/items/jabberwocky", json={"section": "pets"})
    assert response.status_code == 404
    assert response.get_json() == {
        "error": "404 Not Found: 'jabberwocky' not found in grocery list."


@@ 140,7 140,7 @@ def test_update_unknown_item(client):


def test_update_item_with_invalid_fields(client):
    response = client.put("/api/items/apples", json={"variety": "granny smith"})
    response = client.patch("/api/items/apples", json={"variety": "granny smith"})
    assert response.status_code == 400
    assert response.get_json() == {
        "error": "400 Bad Request: Invalid fields in request"