forked from hack4impact-uiuc/flask-exercise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
103 lines (76 loc) · 2.74 KB
/
test_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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
res_users = res.json["result"]["users"]
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
res_users = res.json["result"]["users"]
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
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"