From 9bd32b8e99b01bc4bbda9721f67fd8d28bc37d3b Mon Sep 17 00:00:00 2001 From: fullsend-code Date: Wed, 3 Jun 2026 13:16:01 +0000 Subject: [PATCH] fix(#2): add /add endpoint returning 201 Created The /add endpoint was missing from the application. Added a POST /add endpoint that accepts JSON with "a" and "b" numeric fields, returns their sum as {"result": } with HTTP 201 Created status. Includes input validation for missing fields and non-numeric types (returns 400). Added tests covering the happy path, missing fields, and invalid type inputs. Closes #2 --- app.py | 14 ++++++++++++++ test_app.py | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+) 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