def testSetTypeHint(self): """Test creating safe class names from object names.""" test_schema = api.Schema(self.api, 'testSchema', {}) type_to_hint = [ ({ '$ref': 'Activity' }, 'Google_Service_Test_Activity'), ({ 'type': 'boolean' }, ''), ({ 'type': 'integer' }, ''), ({ 'type': 'string' }, ''), # PHP doesn't support primitive type hints. ({ 'type': 'StRing' }, ''), # PHP var names are case-insensitive. ({ '$ref': 'Photo' }, 'Google_Service_Test_Photo'), ({ 'type': 'array', 'items': { 'type': 'string' } }, ''), ({ 'type': 'object', 'properties': { 'p1': { 'type': 'string' } } }, 'Google_Service_Test_TestSchemaTest'), ] for type_dict, expected_hint in type_to_hint: test_property = schema.Property(self.api, test_schema, 'test', type_dict) test_property.SetLanguageModel(self.language_model) self.generator._SetTypeHint(test_property) self.assertEquals(expected_hint, test_property.values['typeHint'])
def testDataValue(self): foo_def_dict = { 'className': 'Foo', 'type': 'string', } prototype = data_types.DataType(foo_def_dict, None, language_model=self.language_model) dv = data_value.DataValue(3, prototype) # Basic Checks self.assertEqual(3, dv.value) self.assertEqual(prototype, dv.data_type) self.assertEqual({}, dv.metadata) self.assertEqual('Foo', dv.code_type) dv.metadata['foo'] = 'bar' self.assertEqual({'foo': 'bar'}, dv.metadata) dv.SetValue('four') self.assertEqual(dv.value, 'four') self.assertEqual(self.language_model, dv.GetLanguageModel()) other_language_model = language_model.LanguageModel( class_name_delimiter='+') dv.SetLanguageModel(other_language_model) self.assertEqual(other_language_model, dv.GetLanguageModel()) # Now that we've set a local language model... make sure the codepath # for setting the data_type's language model gets exercised. self.assertEqual('Foo', dv.code_type) # Check that the constructor doesn't freak if an odd object is passed in dv = data_value.DataValue(object, prototype) self.assertEqual(dv.value, object) # A standard case is the prototype being a Property object. It is not # uncommon that the Property's data_type is a SchemaReference. To verify # this case is handled correctly we must fake an API. bar_def_dict = { 'className': 'Foo', 'type': 'string', } class MockApi(object): def __init__(self): self.model_module = None def SetSchema(self, s): self.schema = s def SetSchemaRef(self, schema_ref): self.schema_ref = schema_ref # pylint: disable=unused-argument def ToClassName(self, name, element, element_type): return name # pylint: disable=unused-argument def SchemaByName(self, schema_name): return self.schema # pylint: disable=unused-argument def DataTypeFromJson(self, unused_def_dict, tentative_class_name, parent=None, wire_name=None): return self.schema_ref def NestedClassNameForProperty(self, name, owning_schema): return '%s%s' % (owning_schema.class_name, name) mock_api = MockApi() bar_schema = schema.Schema(mock_api, 'Bar', bar_def_dict) mock_api.SetSchema(bar_schema) schema_ref = data_types.SchemaReference('Bar', mock_api) mock_api.SetSchemaRef(schema_ref) prototype = schema.Property(mock_api, schema_ref, 'baz', foo_def_dict) dv = data_value.DataValue('3', prototype) # Assure all the unwrapping gymnastics in the DataValue constructor did # their job correctly. self.assertEqual(mock_api.schema, dv.data_type)