Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Unified Diff: third_party/google-endpoints/apitools/base/py/util_test.py

Issue 2666783008: Add google-endpoints to third_party/. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/google-endpoints/apitools/base/py/util_test.py
diff --git a/third_party/google-endpoints/apitools/base/py/util_test.py b/third_party/google-endpoints/apitools/base/py/util_test.py
new file mode 100644
index 0000000000000000000000000000000000000000..a06d1a9b94044ee2f3329563c7f2e6d1eafdebaa
--- /dev/null
+++ b/third_party/google-endpoints/apitools/base/py/util_test.py
@@ -0,0 +1,198 @@
+#
+# Copyright 2015 Google Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Tests for util.py."""
+import unittest2
+
+from apitools.base.protorpclite import messages
+from apitools.base.py import encoding
+from apitools.base.py import exceptions
+from apitools.base.py import util
+
+
+class MockedMethodConfig(object):
+
+ def __init__(self, relative_path, path_params):
+ self.relative_path = relative_path
+ self.path_params = path_params
+
+
+class MessageWithRemappings(messages.Message):
+
+ class AnEnum(messages.Enum):
+ value_one = 1
+ value_two = 2
+
+ str_field = messages.StringField(1)
+ enum_field = messages.EnumField('AnEnum', 2)
+
+
+encoding.AddCustomJsonFieldMapping(
+ MessageWithRemappings, 'str_field', 'path_field')
+encoding.AddCustomJsonEnumMapping(
+ MessageWithRemappings.AnEnum, 'value_one', 'ONE')
+
+
+class UtilTest(unittest2.TestCase):
+
+ def testExpand(self):
+ method_config_xy = MockedMethodConfig(relative_path='{x}/y/{z}',
+ path_params=['x', 'z'])
+ self.assertEquals(
+ util.ExpandRelativePath(method_config_xy, {'x': '1', 'z': '2'}),
+ '1/y/2')
+ self.assertEquals(
+ util.ExpandRelativePath(
+ method_config_xy,
+ {'x': '1', 'z': '2'},
+ relative_path='{x}/y/{z}/q'),
+ '1/y/2/q')
+
+ def testReservedExpansion(self):
+ method_config_reserved = MockedMethodConfig(relative_path='{+x}/baz',
+ path_params=['x'])
+ self.assertEquals('foo/:bar:/baz', util.ExpandRelativePath(
+ method_config_reserved, {'x': 'foo/:bar:'}))
+ method_config_no_reserved = MockedMethodConfig(relative_path='{x}/baz',
+ path_params=['x'])
+ self.assertEquals('foo%2F%3Abar%3A/baz', util.ExpandRelativePath(
+ method_config_no_reserved, {'x': 'foo/:bar:'}))
+
+ def testCalculateWaitForRetry(self):
+ try0 = util.CalculateWaitForRetry(0)
+ self.assertTrue(try0 >= 1.0)
+ self.assertTrue(try0 <= 1.5)
+ try1 = util.CalculateWaitForRetry(1)
+ self.assertTrue(try1 >= 1.0)
+ self.assertTrue(try1 <= 3.0)
+ try2 = util.CalculateWaitForRetry(2)
+ self.assertTrue(try2 >= 2.0)
+ self.assertTrue(try2 <= 6.0)
+ try3 = util.CalculateWaitForRetry(3)
+ self.assertTrue(try3 >= 4.0)
+ self.assertTrue(try3 <= 12.0)
+ try4 = util.CalculateWaitForRetry(4)
+ self.assertTrue(try4 >= 8.0)
+ self.assertTrue(try4 <= 24.0)
+
+ self.assertAlmostEqual(10, util.CalculateWaitForRetry(5, max_wait=10))
+
+ def testTypecheck(self):
+
+ class Class1(object):
+ pass
+
+ class Class2(object):
+ pass
+
+ class Class3(object):
+ pass
+
+ instance_of_class1 = Class1()
+
+ self.assertEquals(
+ instance_of_class1, util.Typecheck(instance_of_class1, Class1))
+
+ self.assertEquals(
+ instance_of_class1,
+ util.Typecheck(instance_of_class1, ((Class1, Class2), Class3)))
+
+ self.assertEquals(
+ instance_of_class1,
+ util.Typecheck(instance_of_class1, (Class1, (Class2, Class3))))
+
+ self.assertEquals(
+ instance_of_class1,
+ util.Typecheck(instance_of_class1, Class1, 'message'))
+
+ self.assertEquals(
+ instance_of_class1,
+ util.Typecheck(
+ instance_of_class1, ((Class1, Class2), Class3), 'message'))
+
+ self.assertEquals(
+ instance_of_class1,
+ util.Typecheck(
+ instance_of_class1, (Class1, (Class2, Class3)), 'message'))
+
+ with self.assertRaises(exceptions.TypecheckError):
+ util.Typecheck(instance_of_class1, Class2)
+
+ with self.assertRaises(exceptions.TypecheckError):
+ util.Typecheck(instance_of_class1, (Class2, Class3))
+
+ with self.assertRaises(exceptions.TypecheckError):
+ util.Typecheck(instance_of_class1, Class2, 'message')
+
+ with self.assertRaises(exceptions.TypecheckError):
+ util.Typecheck(instance_of_class1, (Class2, Class3), 'message')
+
+ def testAcceptableMimeType(self):
+ valid_pairs = (
+ ('*', 'text/plain'),
+ ('*/*', 'text/plain'),
+ ('text/*', 'text/plain'),
+ ('*/plain', 'text/plain'),
+ ('text/plain', 'text/plain'),
+ )
+
+ for accept, mime_type in valid_pairs:
+ self.assertTrue(util.AcceptableMimeType([accept], mime_type))
+
+ invalid_pairs = (
+ ('text/*', 'application/json'),
+ ('text/plain', 'application/json'),
+ )
+
+ for accept, mime_type in invalid_pairs:
+ self.assertFalse(util.AcceptableMimeType([accept], mime_type))
+
+ self.assertTrue(util.AcceptableMimeType(['application/json', '*/*'],
+ 'text/plain'))
+ self.assertFalse(util.AcceptableMimeType(['application/json', 'img/*'],
+ 'text/plain'))
+
+ def testMalformedMimeType(self):
+ self.assertRaises(
+ exceptions.InvalidUserInputError,
+ util.AcceptableMimeType, ['*/*'], 'abcd')
+
+ def testUnsupportedMimeType(self):
+ self.assertRaises(
+ exceptions.GeneratedClientError,
+ util.AcceptableMimeType, ['text/html;q=0.9'], 'text/html')
+
+ def testMapRequestParams(self):
+ params = {
+ 'str_field': 'foo',
+ 'enum_field': MessageWithRemappings.AnEnum.value_one,
+ }
+ remapped_params = {
+ 'path_field': 'foo',
+ 'enum_field': 'ONE',
+ }
+ self.assertEqual(remapped_params,
+ util.MapRequestParams(params, MessageWithRemappings))
+
+ params['enum_field'] = MessageWithRemappings.AnEnum.value_two
+ remapped_params['enum_field'] = 'value_two'
+ self.assertEqual(remapped_params,
+ util.MapRequestParams(params, MessageWithRemappings))
+
+ def testMapParamNames(self):
+ params = ['path_field', 'enum_field']
+ remapped_params = ['str_field', 'enum_field']
+ self.assertEqual(remapped_params,
+ util.MapParamNames(params, MessageWithRemappings))
« no previous file with comments | « third_party/google-endpoints/apitools/base/py/util.py ('k') | third_party/google-endpoints/apitools/data/__init__.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698