|
| 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')) |
0 commit comments