From 61466cf6011759fcdd870af48f6c0af0a374ec6c Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Thu, 15 Oct 2015 22:43:36 -0400 Subject: [PATCH] Coverage for 'gcloud._apitools.exceptions'. --- gcloud/_apitools/test_exceptions.py | 88 +++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 gcloud/_apitools/test_exceptions.py diff --git a/gcloud/_apitools/test_exceptions.py b/gcloud/_apitools/test_exceptions.py new file mode 100644 index 000000000000..643ed6c3a99f --- /dev/null +++ b/gcloud/_apitools/test_exceptions.py @@ -0,0 +1,88 @@ +# pylint: skip-file +import unittest2 + + +class Test_HttpError(unittest2.TestCase): + + def _getTargetClass(self): + from gcloud._apitools.exceptions import HttpError + return HttpError + + def _makeOne(self, *args, **kw): + return self._getTargetClass()(*args, **kw) + + def test_ctor(self): + RESPONSE = {'status': '404'} + CONTENT = b'CONTENT' + URL = 'http://www.example.com' + exception = self._makeOne(RESPONSE, CONTENT, URL) + self.assertEqual(exception.response, RESPONSE) + self.assertEqual(exception.content, CONTENT) + self.assertEqual(exception.url, URL) + self.assertEqual(exception.status_code, 404) + self.assertEqual( + str(exception), + "HttpError accessing : " + "response: <{'status': '404'}>, content ") + + def test_FromResponse(self): + RESPONSE = {'status': '404'} + CONTENT = b'CONTENT' + URL = 'http://www.example.com' + + class _Response(object): + info = RESPONSE + content = CONTENT + request_url = URL + + klass = self._getTargetClass() + exception = klass.FromResponse(_Response()) + self.assertTrue(isinstance(exception, klass)) + self.assertEqual(exception.response, RESPONSE) + self.assertEqual(exception.content, CONTENT) + self.assertEqual(exception.url, URL) + + +class Test_RetryAfterError(unittest2.TestCase): + + def _getTargetClass(self): + from gcloud._apitools.exceptions import RetryAfterError + return RetryAfterError + + def _makeOne(self, *args, **kw): + return self._getTargetClass()(*args, **kw) + + def test_ctor(self): + RESPONSE = {'status': '404'} + CONTENT = b'CONTENT' + URL = 'http://www.example.com' + RETRY_AFTER = 60 + exception = self._makeOne(RESPONSE, CONTENT, URL, RETRY_AFTER) + self.assertEqual(exception.response, RESPONSE) + self.assertEqual(exception.content, CONTENT) + self.assertEqual(exception.url, URL) + self.assertEqual(exception.retry_after, RETRY_AFTER) + self.assertEqual( + str(exception), + "HttpError accessing : " + "response: <{'status': '404'}>, content ") + + def test_FromResponse(self): + RESPONSE = {'status': '404'} + CONTENT = b'CONTENT' + URL = 'http://www.example.com' + RETRY_AFTER = 60 + + class _Response(object): + info = RESPONSE + content = CONTENT + request_url = URL + retry_after = RETRY_AFTER + + klass = self._getTargetClass() + exception = klass.FromResponse(_Response()) + self.assertTrue(isinstance(exception, klass)) + self.assertEqual(exception.response, RESPONSE) + self.assertEqual(exception.content, CONTENT) + self.assertEqual(exception.url, URL) + self.assertEqual(exception.retry_after, RETRY_AFTER)