Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flask-exerscise #10

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
62 changes: 59 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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
Expand Down Expand Up @@ -48,14 +51,67 @@ def hello_world():

@app.route("/mirror/<name>")
def mirror(name):
data = {"name": name}
return create_response(data)
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(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/<id>", 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/<id>", 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!

"""
~~~~~~~~~~~~ END API ~~~~~~~~~~~~
"""

if __name__ == "__main__":
app.run(debug=True)
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest


@pytest.fixture("session")
@pytest.fixture
def client():
from app import app

Expand Down
64 changes: 63 additions & 1 deletion test_app.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"