diff --git a/.env b/.env new file mode 100644 index 0000000..e69de29 diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 7ac0cc2..0000000 --- a/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -*.pyc -.vscode/ -venv/ -__pycache__ -.DS_Store/ -migrations/ -.pytest_cache \ No newline at end of file diff --git a/__pycache__/app.cpython-311.pyc b/__pycache__/app.cpython-311.pyc new file mode 100644 index 0000000..800fd18 Binary files /dev/null and b/__pycache__/app.cpython-311.pyc differ diff --git a/__pycache__/conftest.cpython-311-pytest-7.3.1.pyc b/__pycache__/conftest.cpython-311-pytest-7.3.1.pyc new file mode 100644 index 0000000..e9ba1ad Binary files /dev/null and b/__pycache__/conftest.cpython-311-pytest-7.3.1.pyc differ diff --git a/__pycache__/test_app.cpython-311-pytest-7.3.1.pyc b/__pycache__/test_app.cpython-311-pytest-7.3.1.pyc new file mode 100644 index 0000000..e264b1d Binary files /dev/null and b/__pycache__/test_app.cpython-311-pytest-7.3.1.pyc differ diff --git a/app.py b/app.py index 314e633..edba9eb 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,5 @@ from typing import Tuple - +import json from flask import Flask, jsonify, request, Response import mockdb.mockdb_interface as db @@ -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 optional data :param status optional status code, defaults to 200 :param message optional message @@ -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!"}) @@ -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/") +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/" , 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/" , 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 ~~~~~~~~~~~~ """ diff --git a/conftest.py b/conftest.py index e919768..0087b5d 100644 --- a/conftest.py +++ b/conftest.py @@ -1,7 +1,7 @@ import pytest -@pytest.fixture("session") +@pytest.fixture(scope = "session") def client(): from app import app diff --git a/mockdb/__pycache__/dummy_data.cpython-311.pyc b/mockdb/__pycache__/dummy_data.cpython-311.pyc new file mode 100644 index 0000000..eefa7b6 Binary files /dev/null and b/mockdb/__pycache__/dummy_data.cpython-311.pyc differ diff --git a/mockdb/__pycache__/mockdb_interface.cpython-311.pyc b/mockdb/__pycache__/mockdb_interface.cpython-311.pyc new file mode 100644 index 0000000..7845a5b Binary files /dev/null and b/mockdb/__pycache__/mockdb_interface.cpython-311.pyc differ diff --git a/mockdb/dummy_data.py b/mockdb/dummy_data.py index 642261c..51f80e7 100644 --- a/mockdb/dummy_data.py +++ b/mockdb/dummy_data.py @@ -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}]} \ No newline at end of file diff --git a/mockdb/mockdb_interface.py b/mockdb/mockdb_interface.py index e059be8..12bafe0 100644 --- a/mockdb/mockdb_interface.py +++ b/mockdb/mockdb_interface.py @@ -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] diff --git a/test_app.py b/test_app.py index 586aba5..e673cdf 100644 --- a/test_app.py +++ b/test_app.py @@ -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 + + +