4
4
5
5
from answerking_app .models .models import Category , Item
6
6
from answerking_app .utils .ErrorType import ErrorMessage
7
- from answerking_app .utils .model_types import (
8
- CategoryType ,
9
- DetailError ,
10
- IDType ,
11
- ItemType ,
12
- NewCategoryName ,
13
- NewCategoryType ,
14
- )
7
+ from answerking_app .utils .model_types import (CategoryType , DetailError ,
8
+ ItemType )
15
9
16
10
client = Client ()
17
11
@@ -104,7 +98,7 @@ def test_get_all_with_categories_returns_ok(self):
104
98
self .assertEqual (expected , actual )
105
99
self .assertEqual (response .status_code , 200 )
106
100
107
- def test_get_id_valid_returns_ok (self ):
101
+ def test_get_valid_id_returns_ok (self ):
108
102
# Arrange
109
103
expected : CategoryType = {
110
104
"id" : self .test_cat_1 .id ,
@@ -132,20 +126,25 @@ def test_get_id_valid_returns_ok(self):
132
126
# Act
133
127
response = client .get (f"/api/categories/{ self .test_cat_1 .id } " )
134
128
actual = response .json ()
135
-
136
129
# Assert
137
130
self .assertEqual (expected , actual )
138
131
self .assertEqual (response .status_code , 200 )
139
132
140
- def test_get_id_invalid_returns_not_found (self ):
133
+ def test_get_invalid_id_returns_not_found (self ):
141
134
# Arrange
142
- expected : DetailError = {"detail" : "/api/categories/f not found" }
135
+ expected : DetailError = {
136
+ "detail" : "Not Found" ,
137
+ "status" : 404 ,
138
+ "title" : "Resource not found" ,
139
+ "type" : "http://testserver/problems/not_found/" ,
140
+ }
143
141
144
142
# Act
145
143
response = client .get ("/api/categories/f" )
146
144
actual = response .json ()
147
145
148
146
# Assert
147
+ self .assertIsInstance (actual .pop ("traceId" ), str )
149
148
self .assertEqual (expected , actual )
150
149
self .assertEqual (response .status_code , 404 )
151
150
@@ -161,10 +160,9 @@ def test_post_valid_with_items_returns_ok(self):
161
160
"stock" : self .test_item_3 .stock ,
162
161
"calories" : self .test_item_3 .calories ,
163
162
}
164
- post_data : NewCategoryType = {"name" : "Vegetarian" , "items" : [item ]}
163
+ post_data : CategoryType = {"name" : "Vegetarian" , "items" : [item ]}
165
164
166
- expected_id : IDType = {"id" : self .test_cat_2 .id + 1 }
167
- expected : CategoryType = {** post_data , ** expected_id }
165
+ expected : CategoryType = {** post_data , "id" : self .test_cat_2 .id + 1 }
168
166
169
167
# Act
170
168
response = client .post (
@@ -188,9 +186,9 @@ def test_post_valid_with_items_returns_ok(self):
188
186
def test_post_valid_without_items_returns_ok (self ):
189
187
# Arrange
190
188
old_list = client .get ("/api/categories" ).json ()
191
- post_data : NewCategoryType = {"name" : "Gluten Free" , "items" : []}
192
- expected_id : IDType = { "id" : self . test_cat_2 . id + 1 }
193
- expected : CategoryType = {** post_data , ** expected_id }
189
+ post_data : CategoryType = {"name" : "Gluten Free" , "items" : []}
190
+
191
+ expected : CategoryType = {** post_data , "id" : self . test_cat_2 . id + 1 }
194
192
195
193
# Act
196
194
response = client .post (
@@ -210,7 +208,13 @@ def test_post_valid_without_items_returns_ok(self):
210
208
def test_post_invalid_json_returns_bad_request (self ):
211
209
# Arrange
212
210
invalid_json_data : str = '{"invalid": }'
213
- expected_json_error : str = "JSON parse error -"
211
+ expected : DetailError = {
212
+ "detail" : "Parsing JSON Error" ,
213
+ "errors" : "JSON parse error - Expecting value: line 1 column 13 (char 12)" ,
214
+ "status" : 400 ,
215
+ "title" : "Invalid input json." ,
216
+ "type" : "http://testserver/problems/error/" ,
217
+ }
214
218
215
219
# Act
216
220
response = client .post (
@@ -221,17 +225,23 @@ def test_post_invalid_json_returns_bad_request(self):
221
225
actual = response .json ()
222
226
223
227
# Assert
224
- self .assertRaises ( ParseError )
225
- self .assertIn ( expected_json_error , actual [ "detail" ] )
228
+ self .assertIsInstance ( actual . pop ( "traceId" ), str )
229
+ self .assertEqual ( expected , actual )
226
230
self .assertEqual (response .status_code , 400 )
227
231
228
232
def test_post_invalid_details_returns_bad_request (self ):
229
233
# Arrange
230
- invalid_post_data : NewCategoryType = {
234
+ invalid_post_data : CategoryType = {
231
235
"name" : "Vegetarian%" ,
232
236
"items" : [],
233
237
}
234
- expected_failure_error : dict = {"name" : ["Enter a valid value." ]}
238
+ expected : DetailError = {
239
+ "detail" : "Validation Error" ,
240
+ "errors" : {"name" : ["Enter a valid value." ]},
241
+ "status" : 400 ,
242
+ "title" : "Invalid input." ,
243
+ "type" : "http://testserver/problems/error/" ,
244
+ }
235
245
236
246
# Act
237
247
response = client .post (
@@ -242,7 +252,8 @@ def test_post_invalid_details_returns_bad_request(self):
242
252
actual = response .json ()
243
253
244
254
# Assert
245
- self .assertEqual (expected_failure_error , actual )
255
+ self .assertIsInstance (actual .pop ("traceId" ), str )
256
+ self .assertEqual (expected , actual )
246
257
self .assertEqual (response .status_code , 400 )
247
258
248
259
def test_put_valid_without_items_returns_no_items (self ):
@@ -251,14 +262,13 @@ def test_put_valid_without_items_returns_no_items(self):
251
262
f"/api/categories/{ self .test_cat_1 .id } "
252
263
).json ()
253
264
254
- post_data : NewCategoryName = {"name" : "New Name" }
255
- expected_id : IDType = { "id" : self . test_cat_1 . id }
265
+ post_data : CategoryType = {"name" : "New Name" }
266
+
256
267
expected : CategoryType = {
257
268
** post_data ,
258
- ** expected_id ,
269
+ "id" : self . test_cat_1 . id ,
259
270
"items" : [],
260
271
}
261
-
262
272
# Act
263
273
response = client .put (
264
274
f"/api/categories/{ self .test_cat_1 .id } " ,
@@ -278,22 +288,34 @@ def test_put_valid_without_items_returns_no_items(self):
278
288
self .assertEqual (expected , actual )
279
289
self .assertEqual (response .status_code , 200 )
280
290
281
- def test_put_invalid_id_returns_bad_request (self ):
291
+ def test_put_invalid_id_returns_not_found (self ):
282
292
# Arrange
283
- expected : DetailError = {"detail" : "/api/categories/f not found" }
293
+ expected : DetailError = {
294
+ "detail" : "Not Found" ,
295
+ "status" : 404 ,
296
+ "title" : "Resource not found" ,
297
+ "type" : "http://testserver/problems/not_found/" ,
298
+ }
284
299
285
300
# Act
286
301
response = client .get ("/api/categories/f" )
287
302
actual = response .json ()
288
303
289
304
# Assert
305
+ self .assertIsInstance (actual .pop ("traceId" ), str )
290
306
self .assertEqual (expected , actual )
291
307
self .assertEqual (response .status_code , 404 )
292
308
293
309
def test_put_invalid_json_returns_bad_request (self ):
294
310
# Arrange
295
311
invalid_json_data : str = '{"invalid": }'
296
- expected_json_error : str = "JSON parse error -"
312
+ expected : DetailError = {
313
+ "detail" : "Parsing JSON Error" ,
314
+ "errors" : "JSON parse error - Expecting value: line 1 column 13 (char 12)" ,
315
+ "status" : 400 ,
316
+ "title" : "Invalid input json." ,
317
+ "type" : "http://testserver/problems/error/" ,
318
+ }
297
319
298
320
# Act
299
321
response = client .put (
@@ -304,17 +326,23 @@ def test_put_invalid_json_returns_bad_request(self):
304
326
actual = response .json ()
305
327
306
328
# Assert
307
- self .assertRaises ( ParseError )
308
- self .assertIn ( expected_json_error , actual [ "detail" ] )
329
+ self .assertIsInstance ( actual . pop ( "traceId" ), str )
330
+ self .assertEqual ( expected , actual )
309
331
self .assertEqual (response .status_code , 400 )
310
332
311
333
def test_put_invalid_name_returns_bad_request (self ):
312
334
# Arrange
313
- invalid_post_data : NewCategoryType = {
335
+ invalid_post_data : CategoryType = {
314
336
"name" : "New Name*" ,
315
337
"items" : [],
316
338
}
317
- expected_failure_error : dict = {"name" : ["Enter a valid value." ]}
339
+ expected : DetailError = {
340
+ "detail" : "Validation Error" ,
341
+ "errors" : {"name" : ["Enter a valid value." ]},
342
+ "status" : 400 ,
343
+ "title" : "Invalid input." ,
344
+ "type" : "http://testserver/problems/error/" ,
345
+ }
318
346
319
347
# Act
320
348
response = client .put (
@@ -325,10 +353,11 @@ def test_put_invalid_name_returns_bad_request(self):
325
353
actual = response .json ()
326
354
327
355
# Assert
328
- self .assertEqual (expected_failure_error , actual )
356
+ self .assertIsInstance (actual .pop ("traceId" ), str )
357
+ self .assertEqual (expected , actual )
329
358
self .assertEqual (response .status_code , 400 )
330
359
331
- def test_put_invalid_item_returns_bad_request (self ):
360
+ def test_put_not_existing_item_returns_not_found (self ):
332
361
# Arrange
333
362
item : ItemType = {
334
363
"id" : - 1 ,
@@ -338,11 +367,17 @@ def test_put_invalid_item_returns_bad_request(self):
338
367
"stock" : self .test_item_1 .stock ,
339
368
"calories" : self .test_item_1 .calories ,
340
369
}
341
- invalid_post_data : NewCategoryType = {
370
+ invalid_post_data : CategoryType = {
342
371
"name" : "New Name" ,
343
372
"items" : [item ],
344
373
}
345
- expected_failure_error : dict = {"detail" : "This item does not exist" }
374
+ expected : DetailError = {
375
+ "detail" : "Object was not Found" ,
376
+ "errors" : ["Item matching query does not exist." ],
377
+ "status" : 404 ,
378
+ "title" : "Resource not found" ,
379
+ "type" : "http://testserver/problems/error/" ,
380
+ }
346
381
347
382
# Act
348
383
response = client .put (
@@ -353,8 +388,9 @@ def test_put_invalid_item_returns_bad_request(self):
353
388
actual = response .json ()
354
389
355
390
# Assert
356
- self .assertEqual (expected_failure_error , actual )
357
- self .assertEqual (response .status_code , 400 )
391
+ self .assertIsInstance (actual .pop ("traceId" ), str )
392
+ self .assertEqual (expected , actual )
393
+ self .assertEqual (response .status_code , 404 )
358
394
359
395
def test_put_wrong_item_except_id_returns_correct_item_details (self ):
360
396
# Arrange
@@ -366,7 +402,7 @@ def test_put_wrong_item_except_id_returns_correct_item_details(self):
366
402
"stock" : 666 ,
367
403
"calories" : 666 ,
368
404
}
369
- invalid_post_data : NewCategoryType = {
405
+ invalid_post_data : CategoryType = {
370
406
"name" : "Burgers" ,
371
407
"items" : [item_with_wrong_info ],
372
408
}
@@ -412,13 +448,18 @@ def test_delete_valid_returns_ok(self):
412
448
413
449
def test_delete_invalid_id_returns_not_found (self ):
414
450
# Arrange
415
- expected : DetailError = {"detail" : "/api/categories/f not found" }
416
-
451
+ expected : DetailError = {
452
+ "detail" : "Not Found" ,
453
+ "status" : 404 ,
454
+ "title" : "Resource not found" ,
455
+ "type" : "http://testserver/problems/not_found/" ,
456
+ }
417
457
# Act
418
458
response = client .delete ("/api/categories/f" )
419
459
actual = response .json ()
420
460
421
461
# Assert
462
+ self .assertIsInstance (actual .pop ("traceId" ), str )
422
463
self .assertEqual (expected , actual )
423
464
self .assertEqual (response .status_code , 404 )
424
465
@@ -461,15 +502,17 @@ def test_put_add_duplicated_item_in_url_to_category_return_400(self):
461
502
content_type = "application/json" ,
462
503
)
463
504
actual = response .json ()
464
- error_msg : ErrorMessage = {
465
- "error " : {
466
- "message " : "Resource update failure" ,
467
- "details " : "Item already in category " ,
468
- }
505
+ expected : DetailError = {
506
+ "detail " : "A server error occurred." ,
507
+ "status " : 400 ,
508
+ "title " : "A server error occurred. " ,
509
+ "type" : "http://testserver/problems/error/" ,
469
510
}
511
+
470
512
# Assert
513
+ self .assertIsInstance (actual .pop ("traceId" ), str )
471
514
self .assertEqual (response .status_code , 400 )
472
- self .assertEqual (actual , error_msg )
515
+ self .assertEqual (actual , expected )
473
516
474
517
475
518
class CategoryTestsDB (TransactionTestCase ):
@@ -508,7 +551,7 @@ def tearDown(self):
508
551
509
552
def test_post_duplicated_name_returns_400 (self ):
510
553
# Arrange
511
- post_data : NewCategoryType = {"name" : "Vegan" , "items" : []}
554
+ post_data : CategoryType = {"name" : "Vegan" , "items" : []}
512
555
client .post (
513
556
"/api/categories" , post_data , content_type = "application/json"
514
557
)
@@ -518,16 +561,23 @@ def test_post_duplicated_name_returns_400(self):
518
561
"/api/categories" , post_data , content_type = "application/json"
519
562
)
520
563
521
- expected : DetailError = {"detail" : "This name already exists" }
564
+ expected : DetailError = {
565
+ "detail" : "This name already exists" ,
566
+ "status" : 400 ,
567
+ "title" : "A server error occurred." ,
568
+ "type" : "http://testserver/problems/error/" ,
569
+ }
570
+
522
571
actual = response .json ()
523
572
524
573
# Assert
574
+ self .assertIsInstance (actual .pop ("traceId" ), str )
525
575
self .assertEqual (expected , actual )
526
576
self .assertEqual (response .status_code , 400 )
527
577
528
578
def test_put_duplicated_name_returns_400 (self ):
529
579
# Arrange
530
- post_data : NewCategoryType = {"name" : "Vegan" , "items" : []}
580
+ post_data : CategoryType = {"name" : "Vegan" , "items" : []}
531
581
532
582
client .post (
533
583
"/api/categories" , post_data , content_type = "application/json"
@@ -540,8 +590,14 @@ def test_put_duplicated_name_returns_400(self):
540
590
content_type = "application/json" ,
541
591
)
542
592
actual = response .json ()
543
- expected : DetailError = {"detail" : "This name already exists" }
593
+ expected : DetailError = {
594
+ "detail" : "This name already exists" ,
595
+ "status" : 400 ,
596
+ "title" : "A server error occurred." ,
597
+ "type" : "http://testserver/problems/error/" ,
598
+ }
544
599
545
600
# Assert
601
+ self .assertIsInstance (actual .pop ("traceId" ), str )
546
602
self .assertEqual (expected , actual )
547
603
self .assertEqual (response .status_code , 400 )
0 commit comments