44
55from answerking_app .models .models import Category , Item
66from 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 )
159
1610client = Client ()
1711
@@ -104,7 +98,7 @@ def test_get_all_with_categories_returns_ok(self):
10498 self .assertEqual (expected , actual )
10599 self .assertEqual (response .status_code , 200 )
106100
107- def test_get_id_valid_returns_ok (self ):
101+ def test_get_valid_id_returns_ok (self ):
108102 # Arrange
109103 expected : CategoryType = {
110104 "id" : self .test_cat_1 .id ,
@@ -132,20 +126,25 @@ def test_get_id_valid_returns_ok(self):
132126 # Act
133127 response = client .get (f"/api/categories/{ self .test_cat_1 .id } " )
134128 actual = response .json ()
135-
136129 # Assert
137130 self .assertEqual (expected , actual )
138131 self .assertEqual (response .status_code , 200 )
139132
140- def test_get_id_invalid_returns_not_found (self ):
133+ def test_get_invalid_id_returns_not_found (self ):
141134 # 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+ }
143141
144142 # Act
145143 response = client .get ("/api/categories/f" )
146144 actual = response .json ()
147145
148146 # Assert
147+ self .assertIsInstance (actual .pop ("traceId" ), str )
149148 self .assertEqual (expected , actual )
150149 self .assertEqual (response .status_code , 404 )
151150
@@ -161,10 +160,9 @@ def test_post_valid_with_items_returns_ok(self):
161160 "stock" : self .test_item_3 .stock ,
162161 "calories" : self .test_item_3 .calories ,
163162 }
164- post_data : NewCategoryType = {"name" : "Vegetarian" , "items" : [item ]}
163+ post_data : CategoryType = {"name" : "Vegetarian" , "items" : [item ]}
165164
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 }
168166
169167 # Act
170168 response = client .post (
@@ -188,9 +186,9 @@ def test_post_valid_with_items_returns_ok(self):
188186 def test_post_valid_without_items_returns_ok (self ):
189187 # Arrange
190188 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 }
194192
195193 # Act
196194 response = client .post (
@@ -210,7 +208,13 @@ def test_post_valid_without_items_returns_ok(self):
210208 def test_post_invalid_json_returns_bad_request (self ):
211209 # Arrange
212210 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+ }
214218
215219 # Act
216220 response = client .post (
@@ -221,17 +225,23 @@ def test_post_invalid_json_returns_bad_request(self):
221225 actual = response .json ()
222226
223227 # 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 )
226230 self .assertEqual (response .status_code , 400 )
227231
228232 def test_post_invalid_details_returns_bad_request (self ):
229233 # Arrange
230- invalid_post_data : NewCategoryType = {
234+ invalid_post_data : CategoryType = {
231235 "name" : "Vegetarian%" ,
232236 "items" : [],
233237 }
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+ }
235245
236246 # Act
237247 response = client .post (
@@ -242,7 +252,8 @@ def test_post_invalid_details_returns_bad_request(self):
242252 actual = response .json ()
243253
244254 # Assert
245- self .assertEqual (expected_failure_error , actual )
255+ self .assertIsInstance (actual .pop ("traceId" ), str )
256+ self .assertEqual (expected , actual )
246257 self .assertEqual (response .status_code , 400 )
247258
248259 def test_put_valid_without_items_returns_no_items (self ):
@@ -251,14 +262,13 @@ def test_put_valid_without_items_returns_no_items(self):
251262 f"/api/categories/{ self .test_cat_1 .id } "
252263 ).json ()
253264
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+
256267 expected : CategoryType = {
257268 ** post_data ,
258- ** expected_id ,
269+ "id" : self . test_cat_1 . id ,
259270 "items" : [],
260271 }
261-
262272 # Act
263273 response = client .put (
264274 f"/api/categories/{ self .test_cat_1 .id } " ,
@@ -278,22 +288,34 @@ def test_put_valid_without_items_returns_no_items(self):
278288 self .assertEqual (expected , actual )
279289 self .assertEqual (response .status_code , 200 )
280290
281- def test_put_invalid_id_returns_bad_request (self ):
291+ def test_put_invalid_id_returns_not_found (self ):
282292 # 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+ }
284299
285300 # Act
286301 response = client .get ("/api/categories/f" )
287302 actual = response .json ()
288303
289304 # Assert
305+ self .assertIsInstance (actual .pop ("traceId" ), str )
290306 self .assertEqual (expected , actual )
291307 self .assertEqual (response .status_code , 404 )
292308
293309 def test_put_invalid_json_returns_bad_request (self ):
294310 # Arrange
295311 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+ }
297319
298320 # Act
299321 response = client .put (
@@ -304,17 +326,23 @@ def test_put_invalid_json_returns_bad_request(self):
304326 actual = response .json ()
305327
306328 # 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 )
309331 self .assertEqual (response .status_code , 400 )
310332
311333 def test_put_invalid_name_returns_bad_request (self ):
312334 # Arrange
313- invalid_post_data : NewCategoryType = {
335+ invalid_post_data : CategoryType = {
314336 "name" : "New Name*" ,
315337 "items" : [],
316338 }
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+ }
318346
319347 # Act
320348 response = client .put (
@@ -325,10 +353,11 @@ def test_put_invalid_name_returns_bad_request(self):
325353 actual = response .json ()
326354
327355 # Assert
328- self .assertEqual (expected_failure_error , actual )
356+ self .assertIsInstance (actual .pop ("traceId" ), str )
357+ self .assertEqual (expected , actual )
329358 self .assertEqual (response .status_code , 400 )
330359
331- def test_put_invalid_item_returns_bad_request (self ):
360+ def test_put_not_existing_item_returns_not_found (self ):
332361 # Arrange
333362 item : ItemType = {
334363 "id" : - 1 ,
@@ -338,11 +367,17 @@ def test_put_invalid_item_returns_bad_request(self):
338367 "stock" : self .test_item_1 .stock ,
339368 "calories" : self .test_item_1 .calories ,
340369 }
341- invalid_post_data : NewCategoryType = {
370+ invalid_post_data : CategoryType = {
342371 "name" : "New Name" ,
343372 "items" : [item ],
344373 }
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+ }
346381
347382 # Act
348383 response = client .put (
@@ -353,8 +388,9 @@ def test_put_invalid_item_returns_bad_request(self):
353388 actual = response .json ()
354389
355390 # 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 )
358394
359395 def test_put_wrong_item_except_id_returns_correct_item_details (self ):
360396 # Arrange
@@ -366,7 +402,7 @@ def test_put_wrong_item_except_id_returns_correct_item_details(self):
366402 "stock" : 666 ,
367403 "calories" : 666 ,
368404 }
369- invalid_post_data : NewCategoryType = {
405+ invalid_post_data : CategoryType = {
370406 "name" : "Burgers" ,
371407 "items" : [item_with_wrong_info ],
372408 }
@@ -412,13 +448,18 @@ def test_delete_valid_returns_ok(self):
412448
413449 def test_delete_invalid_id_returns_not_found (self ):
414450 # 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+ }
417457 # Act
418458 response = client .delete ("/api/categories/f" )
419459 actual = response .json ()
420460
421461 # Assert
462+ self .assertIsInstance (actual .pop ("traceId" ), str )
422463 self .assertEqual (expected , actual )
423464 self .assertEqual (response .status_code , 404 )
424465
@@ -461,15 +502,17 @@ def test_put_add_duplicated_item_in_url_to_category_return_400(self):
461502 content_type = "application/json" ,
462503 )
463504 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/" ,
469510 }
511+
470512 # Assert
513+ self .assertIsInstance (actual .pop ("traceId" ), str )
471514 self .assertEqual (response .status_code , 400 )
472- self .assertEqual (actual , error_msg )
515+ self .assertEqual (actual , expected )
473516
474517
475518class CategoryTestsDB (TransactionTestCase ):
@@ -508,7 +551,7 @@ def tearDown(self):
508551
509552 def test_post_duplicated_name_returns_400 (self ):
510553 # Arrange
511- post_data : NewCategoryType = {"name" : "Vegan" , "items" : []}
554+ post_data : CategoryType = {"name" : "Vegan" , "items" : []}
512555 client .post (
513556 "/api/categories" , post_data , content_type = "application/json"
514557 )
@@ -518,16 +561,23 @@ def test_post_duplicated_name_returns_400(self):
518561 "/api/categories" , post_data , content_type = "application/json"
519562 )
520563
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+
522571 actual = response .json ()
523572
524573 # Assert
574+ self .assertIsInstance (actual .pop ("traceId" ), str )
525575 self .assertEqual (expected , actual )
526576 self .assertEqual (response .status_code , 400 )
527577
528578 def test_put_duplicated_name_returns_400 (self ):
529579 # Arrange
530- post_data : NewCategoryType = {"name" : "Vegan" , "items" : []}
580+ post_data : CategoryType = {"name" : "Vegan" , "items" : []}
531581
532582 client .post (
533583 "/api/categories" , post_data , content_type = "application/json"
@@ -540,8 +590,14 @@ def test_put_duplicated_name_returns_400(self):
540590 content_type = "application/json" ,
541591 )
542592 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+ }
544599
545600 # Assert
601+ self .assertIsInstance (actual .pop ("traceId" ), str )
546602 self .assertEqual (expected , actual )
547603 self .assertEqual (response .status_code , 400 )
0 commit comments