def setup(self): self.path = '%s' self.query = 'query' self.attrs = dict(height=3, width=4, depth=5) self.config = configs.HTTPConfig() self.resource = RESTfulResource(resource_name=None, config=self.config, proxies=None, path=self.path) self.override_header = {} self.extra_header = {}
class TestRESTfulResource(object): def setup(self): self.path = '%s' self.query = 'query' self.attrs = dict(height=3, width=4, depth=5) self.config = configs.HTTPConfig() self.resource = RESTfulResource(resource_name=None, config=self.config, proxies=None, path=self.path) self.override_header = {} self.extra_header = {} def response(self, status_code=200, data=None): r = mock.Mock(requests.Response()) r.status_code = status_code r.content = data return r def test_create(self): resp = dict(id=1) with mock.patch('toothpick.resources.requests', spec=requests) as mock_requests: mock_requests.post.return_value = self.response( status_code = 201, data=resp ) result = self.resource.create(query=None, attributes=self.attrs) assert_equals(result, resp) def test_read(self): with mock.patch('toothpick.resources.requests', spec=requests) as mock_requests: mock_requests.get.return_value = self.response(data=self.attrs) result = self.resource.read(query=self.query) assert_equals(result, self.attrs) def test_update(self): resp = dict(id=1, **self.attrs) with mock.patch('toothpick.resources.requests', spec=requests) as mock_requests: mock_requests.put.return_value = self.response( status_code = 200, data=resp ) result = self.resource.update(query=self.query, attributes=resp) assert_equals(result, resp) def test_delete(self): resp = None with mock.patch('toothpick.resources.requests', spec=requests) as mock_requests: mock_requests.delete.return_value = self.response( status_code = 200, data=resp ) result = self.resource.delete(query=self.query) assert_equals(result, resp) def test_exists(self): with mock.patch('toothpick.resources.requests', spec=requests) as mock_requests: mock_requests.head.return_value = self.response(200) result = self.resource.exists(query=self.query) assert_equals(result, True) mock_requests.head.return_value = self.response(404) result = self.resource.exists(query=self.query) assert_equals(result, False) def test_handle_response(self): response_map = [ # order is important here (range(200,400), type(None)), ([400], exceptions.BadRequest), ([401], exceptions.Unauthorized), ([403], exceptions.Forbidden), ([404], exceptions.NotFound), ([405], exceptions.MethodNotAllowed), ([409], exceptions.Conflict), ([422], exceptions.Invalid), (range(400,500), exceptions.ClientError), (range(500,600), exceptions.ServerError), (range(100,600), exceptions.ResponseError) ] for code in range(100,600): for codes, error_class in response_map: if code in codes: expected_error_class = error_class break try: self.resource.handle_response(self.response(code)) except Exception, e: assert_equals( e.__class__, expected_error_class, msg="Got unexpected exception '%s' for code '%s' (%s)" % ( e.__class__.__name__, code, expected_error_class.__name__ ) ) else: assert_equals( expected_error_class, type(None), msg="Didn't get expected exception '%s' for code '%s'" % ( expected_error_class.__name__, code ) )