@@ -84,7 +84,9 @@ def _create_pets(self, count: int = 1) -> list:
8484 test_pet = PetFactory ()
8585 response = self .client .post (BASE_URL , json = test_pet .serialize ())
8686 self .assertEqual (
87- response .status_code , status .HTTP_201_CREATED , "Could not create test pet"
87+ response .status_code ,
88+ status .HTTP_201_CREATED ,
89+ "Could not create test pet" ,
8890 )
8991 new_pet = response .get_json ()
9092 test_pet .id = new_pet ["id" ]
@@ -233,8 +235,7 @@ def test_query_pet_list_by_category(self):
233235 test_category = pets [0 ].category
234236 category_pets = [pet for pet in pets if pet .category == test_category ]
235237 response = self .client .get (
236- BASE_URL ,
237- query_string = f"category={ quote_plus (test_category )} "
238+ BASE_URL , query_string = f"category={ quote_plus (test_category )} "
238239 )
239240 self .assertEqual (response .status_code , status .HTTP_200_OK )
240241 data = response .get_json ()
@@ -254,9 +255,7 @@ def test_query_by_availability(self):
254255 logging .debug ("Unavailable Pets [%d] %s" , unavailable_count , unavailable_pets )
255256
256257 # test for available
257- response = self .client .get (
258- BASE_URL , query_string = "available=true"
259- )
258+ response = self .client .get (BASE_URL , query_string = "available=true" )
260259 self .assertEqual (response .status_code , status .HTTP_200_OK )
261260 data = response .get_json ()
262261 self .assertEqual (len (data ), available_count )
@@ -265,9 +264,7 @@ def test_query_by_availability(self):
265264 self .assertEqual (pet ["available" ], True )
266265
267266 # test for unavailable
268- response = self .client .get (
269- BASE_URL , query_string = "available=false"
270- )
267+ response = self .client .get (BASE_URL , query_string = "available=false" )
271268 self .assertEqual (response .status_code , status .HTTP_200_OK )
272269 data = response .get_json ()
273270 self .assertEqual (len (data ), unavailable_count )
@@ -296,9 +293,16 @@ def test_query_by_gender(self):
296293 # ----------------------------------------------------------
297294 def test_purchase_a_pet (self ):
298295 """It should Purchase a Pet"""
299- pets = self ._create_pets (10 )
300- available_pets = [pet for pet in pets if pet .available is True ]
301- pet = available_pets [0 ]
296+ # Create a pet that is available for purchase
297+ pet = PetFactory ()
298+ pet .available = True
299+ response = self .client .post (BASE_URL , json = pet .serialize ())
300+ self .assertEqual (response .status_code , status .HTTP_201_CREATED )
301+ data = response .get_json ()
302+ pet .id = data ["id" ]
303+ self .assertEqual (data ["available" ], True )
304+
305+ # Call purchase on the created id and check the results
302306 response = self .client .put (f"{ BASE_URL } /{ pet .id } /purchase" )
303307 self .assertEqual (response .status_code , status .HTTP_200_OK )
304308 response = self .client .get (f"{ BASE_URL } /{ pet .id } " )
@@ -369,16 +373,16 @@ def test_create_pet_bad_gender(self):
369373 # T E S T M O C K S
370374 ######################################################################
371375
372- @patch (' service.routes.Pet.find_by_name' )
376+ @patch (" service.routes.Pet.find_by_name" )
373377 def test_bad_request (self , bad_request_mock ):
374378 """It should return a Bad Request error from Find By Name"""
375379 bad_request_mock .side_effect = DataValidationError ()
376- response = self .client .get (BASE_URL , query_string = ' name=fido' )
380+ response = self .client .get (BASE_URL , query_string = " name=fido" )
377381 self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
378382
379- @patch (' service.routes.Pet.find_by_name' )
383+ @patch (" service.routes.Pet.find_by_name" )
380384 def test_mock_search_data (self , pet_find_mock ):
381385 """It should showing how to mock data"""
382- pet_find_mock .return_value = [MagicMock (serialize = lambda : {' name' : ' fido' })]
383- response = self .client .get (BASE_URL , query_string = ' name=fido' )
386+ pet_find_mock .return_value = [MagicMock (serialize = lambda : {" name" : " fido" })]
387+ response = self .client .get (BASE_URL , query_string = " name=fido" )
384388 self .assertEqual (response .status_code , status .HTTP_200_OK )
0 commit comments