Skip to content

Commit 6e4cf77

Browse files
committed
Increased test coverage
1 parent ead2d84 commit 6e4cf77

File tree

2 files changed

+42
-59
lines changed

2 files changed

+42
-59
lines changed

service/models.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@
4646
from requests import HTTPError, ConnectionError # pylint: disable=redefined-builtin
4747

4848
# get configuration from environment (12-factor)
49-
ADMIN_PARTY = os.environ.get("ADMIN_PARTY", "False").lower() == "true"
50-
CLOUDANT_HOST = os.environ.get("CLOUDANT_HOST", "localhost")
51-
CLOUDANT_USERNAME = os.environ.get("CLOUDANT_USERNAME", "admin")
52-
CLOUDANT_PASSWORD = os.environ.get("CLOUDANT_PASSWORD", "pass")
49+
ADMIN_PARTY = os.getenv("ADMIN_PARTY", "False").lower() == "true"
50+
CLOUDANT_HOST = os.getenv("CLOUDANT_HOST", "localhost")
51+
CLOUDANT_USERNAME = os.getenv("CLOUDANT_USERNAME", "admin")
52+
CLOUDANT_PASSWORD = os.getenv("CLOUDANT_PASSWORD", "pass")
5353

5454
# global variables for retry (must be int)
55-
RETRY_COUNT = int(os.environ.get("RETRY_COUNT", 10))
56-
RETRY_DELAY = int(os.environ.get("RETRY_DELAY", 1))
57-
RETRY_BACKOFF = int(os.environ.get("RETRY_BACKOFF", 2))
55+
RETRY_COUNT = int(os.getenv("RETRY_COUNT", 10))
56+
RETRY_DELAY = int(os.getenv("RETRY_DELAY", 1))
57+
RETRY_BACKOFF = int(os.getenv("RETRY_BACKOFF", 2))
5858

5959

6060
class DatabaseConnectionError(Exception):

tests/test_routes.py

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,20 @@ class TestPetRoutes(BaseTestCase):
104104
"""Pet Service Routes tests"""
105105

106106
def test_index(self):
107-
"""Test the index page"""
107+
"""It should return the index page"""
108108
resp = self.app.get("/")
109109
self.assertEqual(resp.status_code, status.HTTP_200_OK)
110110
self.assertIn(b"Pet Shop", resp.data)
111111

112112
def test_get_pet_list(self):
113-
"""Get a list of Pets"""
113+
"""It should Get a list of Pets"""
114114
self._create_pets(5)
115115
resp = self.app.get(BASE_URL)
116116
self.assertEqual(resp.status_code, status.HTTP_200_OK)
117117
self.assertTrue(len(resp.data) > 0)
118118

119119
def test_get_pet(self):
120-
"""get a single Pet"""
120+
"""It should Get a single Pet"""
121121
test_pet = self._create_pets()[0]
122122
resp = self.app.get(f"{BASE_URL}/{test_pet.id}", content_type=CONTENT_TYPE_JSON)
123123
self.assertEqual(resp.status_code, status.HTTP_200_OK)
@@ -126,15 +126,15 @@ def test_get_pet(self):
126126
self.assertEqual(data["name"], test_pet.name)
127127

128128
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"""
130130
resp = self.app.get(f"{BASE_URL}/foo")
131131
self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND)
132132
data = resp.get_json()
133133
logging.debug("Response data = %s", data)
134134
self.assertIn("was not found", data["message"])
135135

136136
def test_create_pet(self):
137-
"""Create a new Pet"""
137+
"""It should Create a new Pet"""
138138
test_pet = PetFactory()
139139
logging.debug("Test Pet: %s", test_pet.serialize())
140140
resp = self.app.post(
@@ -168,7 +168,7 @@ def test_create_pet(self):
168168
self.assertEqual(new_pet["birthday"], test_pet.birthday.isoformat())
169169

170170
def test_update_pet(self):
171-
"""Update a Pet"""
171+
"""It should Update a Pet"""
172172
# create a pet to update
173173
test_pet = PetFactory()
174174
resp = self.app.post(
@@ -194,7 +194,7 @@ def test_update_pet(self):
194194
self.assertEqual(updated_pet["category"], "unknown")
195195

196196
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"""
198198
pet = self._create_pets()[0]
199199
pet_data = pet.serialize()
200200
del pet_data["name"]
@@ -207,7 +207,7 @@ def test_update_pet_with_no_name(self):
207207
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
208208

