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

upload code #7

Open
wants to merge 1 commit 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
57 changes: 55 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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 @@ -41,6 +41,11 @@ def create_response(
"""


def update_db():
with open('mockdb/dummy_data.py', 'w')as f:
f.writelines("initial_db_state = "+str(db.db_state))


@app.route("/")
def hello_world():
return create_response({"content": "hello world!"})
Expand All @@ -52,7 +57,55 @@ def mirror(name):
return create_response(data)


# TODO: Implement the rest of the API here!
@app.route("/users", methods=['GET'])
def all_users():
data = db.get("users")
if (request.args.get('team')):
team = request.args.get('team')
data = (list(filter(lambda x: x["team"] == team, data)))
return create_response({"users": data})


@app.route("/users/<id>")
def user_by_id(id):
data = db.getById("users", int(id))
if (data == None):
return create_response(status=404, message="The user does not exist yet")
return create_response({"user": data})


@app.route("/users", methods=['POST'])
def create_new_user():
user_data = request.get_json()
required_fields = ["name", "age", "team"]
if not all(field in user_data for field in required_fields):
return create_response({"error": "Missing required fields"}, 400)
data = db.create("users", user_data)
update_db()
return create_response(data)


@app.route("/users/<id>/", methods=['PUT'])
def update_user(id):
user_data = request.get_json()
required_fields = ["name", "age", "team"]
if not all(field in user_data for field in required_fields):
return create_response({"error": "Missing required fields"}, 400)
data = db.updateById("users", int(id), user_data)
if (data == None):
return create_response(status=404, message="The user does not exist yet")
update_db()
return create_response(data)


@app.route("/users/<id>", methods=['DELETE'])
def delete_user(id):
if (db.getById("users", int(id)) == None):
return create_response(status=404, message="The user does not exist yet")
db.deleteById("users", int(id))
update_db()
return create_response(message="User deleted successfully!")


"""
~~~~~~~~~~~~ END API ~~~~~~~~~~~~
Expand Down
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(scope="session")
def client():
from app import app

Expand Down
38 changes: 38 additions & 0 deletions test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,41 @@ def test_get_user_id(client):
res_user = res.json["result"]["user"]
assert res_user["name"] == "Aria"
assert res_user["age"] == 19


def test_create_new_user(client):
user_data = {
"name": "John",
"age": 30,
"team": "LWB"
}
res = client.post("/users", json=user_data)
assert res.status_code == 200

res_data = res.json["result"]
assert res_data["name"] == "John"
assert res_data["age"] == 30
assert res_data["team"] == "LWB"


def test_create_new_user(client):
user_data = {
"name": "John",
"age": 30,
"team": "LWB"
}
res = client.post("/users", json=user_data)
assert res.status_code == 200

res_data = res.json["result"]
assert res_data["name"] == "John"
assert res_data["age"] == 30
assert res_data["team"] == "LWB"


def test_delete_user(client):
res = client.delete("/users/1")
assert res.status_code == 200

res_data = res.json
assert res_data["message"] == "User deleted successfully!"