From 7c1af36d3234ee492b216daf001945b650ed4976 Mon Sep 17 00:00:00 2001 From: "M. Sharpher" Date: Wed, 24 May 2023 12:12:07 +0300 Subject: [PATCH 1/4] finish the task --- app.py | 47 ++++++++++++++++++++++++++++++++++++++++++++--- conftest.py | 2 +- test_app.py | 42 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 83 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index 314e633..0d6b24c 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,8 @@ 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__) @@ -45,11 +47,49 @@ def create_response( def hello_world(): return create_response({"content": "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({},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({},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({},201,"successfully updated") + return create_response({},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({},404,"the user id is not found") + db.deleteById("users",int(id)) + return create_response({},201,"successfully deleted") # TODO: Implement the rest of the API here! @@ -57,5 +97,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..9adce23 100644 --- a/test_app.py +++ b/test_app.py @@ -2,18 +2,17 @@ # 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 assert res.json["result"]["content"] == "hello world!" - def test_mirror(client): res = client.get("/mirror/Tim") assert res.status_code == 200 assert res.json["result"]["name"] == "Tim" - def test_get_users(client): res = client.get("/users") assert res.status_code == 200 @@ -22,7 +21,6 @@ def test_get_users(client): assert len(res_users) == 4 assert res_users[0]["name"] == "Aria" - def tests_get_users_with_team(client): res = client.get("/users?team=LWB") assert res.status_code == 200 @@ -31,7 +29,6 @@ def tests_get_users_with_team(client): assert len(res_users) == 2 assert res_users[1]["name"] == "Tim" - def test_get_user_id(client): res = client.get("/users/1") assert res.status_code == 200 @@ -39,3 +36,40 @@ 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",data=json.dumps({"name": "gili"}), headers={"Content-Type": "application/json"}) + 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",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" \ No newline at end of file From 761c0b9929e024c3c382d89757aef4b2c7ba485f Mon Sep 17 00:00:00 2001 From: "M. Sharpher" Date: Wed, 24 May 2023 12:28:22 +0300 Subject: [PATCH 2/4] after run black --- app.py | 73 ++++++++++++++++++++++++++++++++--------------------- test_app.py | 62 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 93 insertions(+), 42 deletions(-) diff --git a/app.py b/app.py index 0d6b24c..6c6ad05 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,7 @@ 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__) @@ -12,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 @@ -47,49 +48,63 @@ def create_response( def hello_world(): return create_response({"content": "hello world!"}) + @app.route("/mirror/") def mirror(name): 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({},404,"user not found") + if db.getById("users", int(id)): + return create_response({"user": db.getById("users", int(id))}) + return create_response({}, 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"]) + 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({},401,message) - -@app.route("/users/",methods=["PUT"]) + 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({}, 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({},201,"successfully updated") - return create_response({},404,"the user id is not found") + if db.updateById("users", int(id), details) != None: + return create_response({}, 201, "successfully updated") + return create_response({}, 404, "the user id is not found") + -@app.route("/users/",methods=["DELETE"]) +@app.route("/users/", methods=["DELETE"]) def delete_user(id): - if(db.getById("users",int(id))== None): - return create_response({},404,"the user id is not found") - db.deleteById("users",int(id)) - return create_response({},201,"successfully deleted") + if db.getById("users", int(id)) == None: + return create_response({}, 404, "the user id is not found") + db.deleteById("users", int(id)) + return create_response({}, 201, "successfully deleted") # TODO: Implement the rest of the API here! diff --git a/test_app.py b/test_app.py index 9adce23..d93f05d 100644 --- a/test_app.py +++ b/test_app.py @@ -1,18 +1,21 @@ - # 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 assert res.json["result"]["content"] == "hello world!" + def test_mirror(client): res = client.get("/mirror/Tim") assert res.status_code == 200 assert res.json["result"]["name"] == "Tim" + def test_get_users(client): res = client.get("/users") assert res.status_code == 200 @@ -21,6 +24,7 @@ def test_get_users(client): assert len(res_users) == 4 assert res_users[0]["name"] == "Aria" + def tests_get_users_with_team(client): res = client.get("/users?team=LWB") assert res.status_code == 200 @@ -29,6 +33,7 @@ def tests_get_users_with_team(client): assert len(res_users) == 2 assert res_users[1]["name"] == "Tim" + def test_get_user_id(client): res = client.get("/users/1") assert res.status_code == 200 @@ -37,39 +42,70 @@ def test_get_user_id(client): 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" + 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"}) + 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"}) + 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" + 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"}) + 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" + 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"}) + 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" + assert res.json["message"] == "the user id is not found" + def test_delete_user(client): - res = client.delete("/users/1",data=json.dumps({"name": "gili"}), headers={"Content-Type": "application/json"}) + res = client.delete( + "/users/1", + data=json.dumps({"name": "gili"}), + headers={"Content-Type": "application/json"}, + ) assert res.status_code == 201 - assert res.json["message"]== "successfully deleted" + assert res.json["message"] == "successfully deleted" + def test_delete_user_with_not_exist_id(client): - res = client.delete("/users/89",data=json.dumps({"name": "gili"}), headers={"Content-Type": "application/json"}) + res = client.delete( + "/users/89", + 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" \ No newline at end of file + assert res.json["message"] == "the user id is not found" From f83cade955e6ef656cfbea5fce645f1fae0fac20 Mon Sep 17 00:00:00 2001 From: "M. Sharpher" Date: Wed, 24 May 2023 13:20:44 +0300 Subject: [PATCH 3/4] code review --- app.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index 6c6ad05..16f78c2 100644 --- a/app.py +++ b/app.py @@ -58,7 +58,7 @@ def mirror(name): def get_users_by_id(id): if db.getById("users", int(id)): return create_response({"user": db.getById("users", int(id))}) - return create_response({}, 404, "user not found") + return create_response(None, 404, "user not found") @app.route("/users") @@ -88,23 +88,23 @@ def add_new_user(): message += "age" if "team" not in user: message += "team" - return create_response({}, 401, message) + 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({}, 201, "successfully updated") - return create_response({}, 404, "the user id is not found") + 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({}, 404, "the user id is not found") + return create_response(None, 404, "the user id is not found") db.deleteById("users", int(id)) - return create_response({}, 201, "successfully deleted") + return create_response(None, 201, "successfully deleted") # TODO: Implement the rest of the API here! From 08e16ef8b7be75cb20de08ae7dd9f852335122d3 Mon Sep 17 00:00:00 2001 From: "M. Sharpher" Date: Wed, 24 May 2023 17:23:24 +0300 Subject: [PATCH 4/4] clean code --- test_app.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/test_app.py b/test_app.py index d93f05d..67abf02 100644 --- a/test_app.py +++ b/test_app.py @@ -92,20 +92,12 @@ def test_update_user_with_not_exist_id(client): def test_delete_user(client): - res = client.delete( - "/users/1", - data=json.dumps({"name": "gili"}), - headers={"Content-Type": "application/json"}, - ) + 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", - data=json.dumps({"name": "gili"}), - headers={"Content-Type": "application/json"}, - ) + res = client.delete("/users/89") assert res.status_code == 404 assert res.json["message"] == "the user id is not found"