Prevented error when response doesn't have _items.

This commit is contained in:
2016-07-21 16:34:52 +02:00
parent f47dcd2a31
commit 011bfa9809
2 changed files with 66 additions and 1 deletions

View File

@@ -33,7 +33,7 @@ class Project(List, Find, Create, Post, Update, Delete, Replace):
response = api.get(url)
# Keep the response a dictionary, and cast it later into an object.
if response['_items']:
if response.get('_items'):
item = utils.convert_datetime(response['_items'][0])
return cls(item)
else:

65
tests/test_projects.py Normal file
View File

@@ -0,0 +1,65 @@
import unittest
import responses
import pillarsdk
import pillarsdk.exceptions as sdk_exceptions
mock = responses.RequestsMock(assert_all_requests_are_fired=True)
class ProjectsTests(unittest.TestCase):
def setUp(self):
self.endpoint = 'http://localhost:12345'
self.api = pillarsdk.Api(
endpoint=self.endpoint,
username='',
password='',
token='jemoeder',
)
@mock.activate
def test_find_project_happy(self):
project_id = 24 * 'a'
# Finding the existing project
mock.add(responses.GET,
'%s/projects' % self.endpoint,
json={'_items': [{
'_id': project_id,
'_etag': 'awesome-etag',
'name': 'test-project'}
]})
proj = pillarsdk.Project.find_one({'_id': project_id}, api=self.api)
self.assertEqual(project_id, proj['_id'])
@mock.activate
def test_find_project_unhappy_empty_response(self):
project_id = 24 * 'a'
# Finding the existing project
mock.add(responses.GET,
'%s/projects' % self.endpoint,
json={})
self.assertRaises(sdk_exceptions.ResourceNotFound,
pillarsdk.Project.find_one,
{'_id': project_id}, api=self.api)
@mock.activate
def test_find_project_unhappy_404_response(self):
project_id = 24 * 'a'
# Finding the existing project
mock.add(responses.GET,
'%s/projects' % self.endpoint,
json={'_items': [{
'_id': project_id,
'_etag': 'awesome-etag',
'name': 'test-project'}
]},
status=404)
self.assertRaises(sdk_exceptions.ResourceNotFound,
pillarsdk.Project.find_one,
{'_id': project_id}, api=self.api)