def test_strip_alias(self): first_alias = Alias(None, None, None) second_alias = Alias(None, None, None) third_alias = Alias(None, None, None) first_alias.data_type = second_alias second_alias.data_type = third_alias third_alias.data_type = String() test_struct = Struct('TestStruct', None, None) test_struct.set_attributes( None, [StructField('field1', List(Nullable(first_alias)), None, None)]) curr_type = first_alias while hasattr(curr_type, 'data_type'): curr_type.data_type = resolve_aliases(curr_type.data_type) curr_type = curr_type.data_type self.assertIsInstance(first_alias.data_type, String) self.assertEqual(len(test_struct.fields), 1) field = test_struct.fields[0] strip_alias(field.data_type) list_type = field.data_type self.assertIsInstance(list_type, List) nullable_type = list_type.data_type self.assertIsInstance(nullable_type, Nullable) string_type = nullable_type.data_type self.assertIsInstance(string_type, String)
def _make_namespace_with_alias(): # type: (...) -> ApiNamespace ns = ApiNamespace('ns_with_alias') struct1 = Struct(name='Struct1', namespace=ns, ast_node=None) struct1.set_attributes(None, [StructField('f1', Boolean(), None, None)]) ns.add_data_type(struct1) alias = Alias(name='AliasToStruct1', namespace=ns, ast_node=None) alias.set_attributes(doc=None, data_type=struct1) ns.add_alias(alias) return ns
def test_resolve_aliases(self): first_alias = Alias(None, None, None) first_alias.data_type = String() resolved_type = resolve_aliases(first_alias.data_type) # Test that single-level alias chain resolves self.assertIsInstance(resolved_type, String) self.assertIsInstance(first_alias.data_type, String) first_alias = Alias(None, None, None) second_alias = Alias(None, None, None) first_alias.data_type = second_alias second_alias.data_type = String() # Test that a two-level alias chain resolves resolved_type = resolve_aliases(first_alias.data_type) first_alias.data_type = resolved_type self.assertIsInstance(resolved_type, String) self.assertIsInstance(first_alias.data_type, String) self.assertIsInstance(second_alias.data_type, String)
def test_strip_alias(self): first_alias = Alias(None, None, None) second_alias = Alias(None, None, None) third_alias = Alias(None, None, None) first_alias.data_type = second_alias second_alias.data_type = third_alias third_alias.data_type = String() test_struct = Struct('TestStruct', None, None) test_struct.set_attributes(None, [ StructField('field1', List(Nullable(first_alias)), None, None) ]) curr_type = first_alias while hasattr(curr_type, 'data_type'): curr_type.data_type = resolve_aliases(curr_type.data_type) curr_type = curr_type.data_type self.assertIsInstance(first_alias.data_type, String) self.assertEqual(len(test_struct.fields), 1) field = test_struct.fields[0] strip_alias(field.data_type) list_type = field.data_type self.assertIsInstance(list_type, List) nullable_type = list_type.data_type self.assertIsInstance(nullable_type, Nullable) string_type = nullable_type.data_type self.assertIsInstance(string_type, String)
def test_no_preserve_aliases_from_api(self): api = Api(version=None) api.ensure_namespace('preserve_alias') ns = api.namespaces['preserve_alias'] namespace_id = Alias('NamespaceId', ns, None) namespace_id.data_type = String() shared_folder_id = Alias('SharedFolderId', ns, None) shared_folder_id.set_attributes(None, namespace_id) path_root_id = Alias('PathRootId', ns, None) path_root_id.set_attributes(None, shared_folder_id) ns.add_alias(namespace_id) ns.add_alias(shared_folder_id) ns.add_alias(path_root_id) test_struct = Struct('TestStruct', ns, None) test_struct.set_attributes( None, [StructField('field1', path_root_id, None, None)]) test_union = Union('TestUnion', ns, None, None) test_union.set_attributes( None, [UnionField('test', path_root_id, None, None)]) ns.add_data_type(test_struct) ns.add_data_type(test_union) struct_alias = Alias('StructAlias', ns, None) struct_alias.set_attributes(None, test_struct) ns.add_alias(struct_alias) api = remove_aliases_from_api(api) # Ensure namespace exists self.assertEqual(len(api.namespaces), 1) self.assertTrue('preserve_alias' in api.namespaces) ns = api.namespaces['preserve_alias'] # Ensure aliases are gone self.assertEqual(len(ns.aliases), 0) data_types = { data_type._name: data_type for data_type in ns.data_types } # Ensure struct and union field aliases resolve to proper types test_struct = data_types.get('TestStruct') self.assertIsInstance(test_struct, Struct) self.assertTrue(len(test_struct.fields), 1) field = test_struct.fields[0] self.assertEqual(field.name, 'field1') self.assertIsInstance(field.data_type, String) test_union = data_types['TestUnion'] self.assertTrue(len(test_union.fields), 1) field = test_union.fields[0] self.assertEqual(field.name, 'test') self.assertIsInstance(field.data_type, String)
def test_no_preserve_aliases_from_api(self): api = Api(version=None) # Ensure imports come after 'preserve_alias' lexiographicaly # to catch namespace ordering bugs api.ensure_namespace('preserve_alias') api.ensure_namespace('zzzz') ns = api.namespaces['preserve_alias'] imported = api.namespaces['zzzz'] # Setup aliases namespace_id = Alias('NamespaceId', ns, None) namespace_id.data_type = String() shared_folder_id = Alias('SharedFolderId', ns, None) shared_folder_id.set_attributes(None, namespace_id) path_root_id = Alias('PathRootId', ns, None) path_root_id.set_attributes(None, shared_folder_id) nullable_alias = Alias('NullableAlias', ns, None) nullable_alias.set_attributes(None, Nullable(path_root_id)) foo_alias = Alias('FooAlias', None, None) foo_alias.set_attributes(None, String()) bar_alias = Alias('BarAlias', None, None) bar_alias.set_attributes(None, foo_alias) ns.add_alias(namespace_id) ns.add_alias(shared_folder_id) ns.add_alias(path_root_id) ns.add_alias(nullable_alias) imported.add_alias(foo_alias) imported.add_alias(bar_alias) # Setup composite types test_struct = Struct('TestStruct', ns, None) test_struct.set_attributes(None, [ StructField('field_alias', path_root_id, None, None), StructField('field_nullable_alias', nullable_alias, None, None), StructField('field_list_of_alias', List(path_root_id), None, None) ]) test_union = Union('TestUnion', ns, None, None) test_union.set_attributes(None, [UnionField('test', path_root_id, None, None)]) dependent_struct = Struct('DependentStruct', ns, None) dependent_struct.set_attributes(None, [ StructField('field_alias', imported.alias_by_name['BarAlias'], None, None) ]) ns.add_data_type(test_struct) ns.add_data_type(test_union) ns.add_data_type(dependent_struct) # Setup aliases on composite types struct_alias = Alias('StructAlias', ns, None) struct_alias.set_attributes(None, test_struct) ns.add_alias(struct_alias) api = remove_aliases_from_api(api) # Ensure namespace exists self.assertEqual(len(api.namespaces), 2) self.assertTrue('preserve_alias' in api.namespaces) self.assertTrue('zzzz' in api.namespaces) ns = api.namespaces['preserve_alias'] imported = api.namespaces['zzzz'] # Ensure aliases are gone self.assertEqual(len(ns.aliases), 0) self.assertEqual(len(imported.aliases), 0) data_types = { data_type._name: data_type for data_type in ns.data_types } # Ensure struct and union field aliases resolve to proper types test_struct = data_types.get('TestStruct') self.assertIsInstance(test_struct, Struct) self.assertEqual(len(test_struct.fields), 3) for field in test_struct.fields: if field.name == 'field_list_of_alias': self.assertIsInstance(field.data_type, List) list_type = field.data_type.data_type self.assertIsInstance(list_type, String) elif field.name == 'field_nullable_alias': field_type = field.data_type self.assertIsInstance(field_type, Nullable) self.assertIsInstance(field_type.data_type, String) else: self.assertIsInstance(field.data_type, String) test_union = data_types['TestUnion'] self.assertTrue(len(test_union.fields), 1) field = test_union.fields[0] self.assertEqual(field.name, 'test') self.assertIsInstance(field.data_type, String) # Ensure struct using imported alias resolves properly dependent_struct = data_types.get('DependentStruct') self.assertIsInstance(dependent_struct, Struct) self.assertEqual(len(dependent_struct.fields), 1) field = dependent_struct.fields[0] self.assertIsInstance(field.data_type, String)
def test_preserve_aliases_from_api(self): api = Api(version=None) # Ensure imports come after 'preserve_alias' lexiographicaly # to catch namespace ordering bugs api.ensure_namespace('preserve_alias') api.ensure_namespace('zzzz') ns = api.namespaces['preserve_alias'] imported = api.namespaces['zzzz'] namespace_id = Alias('NamespaceId', ns, None) namespace_id.data_type = String() shared_folder_id = Alias('SharedFolderId', ns, None) shared_folder_id.set_attributes(None, namespace_id) path_root_id = Alias('PathRootId', ns, None) path_root_id.set_attributes(None, shared_folder_id) foo_alias = Alias('FooAlias', None, None) foo_alias.set_attributes(None, String()) bar_alias = Alias('BarAlias', None, None) bar_alias.set_attributes(None, foo_alias) ns.add_alias(namespace_id) ns.add_alias(shared_folder_id) ns.add_alias(path_root_id) imported.add_alias(foo_alias) imported.add_alias(bar_alias) test_struct = Struct('TestStruct', ns, None) test_struct.set_attributes(None, [StructField('field1', path_root_id, None, None)]) test_union = Union('TestUnion', ns, None, None) test_union.set_attributes(None, [UnionField('test', path_root_id, None, None)]) dependent_struct = Struct('DependentStruct', ns, None) dependent_struct.set_attributes(None, [ StructField('field_alias', imported.alias_by_name['BarAlias'], None, None) ]) ns.add_data_type(test_struct) ns.add_data_type(test_union) ns.add_data_type(dependent_struct) struct_alias = Alias('StructAlias', ns, None) struct_alias.set_attributes(None, test_struct) ns.add_alias(struct_alias) # Ensure namespace exists self.assertEqual(len(api.namespaces), 2) self.assertTrue('preserve_alias' in api.namespaces) self.assertTrue('zzzz' in api.namespaces) ns = api.namespaces['preserve_alias'] imported = api.namespaces['zzzz'] # Ensure aliases exist self.assertEqual(len(ns.aliases), 4) self.assertEqual(len(imported.aliases), 2) aliases = { alias._name: alias for alias in ns.aliases } imported_aliases = { alias.name: alias for alias in imported.aliases } data_types = { data_type._name: data_type for data_type in ns.data_types } # Ensure aliases are in the namespace self.assertTrue('NamespaceId' in aliases) self.assertTrue('SharedFolderId' in aliases) self.assertTrue('PathRootId' in aliases) self.assertTrue('StructAlias' in aliases) self.assertTrue('FooAlias' in imported_aliases) self.assertTrue('BarAlias' in imported_aliases) # Ensure aliases resolve to proper types self.assertIsInstance(aliases['NamespaceId'].data_type, String) self.assertIsInstance(aliases['SharedFolderId'].data_type, Alias) self.assertIsInstance(aliases['PathRootId'].data_type, Alias) self.assertIsInstance(aliases['StructAlias'].data_type, Struct) self.assertIsInstance(imported_aliases['FooAlias'].data_type, String) self.assertIsInstance(imported_aliases['BarAlias'].data_type, Alias) # Ensure struct and union field aliases resolve to proper types self.assertIsInstance(data_types['TestStruct'], Struct) test_struct = data_types.get('TestStruct') dependent_struct = data_types.get('DependentStruct') self.assertTrue(len(test_struct.fields), 1) self.assertTrue(len(dependent_struct.fields), 1) field = test_struct.fields[0] self.assertEqual(field.name, 'field1') self.assertIsInstance(field.data_type, Alias) field = dependent_struct.fields[0] self.assertEqual(field.name, 'field_alias') self.assertIsInstance(field.data_type, Alias) test_union = data_types['TestUnion'] self.assertTrue(len(test_union.fields), 1) field = test_union.fields[0] self.assertEqual(field.name, 'test') self.assertIsInstance(field.data_type, Alias)
def test_no_preserve_aliases_from_api(self): api = Api(version=None) # Ensure imports come after 'preserve_alias' lexiographicaly # to catch namespace ordering bugs api.ensure_namespace('preserve_alias') api.ensure_namespace('zzzz') ns = api.namespaces['preserve_alias'] imported = api.namespaces['zzzz'] # Setup aliases namespace_id = Alias('NamespaceId', ns, None) namespace_id.data_type = String() shared_folder_id = Alias('SharedFolderId', ns, None) shared_folder_id.set_attributes(None, namespace_id) path_root_id = Alias('PathRootId', ns, None) path_root_id.set_attributes(None, shared_folder_id) nullable_alias = Alias('NullableAlias', ns, None) nullable_alias.set_attributes(None, Nullable(path_root_id)) foo_alias = Alias('FooAlias', None, None) foo_alias.set_attributes(None, String()) bar_alias = Alias('BarAlias', None, None) bar_alias.set_attributes(None, foo_alias) ns.add_alias(namespace_id) ns.add_alias(shared_folder_id) ns.add_alias(path_root_id) ns.add_alias(nullable_alias) imported.add_alias(foo_alias) imported.add_alias(bar_alias) # Setup composite types test_struct = Struct('TestStruct', ns, None) test_struct.set_attributes(None, [ StructField('field_alias', path_root_id, None, None), StructField('field_nullable_alias', nullable_alias, None, None), StructField('field_list_of_alias', List(path_root_id), None, None) ]) test_union = Union('TestUnion', ns, None, None) test_union.set_attributes( None, [UnionField('test', path_root_id, None, None)]) dependent_struct = Struct('DependentStruct', ns, None) dependent_struct.set_attributes(None, [ StructField('field_alias', imported.alias_by_name['BarAlias'], None, None) ]) ns.add_data_type(test_struct) ns.add_data_type(test_union) ns.add_data_type(dependent_struct) # Setup aliases on composite types struct_alias = Alias('StructAlias', ns, None) struct_alias.set_attributes(None, test_struct) ns.add_alias(struct_alias) api = remove_aliases_from_api(api) # Ensure namespace exists self.assertEqual(len(api.namespaces), 2) self.assertTrue('preserve_alias' in api.namespaces) self.assertTrue('zzzz' in api.namespaces) ns = api.namespaces['preserve_alias'] imported = api.namespaces['zzzz'] # Ensure aliases are gone self.assertEqual(len(ns.aliases), 0) self.assertEqual(len(imported.aliases), 0) data_types = { data_type._name: data_type for data_type in ns.data_types } # Ensure struct and union field aliases resolve to proper types test_struct = data_types.get('TestStruct') self.assertIsInstance(test_struct, Struct) self.assertEqual(len(test_struct.fields), 3) for field in test_struct.fields: if field.name == 'field_list_of_alias': self.assertIsInstance(field.data_type, List) list_type = field.data_type.data_type self.assertIsInstance(list_type, String) elif field.name == 'field_nullable_alias': field_type = field.data_type self.assertIsInstance(field_type, Nullable) self.assertIsInstance(field_type.data_type, String) else: self.assertIsInstance(field.data_type, String) test_union = data_types['TestUnion'] self.assertTrue(len(test_union.fields), 1) field = test_union.fields[0] self.assertEqual(field.name, 'test') self.assertIsInstance(field.data_type, String) # Ensure struct using imported alias resolves properly dependent_struct = data_types.get('DependentStruct') self.assertIsInstance(dependent_struct, Struct) self.assertEqual(len(dependent_struct.fields), 1) field = dependent_struct.fields[0] self.assertIsInstance(field.data_type, String)
def test_preserve_aliases_from_api(self): api = Api(version=None) # Ensure imports come after 'preserve_alias' lexiographicaly # to catch namespace ordering bugs api.ensure_namespace('preserve_alias') api.ensure_namespace('zzzz') ns = api.namespaces['preserve_alias'] imported = api.namespaces['zzzz'] namespace_id = Alias('NamespaceId', ns, None) namespace_id.data_type = String() shared_folder_id = Alias('SharedFolderId', ns, None) shared_folder_id.set_attributes(None, namespace_id) path_root_id = Alias('PathRootId', ns, None) path_root_id.set_attributes(None, shared_folder_id) foo_alias = Alias('FooAlias', None, None) foo_alias.set_attributes(None, String()) bar_alias = Alias('BarAlias', None, None) bar_alias.set_attributes(None, foo_alias) ns.add_alias(namespace_id) ns.add_alias(shared_folder_id) ns.add_alias(path_root_id) imported.add_alias(foo_alias) imported.add_alias(bar_alias) test_struct = Struct('TestStruct', ns, None) test_struct.set_attributes( None, [StructField('field1', path_root_id, None, None)]) test_union = Union('TestUnion', ns, None, None) test_union.set_attributes( None, [UnionField('test', path_root_id, None, None)]) dependent_struct = Struct('DependentStruct', ns, None) dependent_struct.set_attributes(None, [ StructField('field_alias', imported.alias_by_name['BarAlias'], None, None) ]) ns.add_data_type(test_struct) ns.add_data_type(test_union) ns.add_data_type(dependent_struct) struct_alias = Alias('StructAlias', ns, None) struct_alias.set_attributes(None, test_struct) ns.add_alias(struct_alias) # Ensure namespace exists self.assertEqual(len(api.namespaces), 2) self.assertTrue('preserve_alias' in api.namespaces) self.assertTrue('zzzz' in api.namespaces) ns = api.namespaces['preserve_alias'] imported = api.namespaces['zzzz'] # Ensure aliases exist self.assertEqual(len(ns.aliases), 4) self.assertEqual(len(imported.aliases), 2) aliases = {alias._name: alias for alias in ns.aliases} imported_aliases = {alias.name: alias for alias in imported.aliases} data_types = { data_type._name: data_type for data_type in ns.data_types } # Ensure aliases are in the namespace self.assertTrue('NamespaceId' in aliases) self.assertTrue('SharedFolderId' in aliases) self.assertTrue('PathRootId' in aliases) self.assertTrue('StructAlias' in aliases) self.assertTrue('FooAlias' in imported_aliases) self.assertTrue('BarAlias' in imported_aliases) # Ensure aliases resolve to proper types self.assertIsInstance(aliases['NamespaceId'].data_type, String) self.assertIsInstance(aliases['SharedFolderId'].data_type, Alias) self.assertIsInstance(aliases['PathRootId'].data_type, Alias) self.assertIsInstance(aliases['StructAlias'].data_type, Struct) self.assertIsInstance(imported_aliases['FooAlias'].data_type, String) self.assertIsInstance(imported_aliases['BarAlias'].data_type, Alias) # Ensure struct and union field aliases resolve to proper types self.assertIsInstance(data_types['TestStruct'], Struct) test_struct = data_types.get('TestStruct') dependent_struct = data_types.get('DependentStruct') self.assertTrue(len(test_struct.fields), 1) self.assertTrue(len(dependent_struct.fields), 1) field = test_struct.fields[0] self.assertEqual(field.name, 'field1') self.assertIsInstance(field.data_type, Alias) field = dependent_struct.fields[0] self.assertEqual(field.name, 'field_alias') self.assertIsInstance(field.data_type, Alias) test_union = data_types['TestUnion'] self.assertTrue(len(test_union.fields), 1) field = test_union.fields[0] self.assertEqual(field.name, 'test') self.assertIsInstance(field.data_type, Alias)