diff --git a/app.py b/app.py index a2d2561..0c4cac1 100644 --- a/app.py +++ b/app.py @@ -62,6 +62,20 @@ def delete_item(item_id): return "", 204 +@app.route("/add", methods=["POST"]) +def add(): + data = request.get_json() + if not data or "a" not in data or "b" not in data: + return jsonify(error="both 'a' and 'b' are required"), 400 + + try: + result = data["a"] + data["b"] + except TypeError: + return jsonify(error="'a' and 'b' must be numbers"), 400 + + return jsonify(result=result), 201 + + _items_store = [] diff --git a/test_app.py b/test_app.py index d5ae8f6..3044652 100644 --- a/test_app.py +++ b/test_app.py @@ -63,3 +63,23 @@ def test_delete_item(client): def test_delete_item_not_found(client): resp = client.delete("/items/999") assert resp.status_code == 404 + + +def test_add(client): + resp = client.post("/add", json={"a": 2, "b": 3}) + assert resp.status_code == 201 + data = resp.get_json() + assert data["result"] == 5 + + +def test_add_missing_fields(client): + resp = client.post("/add", json={"a": 2}) + assert resp.status_code == 400 + + resp = client.post("/add", json={}) + assert resp.status_code == 400 + + +def test_add_invalid_types(client): + resp = client.post("/add", json={"a": "x", "b": 3}) + assert resp.status_code == 400