-
Notifications
You must be signed in to change notification settings - Fork 0
/
flasktest.py
329 lines (291 loc) · 10.4 KB
/
flasktest.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import requests
import json
BASE_URL = 'http://127.0.0.1:5000' # Assuming Flask app is running locally on port 5000
# Function to pretty print JSON response
def print_json(response):
try:
print(json.dumps(response.json(), indent=4))
except ValueError:
print("Response is not in JSON format.")
print(response.text)
# Function to handle HTTP errors
def handle_http_error(response):
if response.status_code >= 400:
print(f"HTTP Error: {response.status_code}")
print_json(response)
return True
return False
# Test signup endpoint
def test_signup():
try:
url = f"{BASE_URL}/signup"
data = {
"name": "John Doe",
"phone": "1234567890",
"email": "[email protected]",
"password": "password123"
}
response = requests.post(url, json=data)
if not handle_http_error(response):
print_json(response)
except requests.RequestException as e:
print(f"Request Exception: {e}")
# Test login endpoint
def test_login():
try:
url = f"{BASE_URL}/login"
data = {
"email": "[email protected]",
"password": "password123"
}
response = requests.post(url, json=data)
if not handle_http_error(response):
print_json(response)
except requests.RequestException as e:
print(f"Request Exception: {e}")
# Test add_movie endpoint
def test_add_movie():
try:
url = f"{BASE_URL}/add_movie"
data = {
"name": "The Matrix",
"genre": "Science Fiction",
"rating": 9.0,
"release_date": "1999-03-31",
'key': '14587334ae0ff1fd9bd04a5a85110c2fXPZT0001'
}
response = requests.post(url, json=data)
if not handle_http_error(response):
print_json(response)
except requests.RequestException as e:
print(f"Request Exception: {e}")
# Test movies endpoint
def test_get_movies():
try:
url = f"{BASE_URL}/movies"
response = requests.get(url)
if not handle_http_error(response):
print_json(response)
except requests.RequestException as e:
print(f"Request Exception: {e}")
# Test rate_movie endpoint
def test_rate_movie():
try:
url = f"{BASE_URL}/rate_movie"
data = {
"user_id": 1,
"movie_id": 1,
"rating": 8.,
'key': '14587334ae0ff1fd9bd04a5a85110c2fXPZT0001'
}
response = requests.post(url, json=data)
if not handle_http_error(response):
print_json(response)
except requests.RequestException as e:
print(f"Request Exception: {e}")
# Test search_movie endpoint
def test_search_movie():
try:
url = f"{BASE_URL}/search_movie?name=Endgame"
response = requests.get(url)
if not handle_http_error(response):
print_json(response)
except requests.RequestException as e:
print(f"Request Exception: {e}")
# Test catch_all endpoint
def test_catch_all():
try:
url = f"{BASE_URL}/nonexistent_endpoint"
response = requests.get(url)
if not handle_http_error(response):
print_json(response)
except requests.RequestException as e:
print(f"Request Exception: {e}")
# Run tests
if __name__ == '__main__':
print("running code for ------------------------------------------ test_signup -------------------------------------------------------")
test_signup()
print("running code for ------------------------------------------ test_login -------------------------------------------------------")
test_login()
print("running code for ------------------------------------------ test_add_movie -------------------------------------------------------")
test_add_movie()
print("running code for ------------------------------------------ test_get_movies -------------------------------------------------------")
test_get_movies()
print("running code for ------------------------------------------ test_rate_movie -------------------------------------------------------")
test_rate_movie()
print("running code for ------------------------------------------ test_search_movie -------------------------------------------------------")
test_search_movie()
print("running code for ------------------------------------------ test_catch_all -------------------------------------------------------")
test_catch_all()
# for you this is result which you will get after running the code on flaskapp.py and then this one (1st time)
"""
running code for ------------------------------------------ test_signup -------------------------------------------------------
{
"key": "9ed3d4c60951fc9d37c3a3096db4791fXPZT0005",
"message": "User registered successfully."
}
running code for ------------------------------------------ test_login -------------------------------------------------------
{
"key": "9ed3d4c60951fc9d37c3a3096db4791fXPZT0005",
"message": "Login successful",
"user_data": {
"email": "[email protected]",
"id": 5,
"name": "John Doe",
"password": "password123",
"phone": "1234567890"
}
}
running code for ------------------------------------------ test_add_movie -------------------------------------------------------
HTTP Error: 401
{
"message": "Unauthorized access"
}
running code for ------------------------------------------ test_get_movies -------------------------------------------------------
[
{
"genre": "Comedy",
"id": 1,
"name": "Home Alone",
"rating": "PG",
"release_date": "01-04-1996"
},
{
"genre": "Crime",
"id": 2,
"name": "The Godfather",
"rating": "R",
"release_date": "01-04-1972"
},
{
"genre": "Action",
"id": 3,
"name": "Avengers: Endgame",
"rating": "PG",
"release_date": "01-04-2019"
},
{
"genre": "Science Fiction",
"id": 4,
"name": "The Matrix",
"rating": 9.0,
"release_date": "1999-03-31"
}
]
running code for ------------------------------------------ test_rate_movie -------------------------------------------------------
HTTP Error: 401
{
"message": "key was not ok, Unauthorized, False None {}"
}
running code for ------------------------------------------ test_search_movie -------------------------------------------------------
{
"matched_movies": [
{
"average_rating": 3.733333333333333,
"genre": "Action",
"id": 3,
"name": "Avengers: Endgame",
"rating": "PG",
"release_date": "01-04-2019"
}
]
}
running code for ------------------------------------------ test_catch_all -------------------------------------------------------
HTTP Error: 404
{
"endpoints": {
"/add_movie": "POST - Add a movie",
"/help": "GET - View this help message",
"/login": "POST - User login",
"/movies": "GET - Get a list of all movies",
"/rate_movie": "POST - Rate a movie",
"/search_movie?name={movie_name}": "GET - Search for a movie by name"
},
"message": "Welcome to the Movie Rating System API!"
}
"""
# for you this is result which you will get after running the code on flaskapp.py and then this one (2nd time)
"""
running code for ------------------------------------------ test_signup -------------------------------------------------------
HTTP Error: 400
{
"message": "A user with this email already exists."
}
running code for ------------------------------------------ test_login -------------------------------------------------------
{
"key": "14587334ae0ff1fd9bd04a5a85110c2fXPZT0001",
"message": "Login successful",
"user_data": {
"email": "[email protected]",
"id": 1,
"name": "John Doe",
"password": "password123",
"phone": "1234567890"
}
}
running code for ------------------------------------------ test_add_movie -------------------------------------------------------
{
"message": "Movie added successfully"
}
running code for ------------------------------------------ test_get_movies -------------------------------------------------------
[
{
"genre": "Comedy",
"id": 1,
"name": "Home Alone",
"rating": "PG",
"release_date": "01-04-1996"
},
{
"genre": "Crime",
"id": 2,
"name": "The Godfather",
"rating": "R",
"release_date": "01-04-1972"
},
{
"genre": "Action",
"id": 3,
"name": "Avengers: Endgame",
"rating": "PG",
"release_date": "01-04-2019"
},
{
"genre": "Science Fiction",
"id": 4,
"name": "The Matrix",
"rating": 9.0,
"release_date": "1999-03-31"
}
]
running code for ------------------------------------------ test_rate_movie -------------------------------------------------------
{
"message": "Wah, movie rating added in the DB"
}
running code for ------------------------------------------ test_search_movie -------------------------------------------------------
{
"matched_movies": [
{
"average_rating": 3.733333333333333,
"genre": "Action",
"id": 3,
"name": "Avengers: Endgame",
"rating": "PG",
"release_date": "01-04-2019"
}
]
}
running code for ------------------------------------------ test_catch_all -------------------------------------------------------
HTTP Error: 404
{
"endpoints": {
"/add_movie": "POST - Add a movie",
"/help": "GET - View this help message",
"/login": "POST - User login",
"/movies": "GET - Get a list of all movies",
"/rate_movie": "POST - Rate a movie",
"/search_movie?name={movie_name}": "GET - Search for a movie by name"
},
"message": "Welcome to the Movie Rating System API!"
}
"""