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 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 testStringTypeValidateByte(self): string_type = mock_api_types.StringType('byte') value = base64.urlsafe_b64encode('encoded string') string_type.Validate('method', 'path', value) non_bytes = (None, 3, 3.14, [], {}, 'non-base64') for value in non_bytes: self.assertRaises(mock_api_types.ValidationError, string_type.Validate, 'method', 'path', value)
def testStringTypeValidate(self): string_type = mock_api_types.StringType(None) string_type.Validate('method', 'path', 'string') string_type.Validate('method', 'path', u'unicode') non_strings = (None, 3, 3.14, [], {}) for value in non_strings: self.assertRaises(mock_api_types.ValidationError, string_type.Validate, 'method', 'path', value)
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 testStringTypeValidateUInt64(self): string_type = mock_api_types.StringType('uint64') values = ('0', '253489657232345', str(2**63), str(2**64 - 1)) for value in values: string_type.Validate('method', 'path', value) non_int64s = (None, 3, 3.14, [], {}, 'non-uint64', str(2**64), '-1') for value in non_int64s: self.assertRaises(mock_api_types.ValidationError, string_type.Validate, 'method', 'path', value)
def testStringTypeValidateDate(self): string_type = mock_api_types.StringType('date') value = '2013-05-18' string_type.Validate('method', 'path', value) non_dates = (None, 3, 3.14, [], {}, 'non-date', '2013', '2013-05', '2013-05-18T', '2013-05-18T14', '2013-05-18T14:42', '2013-05-18T14:42:37', '2013-05-18T14:42:37.', '2013-05-18T14:42:37.425', '2013-05-18T14:42:37.425Z') for value in non_dates: self.assertRaises(mock_api_types.ValidationError, string_type.Validate, 'method', 'path', value)
def testArrayTypeValidate(self): # Array of string array_type = mock_api_types.ArrayType(mock_api_types.StringType(None)) for value in ((), [], ('string-value', ), ['string-value'], ('string1', 'string2'), ['string1', 'string2']): array_type.Validate('method', 'path', value) for value in ((123, ), [()], {}, ({}, ), ['string', 7], ('string', 3.14), ['string', True]): self.assertRaises(mock_api_types.ValidationError, array_type.Validate, 'method', 'path', value) try: array_type.Validate('method', 'path', ['good', 'good', 7, 'good']) self.fail('Validation should have failed.') except mock_api_types.ValidationError, e: self.assertEquals( 'method path[2]: expected string, but received 7', e.message)
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 } })
def testStringTypeValidateString(self): string_type = mock_api_types.StringType(None) string_type.ValidateString('method', 'path', 'string') string_type.ValidateString('method', 'path', u'unicode')