def testObjectTypeValidate(self): object_type = mock_api_types.ObjectType() object_type.Define( 'TestType', (mock_api_types.Property(name='key', type=mock_api_types.StringType(None)), mock_api_types.Property( name='value', type=mock_api_types.IntegerType(None))), None) for value in ({}, { 'key': 'test-key' }, { 'value': 17 }, { 'key': 'test-key2', 'value': 19 }): object_type.Validate('method', 'path', value) for value in ((), [], 'key', 17, 3.14, 234985342089L, { 'name': 'invalid-property' }, { 'value': 'invalid-valie' }): self.assertRaises(mock_api_types.ValidationError, object_type.Validate, 'method', 'path', value)
def setUp(self): self._request = mock_api_types.ObjectType() self._request.Define('Request', (mock_api_types.Property( name='request', type=mock_api_types.StringType(None)), ), None) self._response = mock_api_types.ObjectType() self._response.Define('Response', (mock_api_types.Property( name='response', type=mock_api_types.IntegerType(None)), ), None)
def testObjectTypeStr(self): properties = [ mock_api_types.Property('name', mock_api_types.StringType(None)), mock_api_types.Property('value', mock_api_types.IntegerType('int32')) ] object_type = mock_api_types.ObjectType() object_type.Define('Object', properties, None) self.assertEquals('<Object [\'name\', \'value\']>', str(object_type))
def testIntegerTypeValidateStringUInt32(self): integer_type = mock_api_types.IntegerType('uint32') for value in (0, 2147483647, 3, 2**32 - 1): integer_type.ValidateString('method', 'path', str(value)) non_uint32s = (None, 3.14, [], {}, 'non-integer', -2147483648, 2**32, -1) for value in non_uint32s: self.assertRaises(mock_api_types.ValidationError, integer_type.ValidateString, 'method', 'path', str(value))
def testIntegerTypeValidateString(self): integer_type = mock_api_types.IntegerType(None) for value in (-2147483648, 0, 2147483647, 3): integer_type.ValidateString('method', 'path', str(value)) non_integers = (None, 3.14, [], {}, 'non-integer', 2**32, 2**31, -(2**31) - 1) for value in non_integers: self.assertRaises(mock_api_types.ValidationError, integer_type.ValidateString, 'method', 'path', str(value))
def testIntegerTypeValidateInt32(self): integer_type = mock_api_types.IntegerType('int32') for value in (-2147483648, 0, 2147483647, 3): integer_type.Validate('method', 'path', value) integer_type.Validate('method', 'path', int(value)) integer_type.Validate('method', 'path', long(value)) non_int32s = (None, 3.14, [], {}, 'non-integer', 2**32, 2**31, -(2**31) - 1) for value in non_int32s: self.assertRaises(mock_api_types.ValidationError, integer_type.Validate, 'method', 'path', value)
def testValidateParameters(self): method = mock_api_types.Method( 'compute.instances.list', 'list', ('https://www.googleapis.com/compute/v1/' 'projects/{project}/zones/{zone}/instances'), { 'filter': mock_api_types.Parameter( name='filter', type=mock_api_types.StringType(None)), 'maxResults': mock_api_types.Parameter( name='maxResults', type=mock_api_types.IntegerType('int32')), 'project': mock_api_types.Parameter( name='project', type=mock_api_types.StringType('uint64')), 'zone': mock_api_types.Parameter( name='zone', type=mock_api_types.NumberType('double')), 'all': mock_api_types.Parameter(name='all', type=mock_api_types.BooleanType()) }, None, None) parameters = method._ValidateParameters( 'https://www.googleapis.com/compute/v1/' 'projects/1234567890/zones/3.14159265/instances?' 'filter=filter-expression&' 'maxResults=132&' 'all=false') self.assertEquals( { 'filter': 'filter-expression', 'maxResults': '132', 'project': '1234567890', 'zone': '3.14159265', 'all': 'false' }, parameters) # Invalid cases. The base URL will be prepended to all these values below. invalid_suffixes = ( '', '1234567890', '1234567890/zones', '1234567890/zones/3.14159265', 'my-project/zones/3.14159265/instances', '1234567890/zones/my-zone/instances', '1234567890/zones/3.14159265/instances?maxResults=hello', '1234567890/zones/3.14159265/instances?all=yes-please') for suffix in invalid_suffixes: self.assertRaises( mock_api_types.ValidationError, method._ValidateParameters, 'https://www.googleapis.com/compute/v1/projects/' + suffix)
def testObjectTypeValidateAdditional(self): additional = mock_api_types.ObjectType() additional.Define( 'AdditionalType', (mock_api_types.Property(name='key', type=mock_api_types.StringType(None)), mock_api_types.Property( name='value', type=mock_api_types.IntegerType(None))), None) object_type = mock_api_types.ObjectType() object_type.Define('TestAdditional', None, additional) object_type.Validate( 'method', 'path', { 'element-one': { 'key': 'key-one', 'value': 7 }, 'element-two': { 'key': 'key-two', 'value': 11 } })