-
Notifications
You must be signed in to change notification settings - Fork 0
/
tst.py
379 lines (257 loc) · 8.37 KB
/
tst.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""
Client module for testing
"""
import requests
import json
# Sources paths
port = '5001'
root = 'http://127.0.0.1:' + port
# root = 'http://172.18.0.4:5001'
dishes = root + '/dishes'
header = {'Content-Type': 'application/json'}
# Function for communicating with server
def get_all_dishes():
return requests.get(dishes)
def post_dish(name):
return requests.post(dishes, headers=header, data=json.dumps({'name': name}))
def get_dish_by_name(name): # TODO: remove this or previous func
return requests.get(dishes + '/' + name)
def delete_dish_by_name(name: str):
return requests.delete(dishes + '/' + name)
def get_dish_by_idx(idx):
return requests.get(dishes + '/' + idx)
def delete_dish_by_idx(idx):
return requests.delete(dishes + '/' + idx)
# Verification functions
def print_res(res):
print(res.json(), res)
print()
def verify_res_json(res, json):
assert res.json() == json
def verify_res_code(res, code):
assert res.status_code == code
def verify_res(res, json, code):
verify_res_json(res, json)
verify_res_code(res, code)
# Dishes Tests
print(f"Test 1#: Adding a 'fish' dish. Should return 1 (index of the dish) with code 201")
res = post_dish('fish')
print_res(res)
verify_res_code(res, 201)
print(f"Adding an invalid dish ('zish')")
res = post_dish('zish')
print_res(res)
verify_res_code(res, 422)
print(f"Test 2#: Adding a 'chips' dish. Should return 1 (index of the dish) with code 201")
res = post_dish('chips')
print_res(res)
verify_res_code(res, 201)
print(f"Test 3#: getting all dishes. Should return list with fish and chips dishes")
res = get_all_dishes()
print_res(res)
verify_res_code(res, 200)
print(f"Test 4#: Adding a 'fish' dish again. Should return -2 with code ?")
res = post_dish('fish')
print_res(res)
# TODO: verify_res_code(res, 201)
# TODO: add a test that tries to add 'mish' or smth to make sure we treat it correctly..
print(f"Test 5#: Getting dish of index 1. Should return the 'chips' dish")
res = get_dish_by_idx('1')
print_res(res)
verify_res_code(res, 200)
print(f"Test 6#: Getting dish by name 'fish'. Should return the 'fish' dish")
res = get_dish_by_name('fish')
print_res(res)
verify_res_code(res, 200)
print(f"Test 7#: Deleting a dish of index 1. Should return code 200 and value 1 (the index of chips)")
res = delete_dish_by_idx('1')
print_res(res)
verify_res_code(res, 200)
assert res.text.rstrip() == '1'
print(f"Test 8#: Deleting a dish by name 'fish'. NOTE: note 100% regarding expected return values... Assuming it should return the index, which is supposed to be 0. Verifying response code of 200, is that the expected value?")
res = delete_dish_by_name('fish')
print_res(res)
verify_res_code(res, 200)
assert res.text.rstrip() == '0'
### Uncommenting tests
print(f"Test 3#: Deleting the whole dishes resource. Should give an error with code 405")
res = requests.delete(dishes)
print_res(res)
verify_res_code(res, 405)
print(f"Test 6#: Getting dish of index 9. Should return -5 with code 404")
res = get_dish_by_idx('9')
print_res(res)
verify_res_code(res, 404)
assert res.text.rstrip() == "-5" # TODO: why do we need rstrip? Why do we have a linebreak in the reponse?
print(f"Test 7#: Getting a dish 'phish'. Should return -5 with status 404.")
res = get_dish_by_name('phish')
print_res(res)
verify_res_code(res, 404)
assert res.text.rstrip() == '-5'
print("Listing all dishes")
res = get_all_dishes()
print_res(res)
verify_res_code(res, 200)
print(f"Test 10#: Deleting a dish of index 9. Should return -5 with status 404.")
response = delete_dish_by_idx('9')
print_res(response)
verify_res_code(response, 404)
assert response.text.rstrip() == '-5'
print(f"Test 11#: Deleting a dish 'phish'. Should return -5 with status 404.")
response = delete_dish_by_name('phish')
print_res(response)
verify_res_code(response, 404)
assert response.text.rstrip() == '-5'
# Tests for Meals
post_dish('salad')
post_dish('steak')
post_dish('ice cream')
print(f"Printing all existing dishes at this point:")
print_res(get_all_dishes())
# Adding a first meal
print(f"Adding a meal. Should return 1")
meals = root + '/meals'
header = {'Content-Type': 'application/json'}
basic_meal = json.dumps({'name': 'basic', 'appetizer': 2, 'main': 3, 'dessert': 4})
res = requests.post(meals, headers=header, data=basic_meal)
print_res(res)
print(f"Adding a meal. Should return 1")
meals = root + '/meals'
header = {'Content-Type': 'application/json'}
basic_meal = json.dumps({'name': 'basic2', 'appetizer': 2, 'main': 3, 'dessert': 4})
res = requests.post(meals, headers=header, data=basic_meal)
print_res(res)
# Test: Getting all meals. Should have a single 'basic' meal.
def get_all_meals():
return requests.get(meals, headers=header)
print(f"Printing all available meals:")
res = get_all_meals()
print(res.json())
print()
def get_meal(identifier):
return requests.get(meals + '/' + identifier)
def delete_meal(identifier):
return requests.delete(meals + '/' + identifier)
# Getting a meal by index
print(f"Getting meal of index 1")
res = get_meal('1')
print_res(res)
print()
# Getting a meal by name
print(f"Getting meal by name 'basic'")
res = get_meal('basic')
print_res(res)
print()
# Delete meal by index
print(f"Deleteing meal of index 1")
res = delete_meal('1')
print_res(res)
print()
print(f"Adding a bread dish")
res = post_dish('bread')
print_res(res)
print()
print("Adding a hamburger dish")
res = post_dish('hamburger')
print_res(res)
print()
print("Adding a souffle dish")
res = post_dish('souffle')
print_res(res)
print()
alt_dish = json.dumps({'name': 'alt', 'appetizer': 5, 'main': 6, 'dessert': 7})
print(f"Adding an 'alt' meal: {alt_dish}")
res = requests.post(meals, headers=header, data=alt_dish)
print_res(res)
print()
# List meals
print(f"Listing all available meals. Should contain an 'alt' meal only.")
res = get_all_meals()
print(res.json())
print()
# Add and delete a meal by name
print(f"Deleting an 'alt' meal. Should return 2 (index of 'alt' meal)")
res = delete_meal('alt')
print_res(res)
print()
# Test for getting a meal with invalid id
print(f"Testing meals/get on invalid index. Should return -5 and code 404")
res = get_meal('7')
print_res(res)
print()
# Test for getting a meal with invalid name
print(f"Testing meals/get on invalid name. Should return -5 and code 404")
res = get_meal('Italian Monday')
print_res(res)
print()
# Test for getting a meal with invalid id
print(f"Testing meals/delete on invalid index. Should return -5 and code 404")
res = delete_meal('7')
print_res(res)
print()
# Test for getting a meal with invalid name
print(f"Testing meals/delete on invalid name. Should return -5 and code 404")
res = delete_meal('Italian Monday')
print_res(res)
print()
# re-adding 'basic' meal
print(f"Adding the 'basic' meal again")
res = requests.post(meals, headers=header, data=basic_meal)
# List meals
print(f"Listing all available meals:")
res = get_all_meals()
print(res.json())
print()
def update_meal(idx, name, appetizer, main, dessert):
load = json.dumps({'name': name, 'appetizer': appetizer, 'main': main, 'dessert': dessert})
return requests.put(meals + '/' + idx, headers=header, data=load)
print(f"Printing all dishes")
res = get_all_dishes()
print_res(res)
verify_res_code(res, 200)
# Getting a meal by name
print(f"Getting meal by name 'basic2'")
res = get_meal('basic2')
print_res(res)
print()
# Updating an existing meal with put
print(f"Updating the 'basic2' meal")
res = update_meal('3', 'basic_updated', 3, 4, 7)
print_res(res)
print()
# Getting a meal by name
print(f"Getting the update meal (basic_updated)")
res = get_meal('basic_updated')
print_res(res)
print()
print(f"Printing all available meals")
res = get_all_meals()
print_res(res)
print()
res = requests.post(meals, headers=header, data=basic_meal)
# Updating an invalid meal with put
print(f"Updating a non-existing meal")
res = update_meal('7', 'basic_updated', 3, 4, 7)
print_res(res)
print()
print(f"Adding a dish")
res = post_dish("focaccia")
print_res(res)
print()
print(f"Getting the souffle dish")
res = get_dish_by_name("souffle")
print_res(res)
print()
print(f"Deleting a dish that is already a part of a meal")
print(f"We will change t5he basic_updated meal")
res = get_meal('basic_updated')
print_res(res)
print()
print(f"We now delete dish with index 7")
res = delete_dish_by_idx('7')
print_res(res)
print()
print(f"Let's see how this affected the meal:")
res = get_meal('basic_updated')
print_res(res)
print()