Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage for 'gcloud._apitools.util' #1190

Merged
merged 1 commit into from
Oct 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions gcloud/_apitools/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# pylint: skip-file
import unittest2


class Test_TypeCheck(unittest2.TestCase):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


def _callFUT(self, *args, **kw):
from gcloud._apitools.util import Typecheck
return Typecheck(*args, **kw)

def test_pass(self):
self.assertEqual(self._callFUT(123, int), 123)

def test_fail_w_explicit_msg(self):
from gcloud._apitools.exceptions import TypecheckError
with self.assertRaises(TypecheckError) as err:
self._callFUT(123, str, 'foo')
self.assertEqual(err.exception.args, ('foo',))

def test_fail_w_single_type_no_msg(self):
from gcloud._apitools.exceptions import TypecheckError
with self.assertRaises(TypecheckError) as err:
self._callFUT(123, str)

def test_fail_w_tuple_no_msg(self):
from gcloud._apitools.exceptions import TypecheckError
with self.assertRaises(TypecheckError) as err:
self._callFUT(123, (list, tuple))


class Test_CalculateWaitForRetry(unittest2.TestCase):

def _callFUT(self, *args, **kw):
from gcloud._apitools.util import CalculateWaitForRetry
return CalculateWaitForRetry(*args, **kw)

def test_w_negative_jitter_lt_max_wait(self):
import random
from gcloud._testing import _Monkey
with _Monkey(random, uniform=lambda lower, upper: lower):
self.assertEqual(self._callFUT(1, 60), 1.5)

def test_w_positive_jitter_gt_max_wait(self):
import random
from gcloud._testing import _Monkey
with _Monkey(random, uniform=lambda lower, upper: upper):
self.assertEqual(self._callFUT(4, 10), 10)


class Test_AcceptableMimeType(unittest2.TestCase):

def _callFUT(self, *args, **kw):
from gcloud._apitools.util import AcceptableMimeType
return AcceptableMimeType(*args, **kw)

def test_pattern_wo_slash(self):
from gcloud._apitools.exceptions import InvalidUserInputError
with self.assertRaises(InvalidUserInputError) as err:
self._callFUT(['text/*'], 'BOGUS')
self.assertEqual(
err.exception.args,
('Invalid MIME type: "BOGUS"',))

def test_accept_pattern_w_semicolon(self):
from gcloud._apitools.exceptions import GeneratedClientError
with self.assertRaises(GeneratedClientError) as err:
self._callFUT(['text/*;charset=utf-8'], 'text/plain')
self.assertEqual(
err.exception.args,
('MIME patterns with parameter unsupported: '
'"text/*;charset=utf-8"',))

def test_miss(self):
self.assertFalse(self._callFUT(['image/*'], 'text/plain'))

def test_hit(self):
self.assertTrue(self._callFUT(['text/*'], 'text/plain'))
3 changes: 0 additions & 3 deletions gcloud/_apitools/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ def AcceptableMimeType(accept_patterns, mime_type):

def MimeTypeMatches(pattern, mime_type):
"""Return True iff mime_type is acceptable for pattern."""
# Some systems use a single '*' instead of '*/*'.
if pattern == '*':
pattern = '*/*'
return all(accept in ('*', provided) for accept, provided

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

in zip(pattern.split('/'), mime_type.split('/')))

Expand Down