diff --git a/app.py b/app.py index 314e633..16f78c2 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,9 @@ from flask import Flask, jsonify, request, Response import mockdb.mockdb_interface as db +from mockdb.dummy_data import initial_db_state + +db_state = initial_db_state app = Flask(__name__) @@ -10,7 +13,7 @@ def create_response( data: dict = None, status: int = 200, message: str = "" ) -> Tuple[Response, int]: """Wraps response in a consistent format throughout the API. - + Format inspired by https://medium.com/@shazow/how-i-design-json-api-responses-71900f00f2db Modifications included: - make success a boolean since there's only 2 values @@ -48,8 +51,60 @@ def hello_world(): @app.route("/mirror/") def mirror(name): - data = {"name": name} - return create_response(data) + return create_response({"name": name}) + + +@app.route("/users/") +def get_users_by_id(id): + if db.getById("users", int(id)): + return create_response({"user": db.getById("users", int(id))}) + return create_response(None, 404, "user not found") + + +@app.route("/users") +def get_users_in_team(): + if request.args.get("team"): + return create_response( + { + "users": list( + filter( + lambda x: x["team"] == request.args.get("team"), db.get("users") + ) + ) + } + ) + return create_response({"users": db.get("users")}) + + +@app.route("/users", methods=["POST"]) +def add_new_user(): + user = request.get_json() + message = "you have to send correct the:" + if "name" in user and "age" in user and "team" in user: + return create_response({"newUser": [db.create("users", user)]}, 201) + if "name" not in user: + message += "name" + if "age" not in user: + message += "age" + if "team" not in user: + message += "team" + return create_response(None, 401, message) + + +@app.route("/users/", methods=["PUT"]) +def update_user(id): + details = request.get_json() + if db.updateById("users", int(id), details) != None: + return create_response(None, 201, "successfully updated") + return create_response(None, 404, "the user id is not found") + + +@app.route("/users/", methods=["DELETE"]) +def delete_user(id): + if db.getById("users", int(id)) == None: + return create_response(None, 404, "the user id is not found") + db.deleteById("users", int(id)) + return create_response(None, 201, "successfully deleted") # TODO: Implement the rest of the API here! @@ -57,5 +112,6 @@ def mirror(name): """ ~~~~~~~~~~~~ END API ~~~~~~~~~~~~ """ + if __name__ == "__main__": app.run(debug=True) diff --git a/conftest.py b/conftest.py index e919768..a38efe0 100644 --- a/conftest.py +++ b/conftest.py @@ -1,7 +1,7 @@ import pytest -@pytest.fixture("session") +@pytest.fixture def client(): from app import app diff --git a/test_app.py b/test_app.py index 586aba5..67abf02 100644 --- a/test_app.py +++ b/test_app.py @@ -1,7 +1,9 @@ - # pytest automatically injects fixtures # that are defined in conftest.py # in this case, client is injected +import json + + def test_index(client): res = client.get("/") assert res.status_code == 200 @@ -39,3 +41,63 @@ def test_get_user_id(client): res_user = res.json["result"]["user"] assert res_user["name"] == "Aria" assert res_user["age"] == 19 + + +def test_get_user_id_with_not_exist_id(client): + res = client.get("/users/78") + assert res.status_code == 404 + assert res.json["message"] == "user not found" + + +def test_add_new_user(client): + res = client.post( + "/users", + data=json.dumps({"name": "mali", "age": 8, "team": "LWB"}), + headers={"Content-Type": "application/json"}, + ) + assert res.status_code == 201 + res_user = res.json["result"]["newUser"][0] + assert res_user["name"] == "mali" + assert res_user["age"] == 8 + + +def test_add_new_user_with_uncorrect_detailes(client): + res = client.post( + "/users", + data=json.dumps({"age": 8, "team": "LWB"}), + headers={"Content-Type": "application/json"}, + ) + assert res.status_code == 401 + assert res.json["message"] == "you have to send correct the:name" + + +def test_update_user(client): + res = client.put( + "/users/1", + data=json.dumps({"name": "gili"}), + headers={"Content-Type": "application/json"}, + ) + assert res.status_code == 201 + assert res.json["message"] == "successfully updated" + + +def test_update_user_with_not_exist_id(client): + res = client.put( + "/users/15", + data=json.dumps({"name": "gili"}), + headers={"Content-Type": "application/json"}, + ) + assert res.status_code == 404 + assert res.json["message"] == "the user id is not found" + + +def test_delete_user(client): + res = client.delete("/users/1") + assert res.status_code == 201 + assert res.json["message"] == "successfully deleted" + + +def test_delete_user_with_not_exist_id(client): + res = client.delete("/users/89") + assert res.status_code == 404 + assert res.json["message"] == "the user id is not found"