2323# from unittest.mock import MagicMock, patch
2424from urllib .parse import quote_plus
2525from wsgi import app
26+
2627# from service import create_app
2728from service .common import status
2829from service .models import Pet , db
@@ -79,7 +80,9 @@ def _create_pets(self, count):
7980 test_pet = PetFactory ()
8081 response = self .client .post (BASE_URL , json = test_pet .serialize ())
8182 self .assertEqual (
82- response .status_code , status .HTTP_201_CREATED , "Could not create test pet"
83+ response .status_code ,
84+ status .HTTP_201_CREATED ,
85+ "Could not create test pet" ,
8386 )
8487 new_pet = response .get_json ()
8588 test_pet .id = new_pet ["id" ]
@@ -189,8 +192,7 @@ def test_query_pet_list_by_category(self):
189192 test_category = pets [0 ].category
190193 category_pets = [pet for pet in pets if pet .category == test_category ]
191194 response = self .client .get (
192- BASE_URL ,
193- query_string = f"category={ quote_plus (test_category )} "
195+ BASE_URL , query_string = f"category={ quote_plus (test_category )} "
194196 )
195197 self .assertEqual (response .status_code , status .HTTP_200_OK )
196198 data = response .get_json ()
@@ -200,9 +202,46 @@ def test_query_pet_list_by_category(self):
200202 self .assertEqual (pet ["category" ], test_category )
201203
202204 ######################################################################
203- # T E S T S A D P A T H S
205+ # T E S T A C T I O N S
204206 ######################################################################
205207
208+ def test_purchase_a_pet (self ):
209+ """It should Purchase a Pet"""
210+ pets = self ._create_pets (10 )
211+ available_pets = [pet for pet in pets if pet .available is True ]
212+ pet = available_pets [0 ]
213+ response = self .client .put (f"{ BASE_URL } /{ pet .id } /purchase" )
214+ self .assertEqual (response .status_code , status .HTTP_200_OK )
215+ response = self .client .get (f"{ BASE_URL } /{ pet .id } " )
216+ self .assertEqual (response .status_code , status .HTTP_200_OK )
217+ data = response .get_json ()
218+ logging .debug ("Response data: %s" , data )
219+ self .assertEqual (data ["available" ], False )
220+
221+ def test_purchase_not_available (self ):
222+ """It should not Purchase a Pet that is not available"""
223+ pets = self ._create_pets (10 )
224+ unavailable_pets = [pet for pet in pets if pet .available is False ]
225+ pet = unavailable_pets [0 ]
226+ response = self .client .put (f"{ BASE_URL } /{ pet .id } /purchase" )
227+ self .assertEqual (response .status_code , status .HTTP_409_CONFLICT )
228+
229+
230+ ######################################################################
231+ # T E S T S A D P A T H S
232+ ######################################################################
233+ class TestSadPaths (TestCase ):
234+ """Test REST Exception Handling"""
235+
236+ def setUp (self ):
237+ """Runs before each test"""
238+ self .client = app .test_client ()
239+
240+ def test_method_not_allowed (self ):
241+ """It should not allow update without a pet id"""
242+ response = self .client .put (BASE_URL )
243+ self .assertEqual (response .status_code , status .HTTP_405_METHOD_NOT_ALLOWED )
244+
206245 def test_create_pet_no_data (self ):
207246 """It should not Create a Pet with missing data"""
208247 response = self .client .post (BASE_URL , json = {})
@@ -233,35 +272,10 @@ def test_create_pet_bad_gender(self):
233272 logging .debug (pet )
234273 # change gender to a bad string
235274 test_pet = pet .serialize ()
236- test_pet ["gender" ] = "male" # wrong case
275+ test_pet ["gender" ] = "male" # wrong case
237276 response = self .client .post (BASE_URL , json = test_pet )
238277 self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
239278
240- ######################################################################
241- # T E S T A C T I O N S
242- ######################################################################
243-
244- def test_purchase_a_pet (self ):
245- """It should Purchase a Pet"""
246- pets = self ._create_pets (10 )
247- available_pets = [pet for pet in pets if pet .available is True ]
248- pet = available_pets [0 ]
249- response = self .client .put (f"{ BASE_URL } /{ pet .id } /purchase" )
250- self .assertEqual (response .status_code , status .HTTP_200_OK )
251- response = self .client .get (f"{ BASE_URL } /{ pet .id } " )
252- self .assertEqual (response .status_code , status .HTTP_200_OK )
253- data = response .get_json ()
254- logging .debug ("Response data: %s" , data )
255- self .assertEqual (data ["available" ], False )
256-
257- def test_purchase_not_available (self ):
258- """It should not Purchase a Pet that is not available"""
259- pets = self ._create_pets (10 )
260- unavailable_pets = [pet for pet in pets if pet .available is False ]
261- pet = unavailable_pets [0 ]
262- response = self .client .put (f"{ BASE_URL } /{ pet .id } /purchase" )
263- self .assertEqual (response .status_code , status .HTTP_409_CONFLICT )
264-
265279 ######################################################################
266280 # T E S T M O C K S
267281 ######################################################################
0 commit comments