Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []


Expand Down
20 changes: 20 additions & 0 deletions test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading