@@ -104,20 +104,20 @@ class TestPetRoutes(BaseTestCase):
104
104
"""Pet Service Routes tests"""
105
105
106
106
def test_index (self ):
107
- """Test the index page"""
107
+ """It should return the index page"""
108
108
resp = self .app .get ("/" )
109
109
self .assertEqual (resp .status_code , status .HTTP_200_OK )
110
110
self .assertIn (b"Pet Shop" , resp .data )
111
111
112
112
def test_get_pet_list (self ):
113
- """Get a list of Pets"""
113
+ """It should Get a list of Pets"""
114
114
self ._create_pets (5 )
115
115
resp = self .app .get (BASE_URL )
116
116
self .assertEqual (resp .status_code , status .HTTP_200_OK )
117
117
self .assertTrue (len (resp .data ) > 0 )
118
118
119
119
def test_get_pet (self ):
120
- """get a single Pet"""
120
+ """It should Get a single Pet"""
121
121
test_pet = self ._create_pets ()[0 ]
122
122
resp = self .app .get (f"{ BASE_URL } /{ test_pet .id } " , content_type = CONTENT_TYPE_JSON )
123
123
self .assertEqual (resp .status_code , status .HTTP_200_OK )
@@ -126,15 +126,15 @@ def test_get_pet(self):
126
126
self .assertEqual (data ["name" ], test_pet .name )
127
127
128
128
def test_get_pet_not_found (self ):
129
- """Get a Pet that doesn't exist"""
129
+ """It should not Get a Pet that doesn't exist"""
130
130
resp = self .app .get (f"{ BASE_URL } /foo" )
131
131
self .assertEqual (resp .status_code , status .HTTP_404_NOT_FOUND )
132
132
data = resp .get_json ()
133
133
logging .debug ("Response data = %s" , data )
134
134
self .assertIn ("was not found" , data ["message" ])
135
135
136
136
def test_create_pet (self ):
137
- """Create a new Pet"""
137
+ """It should Create a new Pet"""
138
138
test_pet = PetFactory ()
139
139
logging .debug ("Test Pet: %s" , test_pet .serialize ())
140
140
resp = self .app .post (
@@ -168,7 +168,7 @@ def test_create_pet(self):
168
168
self .assertEqual (new_pet ["birthday" ], test_pet .birthday .isoformat ())
169
169
170
170
def test_update_pet (self ):
171
- """Update a Pet"""
171
+ """It should Update a Pet"""
172
172
# create a pet to update
173
173
test_pet = PetFactory ()
174
174
resp = self .app .post (
@@ -194,7 +194,7 @@ def test_update_pet(self):
194
194
self .assertEqual (updated_pet ["category" ], "unknown" )
195
195
196
196
def test_update_pet_with_no_name (self ):
197
- """Update a Pet without assigning a name"""
197
+ """It should not Update a Pet without assigning a name"""
198
198
pet = self ._create_pets ()[0 ]
199
199
pet_data = pet .serialize ()
200
200
del pet_data ["name" ]
@@ -207,7 +207,7 @@ def test_update_pet_with_no_name(self):
207
207
self .assertEqual (resp .status_code , status .HTTP_400_BAD_REQUEST )
208
208
209
209
def test_update_pet_not_found (self ):
210
- """Update a Pet that doesn't exist"""
210
+ """It should not Update a Pet that doesn't exist"""
211
211
resp = self .app .put (
212
212
f"{ BASE_URL } /foo" ,
213
213
json = {},
@@ -217,15 +217,15 @@ def test_update_pet_not_found(self):
217
217
self .assertEqual (resp .status_code , status .HTTP_404_NOT_FOUND )
218
218
219
219
def test_update_pet_not_authorized (self ):
220
- """Update a Pet Not Authorized"""
220
+ """It should not Update a Pet if Not Authorized"""
221
221
pet = self ._create_pets ()[0 ]
222
222
pet_data = pet .serialize ()
223
223
del pet_data ["name" ]
224
224
resp = self .app .put (f"{ BASE_URL } /{ pet .id } " , json = pet_data )
225
225
self .assertEqual (resp .status_code , status .HTTP_401_UNAUTHORIZED )
226
226
227
227
def test_delete_pet (self ):
228
- """Delete a Pet"""
228
+ """It should Delete a Pet"""
229
229
pets = self ._create_pets (5 )
230
230
pet_count = self ._get_pet_count ()
231
231
test_pet = pets [0 ]
@@ -239,15 +239,15 @@ def test_delete_pet(self):
239
239
self .assertEqual (new_count , pet_count - 1 )
240
240
241
241
def test_delete_all_pet (self ):
242
- """Delete All Pets under test only"""
242
+ """It should Delete All Pets under test only"""
243
243
self ._create_pets (1 )
244
244
app .config ["TESTING" ] = False
245
245
resp = self .app .delete (BASE_URL , headers = self .headers )
246
246
self .assertEqual (resp .status_code , status .HTTP_204_NO_CONTENT )
247
247
app .config ["TESTING" ] = True
248
248
249
249
def test_create_pet_with_no_name (self ):
250
- """Create a Pet without a name"""
250
+ """It should not Create a Pet without a name"""
251
251
pet = self ._create_pets ()[0 ]
252
252
new_pet = pet .serialize ()
253
253
del new_pet ["name" ]
@@ -258,19 +258,19 @@ def test_create_pet_with_no_name(self):
258
258
self .assertEqual (resp .status_code , status .HTTP_400_BAD_REQUEST )
259
259
260
260
def test_create_pet_no_content_type (self ):
261
- """Create a Pet with no Content-Type"""
261
+ """It should not Create a Pet with no Content-Type"""
262
262
resp = self .app .post (BASE_URL , data = "bad data" , headers = self .headers )
263
263
self .assertEqual (resp .status_code , status .HTTP_415_UNSUPPORTED_MEDIA_TYPE )
264
264
265
265
def test_create_pet_wrong_content_type (self ):
266
- """Create a Pet with wrong Content-Type"""
266
+ """It should not Create a Pet with wrong Content-Type"""
267
267
resp = self .app .post (
268
268
BASE_URL , data = {}, content_type = "plain/text" , headers = self .headers
269
269
)
270
270
self .assertEqual (resp .status_code , status .HTTP_415_UNSUPPORTED_MEDIA_TYPE )
271
271
272
272
def test_call_create_with_an_id (self ):
273
- """Call create passing an id"""
273
+ """It should not create passing an id"""
274
274
resp = self .app .post (f"{ BASE_URL } /foo" , json = {}, headers = self .headers )
275
275
self .assertEqual (resp .status_code , status .HTTP_405_METHOD_NOT_ALLOWED )
276
276
@@ -279,7 +279,7 @@ class TestPetQuery(BaseTestCase):
279
279
"""Pet Service Query tests"""
280
280
281
281
def test_query_by_name (self ):
282
- """Query Pets by name"""
282
+ """It should Query Pets by name"""
283
283
pets = self ._create_pets (5 )
284
284
test_name = pets [0 ].name
285
285
name_count = len ([pet for pet in pets if pet .name == test_name ])
@@ -292,7 +292,7 @@ def test_query_by_name(self):
292
292
self .assertEqual (pet ["name" ], test_name )
293
293
294
294
def test_query_by_category (self ):
295
- """Query Pets by category"""
295
+ """It should Query Pets by category"""
296
296
pets = self ._create_pets (5 )
297
297
test_category = pets [0 ].category
298
298
category_count = len ([pet for pet in pets if pet .category == test_category ])
@@ -309,44 +309,27 @@ def test_query_by_category(self):
309
309
# test_query_by_availability() does not work because of the way CouchDB
310
310
# handles deletions. Need to upgrade to newer ibmcloudant library
311
311
312
- # def test_query_by_availability(self):
313
- # """Query Pets by availability"""
314
- # pets = self._create_pets(10)
315
- # available_pets = [pet for pet in pets if pet.available is True]
316
- # unavailable_pets = [pet for pet in pets if pet.available is False]
317
- # available_count = len(available_pets)
318
- # unavailable_count = len(unavailable_pets)
319
- # logging.debug("Available Pets [%d] %s", available_count, available_pets)
320
- # logging.debug("Unavailable Pets [%d] %s", unavailable_count, unavailable_pets)
321
-
322
- # # test for available
323
- # resp = self.app.get(
324
- # BASE_URL, query_string="available=true"
325
- # )
326
- # self.assertEqual(resp.status_code, status.HTTP_200_OK)
327
- # data = resp.get_json()
328
- # self.assertEqual(len(data), available_count)
329
- # # check the data just to be sure
330
- # for pet in data:
331
- # self.assertEqual(pet["available"], True)
332
-
333
- # # test for unavailable
334
- # resp = self.app.get(
335
- # BASE_URL, query_string="available=false"
336
- # )
337
- # self.assertEqual(resp.status_code, status.HTTP_200_OK)
338
- # data = resp.get_json()
339
- # self.assertEqual(len(data), unavailable_count)
340
- # # check the data just to be sure
341
- # for pet in data:
342
- # self.assertEqual(pet["available"], False)
312
+ def test_query_by_availability (self ):
313
+ """It should Query Pets by availability"""
314
+ pets = self ._create_pets (5 )
315
+ test_available = pets [0 ].available
316
+ available_count = len ([pet for pet in pets if pet .available == test_available ])
317
+ resp = self .app .get (
318
+ BASE_URL , query_string = f"available={ test_available } "
319
+ )
320
+ self .assertEqual (resp .status_code , status .HTTP_200_OK )
321
+ data = resp .get_json ()
322
+ self .assertEqual (len (data ), available_count )
323
+ # check the data just to be sure
324
+ for pet in data :
325
+ self .assertEqual (pet ["available" ], test_available )
343
326
344
327
345
328
class TestPetActions (BaseTestCase ):
346
329
"""Pet Service Action tests"""
347
330
348
331
def test_purchase_a_pet (self ):
349
- """Purchase a Pet"""
332
+ """It should Purchase a Pet"""
350
333
pets = self ._create_pets (10 )
351
334
available_pets = [pet for pet in pets if pet .available is True ]
352
335
pet = available_pets [0 ]
@@ -361,7 +344,7 @@ def test_purchase_a_pet(self):
361
344
self .assertEqual (data ["available" ], False )
362
345
363
346
def test_purchase_not_available (self ):
364
- """Purchase a Pet that is not available"""
347
+ """It should not Purchase a Pet that is not available"""
365
348
pets = self ._create_pets (10 )
366
349
unavailable_pets = [pet for pet in pets if pet .available is False ]
367
350
pet = unavailable_pets [0 ]
@@ -371,7 +354,7 @@ def test_purchase_not_available(self):
371
354
self .assertEqual (resp .status_code , status .HTTP_409_CONFLICT )
372
355
373
356
def test_purchase_does_not_exist (self ):
374
- """Purchase a Pet that doesn't exist"""
357
+ """It should not Purchase a Pet that doesn't exist"""
375
358
resp = self .app .put (f"{ BASE_URL } /0/purchase" , content_type = CONTENT_TYPE_JSON )
376
359
self .assertEqual (resp .status_code , status .HTTP_404_NOT_FOUND )
377
360
@@ -381,7 +364,7 @@ def test_purchase_does_not_exist(self):
381
364
382
365
@patch ("cloudant.client.Cloudant.__init__" )
383
366
def test_connection_error (self , bad_mock ):
384
- """Test Connection error handler"""
367
+ """It should Test Connection error handler"""
385
368
bad_mock .side_effect = DatabaseConnectionError ()
386
369
app .config ["FLASK_ENV" ] = "production"
387
370
self .assertRaises (DatabaseConnectionError , routes .init_db , "test" )
0 commit comments