-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
127 lines (93 loc) · 4.57 KB
/
Copy pathtest.py
File metadata and controls
127 lines (93 loc) · 4.57 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import unittest
import requests
BASE_HTTP = "http://127.0.0.1:5000"
CAT = {
"weight": 4.5,
"tail_length": 34.9,
"breed": 9
}
class MyTestCase(unittest.TestCase):
def test_api_get_data(self):
data = requests.get(BASE_HTTP + "/api/data")
print(data.json())
self.assertEqual(200, data.status_code)
def test_api_post(self):
response = requests.post(BASE_HTTP + "/api/data", json=CAT)
data = requests.get(BASE_HTTP + "/api/data")
data = data.json()
last_id = data[-1]["id"]
self.assertEqual(200, response.status_code)
self.assertEqual(last_id, int(response.json().get("id")))
def test_api_delete(self):
data = requests.get(BASE_HTTP + "/api/data")
data = data.json()
last_id = data[-1]["id"]
response = requests.delete(BASE_HTTP + f"/api/data/{last_id}")
self.assertEqual(200, response.status_code)
self.assertEqual(last_id, int(response.json().get("id")))
def test_wrong_add_weight(self):
wrong_cat = {"weight": -5, "tail_length": 34.9, "breed": 9}
response = requests.post(BASE_HTTP + "/api/data", json=wrong_cat)
self.assertEqual(400, response.status_code)
def test_wrong_add_tail_length(self):
wrong_tail_length = {"weight": 4.5, "tail_length": -5, "breed": 9}
response = requests.post(BASE_HTTP + "/api/data", json=wrong_tail_length)
self.assertEqual(400, response.status_code)
def test_wrong_add_None1(self):
wrong_cat_none = {"tail_length": 5, "breed": 9}
response = requests.post(BASE_HTTP + "/api/data", json=wrong_cat_none)
self.assertEqual(400, response.status_code)
def test_wrong_add_None2(self):
wrong_cat_none = {"weight": 5, "breed": 9}
response = requests.post(BASE_HTTP + "/api/data", json=wrong_cat_none)
self.assertEqual(400, response.status_code)
def test_wrong_add_None3(self):
wrong_cat_none = {"tail_length": 5, "weight": 9}
response = requests.post(BASE_HTTP + "/api/data", json=wrong_cat_none)
self.assertEqual(400, response.status_code)
def test_wrong_add_None4(self):
wrong_cat_none = {}
response = requests.post(BASE_HTTP + "/api/data", json=wrong_cat_none)
print(response.json().get("error"))
self.assertEqual(400, response.status_code)
def test_wrong_add_None5(self):
wrong_tail_length = {"weight": 4.5, "tail_length": None, "breed": 9}
response = requests.post(BASE_HTTP + "/api/data", json=wrong_tail_length)
self.assertEqual(400, response.status_code)
# def test_wrong_add_more(self):
# wrong_tail_length = [{"weight": 4.5, "tail_length": 34.6, "breed": 9}, {"weight": 6.7, "tail_length": 12.6, "breed": 9}]
# response = requests.post(BASE_HTTP + "/api/data", json=wrong_tail_length)
# self.assertEqual(400, response.status_code)
#
# def test_wrong_add_more2(self):
# wrong_tail_length = [{"weight": 4.5, "tail_length": 34.6, "breed": 9}]
# response = requests.post(BASE_HTTP + "/api/data", json=wrong_tail_length)
# self.assertEqual(400, response.status_code)
def test_api_wrong_delete(self):
# data = requests.get(BASE_HTTP + "/api/data")
# data = data.json()
# last_id = data[-1]['id']
# last_id += 10
response = requests.delete(BASE_HTTP + f"/api/data/999")
self.assertEqual(404, response.status_code)
def test_predict_api(self):
response = requests.get(BASE_HTTP + "/api/predictions?weight=4.2&tail_length=34.9")
self.assertEqual(200, response.status_code)
self.assertNotEqual(None, response.json().get("prediction"))
def test_predict_wrong_api1(self):
response = requests.get(BASE_HTTP + "/api/predictions?weight=-4.2&tail_length=34.9")
self.assertEqual(400, response.status_code)
def test_predict_wrong_api2(self):
response = requests.get(BASE_HTTP + "/api/predictions?weight=4.2&tail_length=-34.9")
self.assertEqual(400, response.status_code)
def test_predict_wrong_api3(self):
response = requests.get(BASE_HTTP + "/api/predictions?weight=4.2")
self.assertEqual(400, response.status_code)
def test_predict_wrong_api4(self):
response = requests.get(BASE_HTTP + "/api/predictions?tail_length=4.2")
self.assertEqual(400, response.status_code)
def test_predict_wrong_api5(self):
response = requests.get(BASE_HTTP + "/api/predictions?")
self.assertEqual(400, response.status_code)
if __name__ == "__main__":
unittest.main()