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-exercise #11

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
Empty file added .env
Empty file.
7 changes: 0 additions & 7 deletions .gitignore

This file was deleted.

Binary file added __pycache__/app.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/conftest.cpython-311-pytest-7.3.1.pyc
Binary file not shown.
Binary file added __pycache__/test_app.cpython-311-pytest-7.3.1.pyc
Binary file not shown.
59 changes: 54 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Tuple

import json
from flask import Flask, jsonify, request, Response
import mockdb.mockdb_interface as db

Expand All @@ -18,7 +18,6 @@ def create_response(
IMPORTANT: data must be a dictionary where:
- the key is the name of the type of data
- the value is the data itself

:param data <str> optional data
:param status <int> optional status code, defaults to 200
:param message <str> optional message
Expand All @@ -39,8 +38,11 @@ def create_response(
"""
~~~~~~~~~~~~ API ~~~~~~~~~~~~
"""


def write_data():
new_data = json.dumps({"users" :db.get("users") })
with open("mockdb/dummy_data.py" ,'w') as f:
f.write( "initial_db_state = " + new_data )

@app.route("/")
def hello_world():
return create_response({"content": "hello world!"})
Expand All @@ -51,9 +53,56 @@ def mirror(name):
data = {"name": name}
return create_response(data)


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

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

@app.route("/users", methods=['POST'])
def create_user():
data = request.get_json()
required_fildes = ["name","age","team"]
if not all(filed in data for filed in required_fildes):
return create_response({"None":None} , 422 ,"Missing data The object should contain: ID, name and team" )
new_user={
"name":data["name"], "age": data["age"], "team": data["team"]
}
res = db.create("users",new_user)
write_data()
return create_response({"users":res} , 201 )


@app.route("/users/<id>" , methods=['PUT'] )
def update_users(id):
data = request.get_json()
update_user = db.updateById("users", int(id) , data)
if update_user == None:
return create_response({"users":data} ,404 ,"" )
write_data()
return create_response({"users":update_user})


@app.route("/users/<id>" , methods=['DELETE'] )
def delete_user(id):
data = db.getById("users",int(id))
if data == None:
return create_response({} , 404 , "User does not exist" )
db.deleteById("users", int(id) )
write_data()
return create_response({} , 200 ,'')

"""
~~~~~~~~~~~~ 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
Binary file added mockdb/__pycache__/dummy_data.cpython-311.pyc
Binary file not shown.
Binary file not shown.
9 changes: 1 addition & 8 deletions mockdb/dummy_data.py
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
initial_db_state = {
"users": [
{"id": 1, "name": "Aria", "age": 19, "team": "LWB"},
{"id": 2, "name": "Tim", "age": 20, "team": "LWB"},
{"id": 3, "name": "Varun", "age": 23, "team": "NNB"},
{"id": 4, "name": "Alex", "age": 24, "team": "C2TC"},
]
}
initial_db_state = {"users": [{"id": 2, "name": "Tim", "age": 20, "team": "LWB"}, {"id": 3, "name": "Varun", "age": 23, "team": "NNB"}, {"id": 4, "name": "Alex", "age": 24, "team": "C2TC"}, {"name": "Ben", "age": 25, "team": "MNB", "id": 5}]}
1 change: 0 additions & 1 deletion mockdb/mockdb_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@ def updateById(type, id, update_values):
item[k] = v
return item


def deleteById(type, id):
db_state[type] = [i for i in get(type) if i["id"] != id]
34 changes: 34 additions & 0 deletions test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,37 @@ 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_user(client):
new_user ={
"name":"Ben",
"age": 25,
"team":"MNB"
}
res = client.post("/users", json=new_user)
assert res.status_code == 201

res_user = res.json["result"]["users"]
assert res_user["name"] == "Ben"
assert res_user["age"] == 25

def test_update_users(client):
new_user ={
"name":"Beni",
"age": 2,
"team":"MNB"
}
res = client.put("/users/1", json=new_user)
assert res.status_code == 200

res_user = res.json["result"]["users"]
assert res_user["name"] == "Beni"
assert res_user["age"] == 2

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