-
Notifications
You must be signed in to change notification settings - Fork 0
/
Z_test_main.py
110 lines (92 loc) · 2.75 KB
/
Z_test_main.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
104
105
106
107
108
109
110
# My Unit Test
from fastapi.testclient import TestClient
from main import app
# look for files with test_*.py
# $ pytest
# command: pytest -vvl
# Unit Tests
def test_basic_example():
# pass
assert True
client = TestClient(app)
def test_put_api():
response = client.put(
"/items/420",
json={
"name": "first item",
"quantity": 5,
"serial_num": "324",
"origin": {"country": "Ethiopia", "production_date": "2023"},
},
)
assert response.status_code == 200
# Tests API when we put in incorrect input; The API will return the message that the input is incorrect, and which field is incrorrect.
def test_put_incorrect_imput_api():
response = client.put(
"/items/422",
json={
"name": "first item",
"quantity": "incorrect input", # quantity should be an integer i.e 5
"serial_num": "324",
"origin": {"country": "Ethiopia", "production_date": "2023"},
},
)
assert response.status_code == 422
# then run "pytest -vvl" again to test GET API
def test_get_api():
response = client.put(
"/items/420",
json={
"name": "first item",
"quantity": 501,
"serial_num": "324",
"origin": {"country": "Ethiopia", "production_date": "2023"},
},
)
assert response.status_code == 200
response = client.get(
"/items/420",
)
assert response.status_code == 200
assert response.json() == {
"name": "first item",
"quantity": 501,
"serial_num": "324",
"origin": {"country": "Ethiopia", "production_date": "2023"},
}
# Test Delete Function
def test_delete_api():
response = client.put(
"/items/420",
json={
"name": "Test Deleted Item",
"quantity": 501,
"serial_num": "324",
"origin": {"country": "Ethiopia", "production_date": "2023"},
},
)
assert response.status_code == 200
response = client.delete("/items/420")
assert response.status_code == 200
response = client.delete("/items/420")
assert response.status_code == 404
# Test GET ALL API
def test_get_all_api():
response = client.put(
"/items/420",
json={
"name": "first item",
"quantity": 5,
"serial_num": "test",
"origin": {"country": "Ethiopia", "production_date": "2023"},
},
)
response = client.get("/items/")
assert response.status_code == 200
assert response.json() == [{
"name": "first item",
"quantity": 5,
"serial_num": "test",
"origin": {"country": "Ethiopia", "production_date": "2023"},
}
]