Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
after run black
  • Loading branch information
malisharfer committed May 24, 2023
commit 761c0b9929e024c3c382d89757aef4b2c7ba485f
73 changes: 44 additions & 29 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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
Expand Down Expand Up @@ -47,49 +48,63 @@ def create_response(
def hello_world():
return create_response({"content": "hello world!"})


@app.route("/mirror/<name>")
def mirror(name):
return create_response({"name": name})


@app.route("/users/<id>")
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/<id>",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/<id>", 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/<id>",methods=["DELETE"])
@app.route("/users/<id>", 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!
Expand Down
62 changes: 49 additions & 13 deletions test_app.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"
assert res.json["message"] == "the user id is not found"