Skip to content

Commit 1a78ec9

Browse files
committed
Merge pull request #1190 from tseaver/coverage-gcloud__apitools_util
Coverage for 'gcloud._apitools.util'
2 parents ddb8d20 + 312c413 commit 1a78ec9

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

gcloud/_apitools/test_util.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# pylint: skip-file
2+
import unittest2
3+
4+
5+
class Test_TypeCheck(unittest2.TestCase):
6+
7+
def _callFUT(self, *args, **kw):
8+
from gcloud._apitools.util import Typecheck
9+
return Typecheck(*args, **kw)
10+
11+
def test_pass(self):
12+
self.assertEqual(self._callFUT(123, int), 123)
13+
14+
def test_fail_w_explicit_msg(self):
15+
from gcloud._apitools.exceptions import TypecheckError
16+
with self.assertRaises(TypecheckError) as err:
17+
self._callFUT(123, str, 'foo')
18+
self.assertEqual(err.exception.args, ('foo',))
19+
20+
def test_fail_w_single_type_no_msg(self):
21+
from gcloud._apitools.exceptions import TypecheckError
22+
with self.assertRaises(TypecheckError) as err:
23+
self._callFUT(123, str)
24+
25+
def test_fail_w_tuple_no_msg(self):
26+
from gcloud._apitools.exceptions import TypecheckError
27+
with self.assertRaises(TypecheckError) as err:
28+
self._callFUT(123, (list, tuple))
29+
30+
31+
class Test_CalculateWaitForRetry(unittest2.TestCase):
32+
33+
def _callFUT(self, *args, **kw):
34+
from gcloud._apitools.util import CalculateWaitForRetry
35+
return CalculateWaitForRetry(*args, **kw)
36+
37+
def test_w_negative_jitter_lt_max_wait(self):
38+
import random
39+
from gcloud._testing import _Monkey
40+
with _Monkey(random, uniform=lambda lower, upper: lower):
41+
self.assertEqual(self._callFUT(1, 60), 1.5)
42+
43+
def test_w_positive_jitter_gt_max_wait(self):
44+
import random
45+
from gcloud._testing import _Monkey
46+
with _Monkey(random, uniform=lambda lower, upper: upper):
47+
self.assertEqual(self._callFUT(4, 10), 10)
48+
49+
50+
class Test_AcceptableMimeType(unittest2.TestCase):
51+
52+
def _callFUT(self, *args, **kw):
53+
from gcloud._apitools.util import AcceptableMimeType
54+
return AcceptableMimeType(*args, **kw)
55+
56+
def test_pattern_wo_slash(self):
57+
from gcloud._apitools.exceptions import InvalidUserInputError
58+
with self.assertRaises(InvalidUserInputError) as err:
59+
self._callFUT(['text/*'], 'BOGUS')
60+
self.assertEqual(
61+
err.exception.args,
62+
('Invalid MIME type: "BOGUS"',))
63+
64+
def test_accept_pattern_w_semicolon(self):
65+
from gcloud._apitools.exceptions import GeneratedClientError
66+
with self.assertRaises(GeneratedClientError) as err:
67+
self._callFUT(['text/*;charset=utf-8'], 'text/plain')
68+
self.assertEqual(
69+
err.exception.args,
70+
('MIME patterns with parameter unsupported: '
71+
'"text/*;charset=utf-8"',))
72+
73+
def test_miss(self):
74+
self.assertFalse(self._callFUT(['image/*'], 'text/plain'))
75+
76+
def test_hit(self):
77+
self.assertTrue(self._callFUT(['text/*'], 'text/plain'))

gcloud/_apitools/util.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ def AcceptableMimeType(accept_patterns, mime_type):
7878

7979
def MimeTypeMatches(pattern, mime_type):
8080
"""Return True iff mime_type is acceptable for pattern."""
81-
# Some systems use a single '*' instead of '*/*'.
82-
if pattern == '*':
83-
pattern = '*/*'
8481
return all(accept in ('*', provided) for accept, provided
8582
in zip(pattern.split('/'), mime_type.split('/')))
8683

0 commit comments

Comments
 (0)