209209
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"""
211211
resp = self.app.put(
212212
f"{BASE_URL}/foo",
213213
json={},
@@ -217,15 +217,15 @@ def test_update_pet_not_found(self):
217217
self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND)
218218

219219
def test_update_pet_not_authorized(self):
220-
"""Update a Pet Not Authorized"""
220+
"""It should not Update a Pet if Not Authorized"""
221221
pet = self._create_pets()[0]
222222
pet_data = pet.serialize()
223223
del pet_data["name"]
224224
resp = self.app.put(f"{BASE_URL}/{pet.id}", json=pet_data)
225225
self.assertEqual(resp.status_code, status.HTTP_401_UNAUTHORIZED)
226226

227227
def test_delete_pet(self):
228-
"""Delete a Pet"""
228+
"""It should Delete a Pet"""
229229
pets = self._create_pets(5)
230230
pet_count = self._get_pet_count()
231231
test_pet = pets[0]
@@ -239,15 +239,15 @@ def test_delete_pet(self):
239239
self.assertEqual(new_count, pet_count - 1)
240240

241241
def test_delete_all_pet(self):
242-
"""Delete All Pets under test only"""
242+
"""It should Delete All Pets under test only"""
243243
self._create_pets(1)
244244
app.config["TESTING"] = False
245245
resp = self.app.delete(BASE_URL, headers=self.headers)
246246
self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
247247
app.config["TESTING"] = True
248248

249249
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"""
251251
pet = self._create_pets()[0]
252252
new_pet = pet.serialize()
253253
del new_pet["name"]
@@ -258,19 +258,19 @@ def test_create_pet_with_no_name(self):
258258
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
259259

260260
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"""
262262
resp = self.app.post(BASE_URL, data="bad data", headers=self.headers)
263263
self.assertEqual(resp.status_code, status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
264264

265265
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"""
267267
resp = self.app.post(
268268
BASE_URL, data={}, content_type="plain/text", headers=self.headers
269269
)
270270
self.assertEqual(resp.status_code, status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
271271

272272
def test_call_create_with_an_id(self):
273-
"""Call create passing an id"""
273+
"""It should not create passing an id"""
274274
resp = self.app.post(f"{BASE_URL}/foo", json={}, headers=self.headers)
275275
self.assertEqual(resp.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
276276

@@ -279,7 +279,7 @@ class TestPetQuery(BaseTestCase):
279279
"""Pet Service Query tests"""
280280

281281
def test_query_by_name(self):
282-
"""Query Pets by name"""
282+
"""It should Query Pets by name"""
283283
pets = self._create_pets(5)
284284
test_name = pets[0].name
285285
name_count = len([pet for pet in pets if pet.name == test_name])
@@ -292,7 +292,7 @@ def test_query_by_name(self):
292292
self.assertEqual(pet["name"], test_name)
293293

294294
def test_query_by_category(self):
295-
"""Query Pets by category"""
295+
"""It should Query Pets by category"""
296296
pets = self._create_pets(5)
297297
test_category = pets[0].category
298298
category_count = len([pet for pet in pets if pet.category == test_category])
@@ -309,44 +309,27 @@ def test_query_by_category(self):
309309
# test_query_by_availability() does not work because of the way CouchDB
310310
# handles deletions. Need to upgrade to newer ibmcloudant library
311311

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)
343326

344327

345328
class TestPetActions(BaseTestCase):
346329
"""Pet Service Action tests"""
347330

348331
def test_purchase_a_pet(self):
349-
"""Purchase a Pet"""
332+
"""It should Purchase a Pet"""
350333
pets = self._create_pets(10)
351334
available_pets = [pet for pet in pets if pet.available is True]
352335
pet = available_pets[0]
@@ -361,7 +344,7 @@ def test_purchase_a_pet(self):
361344
self.assertEqual(data["available"], False)
362345

363346
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"""
365348
pets = self._create_pets(10)
366349
unavailable_pets = [pet for pet in pets if pet.available is False]
367350
pet = unavailable_pets[0]
@@ -371,7 +354,7 @@ def test_purchase_not_available(self):
371354
self.assertEqual(resp.status_code, status.HTTP_409_CONFLICT)
372355

373356
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"""
375358
resp = self.app.put(f"{BASE_URL}/0/purchase", content_type=CONTENT_TYPE_JSON)
376359
self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND)
377360

@@ -381,7 +364,7 @@ def test_purchase_does_not_exist(self):
381364

382365
@patch("cloudant.client.Cloudant.__init__")
383366
def test_connection_error(self, bad_mock):
384-
"""Test Connection error handler"""
367+
"""It should Test Connection error handler"""
385368
bad_mock.side_effect = DatabaseConnectionError()
386369
app.config["FLASK_ENV"] = "production"
387370
self.assertRaises(DatabaseConnectionError, routes.init_db, "test")

0 commit comments

Comments
 (0)