def testImportFile(self): """Test importing FileDescriptor in to module space.""" modules = {} file_descriptor = self.MakeFileDescriptor('standalone') definition.import_file(file_descriptor, modules=modules) self.assertEquals(file_descriptor, descriptor.describe_file(modules['standalone']))
def testImportBuiltInProtorpcClasses(self): """Test that built in Protorpc classes are skipped.""" file_set = descriptor.FileSet() file_set.files = [self.MakeFileDescriptor(u'standalone'), self.MakeFileDescriptor(u'root.nested'), self.MakeFileDescriptor(u'root.nested.nested'), descriptor.describe_file(descriptor), ] root = types.ModuleType('root') nested = types.ModuleType('root.nested') root.nested = nested modules = { 'root': root, 'root.nested': nested, 'protorpc.descriptor': descriptor, } definition.import_file_set(file_set, modules=modules) self.assertEquals(root, modules['root']) self.assertEquals(nested, modules['root.nested']) self.assertEquals(nested.nested, modules['root.nested.nested']) self.assertEquals(descriptor, modules['protorpc.descriptor']) self.assertEquals(file_set, descriptor.describe_file_set( [modules['standalone'], modules['root.nested'], modules['root.nested.nested'], modules['protorpc.descriptor'], ]))
def testImportBuiltInProtorpcClasses(self): """Test that built in Protorpc classes are skipped.""" file_set = descriptor.FileSet() file_set.files = [ self.MakeFileDescriptor(u'standalone'), self.MakeFileDescriptor(u'root.nested'), self.MakeFileDescriptor(u'root.nested.nested'), descriptor.describe_file(descriptor), ] root = types.ModuleType('root') nested = types.ModuleType('root.nested') root.nested = nested modules = { 'root': root, 'root.nested': nested, 'protorpc.descriptor': descriptor, } definition.import_file_set(file_set, modules=modules) self.assertEquals(root, modules['root']) self.assertEquals(nested, modules['root.nested']) self.assertEquals(nested.nested, modules['root.nested.nested']) self.assertEquals(descriptor, modules['protorpc.descriptor']) self.assertEquals( file_set, descriptor.describe_file_set([ modules['standalone'], modules['root.nested'], modules['root.nested.nested'], modules['protorpc.descriptor'], ]))
def testImportFile_Nested(self): """Test importing FileDescriptor in to existing nested module.""" modules = {} file_descriptor = self.MakeFileDescriptor('root.nested') definition.import_file(file_descriptor, modules=modules) self.assertEquals(modules['root'].nested, modules['root.nested']) self.assertEquals(file_descriptor, descriptor.describe_file(modules['root.nested']))
def testNoPackageName(self): """Test describing a module with no module name.""" module = types.ModuleType('') expected = descriptor.FileDescriptor() described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described)
def testNoPackageName(self): """Test describing a module with no module name.""" module = new.module('') expected = descriptor.FileDescriptor() described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described)
def testImportFile_InToExisting(self): """Test importing FileDescriptor in to existing module.""" module = types.ModuleType('standalone') modules = {'standalone': module} file_descriptor = self.MakeFileDescriptor('standalone') definition.import_file(file_descriptor, modules=modules) self.assertEquals(module, modules['standalone']) self.assertEquals(file_descriptor, descriptor.describe_file(modules['standalone']))
def testEmptyModule(self): """Test describing an empty file.""" module = new.module('my.package.name') expected = descriptor.FileDescriptor() expected.package = 'my.package.name' described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described)
def testEmptyModule(self): """Test describing an empty file.""" module = types.ModuleType('my.package.name') expected = descriptor.FileDescriptor() expected.package = 'my.package.name' described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described)
def testPackageName(self): """Test using the 'package' module attribute.""" module = new.module('my.module.name') module.package = 'my.package.name' expected = descriptor.FileDescriptor() expected.package = 'my.package.name' described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described)
def testMain(self): """Test using the 'package' module attribute.""" module = types.ModuleType('__main__') module.__file__ = '/blim/blam/bloom/my_package.py' expected = descriptor.FileDescriptor() expected.package = 'my_package' described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described)
def testMain(self): """Test using the 'package' module attribute.""" module = new.module('__main__') module.__file__ = '/blim/blam/bloom/my_package.py' expected = descriptor.FileDescriptor() expected.package = 'my_package' described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described)
def testPackageName(self): """Test using the 'package' module attribute.""" module = types.ModuleType('my.module.name') module.package = 'my.package.name' expected = descriptor.FileDescriptor() expected.package = 'my.package.name' described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described)
def testDefineModule(self): """Test define_module function.""" file_descriptor = self.MakeFileDescriptor('my.package') module = definition.define_file(file_descriptor) self.assertEquals('my.package', module.__name__) self.assertEquals('my.package', module.MyEnum.__module__) self.assertEquals('my.package', module.MyMessage.__module__) self.assertEquals('my.package', module.MyService.__module__) self.assertEquals(file_descriptor, descriptor.describe_file(module))
def testImportFile_InToGlobalModules(self): """Test importing FileDescriptor in to global modules.""" original_modules = sys.modules try: sys.modules = dict(sys.modules) if 'standalone' in sys.modules: del sys.modules['standalone'] file_descriptor = self.MakeFileDescriptor('standalone') definition.import_file(file_descriptor) self.assertEquals(file_descriptor, descriptor.describe_file(sys.modules['standalone'])) finally: sys.modules = original_modules
def testImportFile_InToGlobalModules(self): """Test importing FileDescriptor in to global modules.""" original_modules = sys.modules try: sys.modules = dict(sys.modules) if 'standalone' in sys.modules: del sys.modules['standalone'] file_descriptor = self.MakeFileDescriptor('standalone') definition.import_file(file_descriptor) self.assertEquals( file_descriptor, descriptor.describe_file(sys.modules['standalone'])) finally: sys.modules = original_modules
def testDefineModule_ReuseModule(self): """Test updating module with additional definitions.""" file_descriptor = self.MakeFileDescriptor('my.package') module = types.ModuleType('override') self.assertEquals(module, definition.define_file(file_descriptor, module)) self.assertEquals('override', module.MyEnum.__module__) self.assertEquals('override', module.MyMessage.__module__) self.assertEquals('override', module.MyService.__module__) # One thing is different between original descriptor and new. file_descriptor.package = 'override' self.assertEquals(file_descriptor, descriptor.describe_file(module))
def testMessages(self): """Test that messages are described.""" module = self.LoadModule( 'my.package', 'class Message1(messages.Message): pass\n' 'class Message2(messages.Message): pass\n') message1 = descriptor.MessageDescriptor() message1.name = 'Message1' message2 = descriptor.MessageDescriptor() message2.name = 'Message2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.message_types = [message1, message2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described)
def testServices(self): """Test that services are described.""" module = self.LoadModule( 'my.package', 'class Service1(remote.Service): pass\n' 'class Service2(remote.Service): pass\n') service1 = descriptor.ServiceDescriptor() service1.name = 'Service1' service2 = descriptor.ServiceDescriptor() service2.name = 'Service2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.service_types = [service1, service2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described)
def testMessages(self): """Test that messages are described.""" module = self.LoadModule('my.package', 'class Message1(messages.Message): pass\n' 'class Message2(messages.Message): pass\n') message1 = descriptor.MessageDescriptor() message1.name = 'Message1' message2 = descriptor.MessageDescriptor() message2.name = 'Message2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.message_types = [message1, message2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described)
def testEnums(self): """Test that enums are described.""" module = self.LoadModule( 'my.package', 'class Enum1(messages.Enum): pass\n' 'class Enum2(messages.Enum): pass\n') enum1 = descriptor.EnumDescriptor() enum1.name = 'Enum1' enum2 = descriptor.EnumDescriptor() enum2.name = 'Enum2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.enum_types = [enum1, enum2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described)
def testEnums(self): """Test that enums are described.""" module = self.LoadModule('my.package', 'class Enum1(messages.Enum): pass\n' 'class Enum2(messages.Enum): pass\n') enum1 = descriptor.EnumDescriptor() enum1.name = 'Enum1' enum2 = descriptor.EnumDescriptor() enum2.name = 'Enum2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.enum_types = [enum1, enum2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described)
def testServices(self): """Test that services are described.""" module = self.LoadModule('my.package', 'class Service1(remote.Service): pass\n' 'class Service2(remote.Service): pass\n') service1 = descriptor.ServiceDescriptor() service1.name = 'Service1' service2 = descriptor.ServiceDescriptor() service2.name = 'Service2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.service_types = [service1, service2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described)
def DoPythonTest(self, file_descriptor): """Execute python test based on a FileDescriptor object. The full test of the Python code generation is to generate a Python source code file, import the module and regenerate the FileDescriptor from it. If the generated FileDescriptor is the same as the original, it means that the generated source code correctly implements the actual FileDescriptor. """ file_name = os.path.join( self.temp_dir, '%s.py' % (file_descriptor.package or 'blank', )) source_file = open(file_name, 'wt') try: generate_python.format_python_file(file_descriptor, source_file) finally: source_file.close() module_to_import = file_descriptor.package or 'blank' module = __import__(module_to_import) if not file_descriptor.package: self.assertFalse(hasattr(module, 'package')) module.package = '' # Create package name so that comparison will work. reloaded_descriptor = descriptor.describe_file(module) # Need to sort both message_types fields because document order is never # Ensured. # TODO(rafek): Ensure document order. if reloaded_descriptor.message_types: reloaded_descriptor.message_types = sorted( reloaded_descriptor.message_types, key=lambda v: v.name) if file_descriptor.message_types: file_descriptor.message_types = sorted( file_descriptor.message_types, key=lambda v: v.name) self.assertEquals(file_descriptor, reloaded_descriptor)
def DoPythonTest(self, file_descriptor): """Execute python test based on a FileDescriptor object. The full test of the Python code generation is to generate a Python source code file, import the module and regenerate the FileDescriptor from it. If the generated FileDescriptor is the same as the original, it means that the generated source code correctly implements the actual FileDescriptor. """ file_name = os.path.join(self.temp_dir, '%s.py' % (file_descriptor.package or 'blank',)) source_file = open(file_name, 'wt') try: generate_python.format_python_file(file_descriptor, source_file) finally: source_file.close() module_to_import = file_descriptor.package or 'blank' module = __import__(module_to_import) if not file_descriptor.package: self.assertFalse(hasattr(module, 'package')) module.package = '' # Create package name so that comparison will work. reloaded_descriptor = descriptor.describe_file(module) # Need to sort both message_types fields because document order is never # Ensured. # TODO(rafek): Ensure document order. if reloaded_descriptor.message_types: reloaded_descriptor.message_types = sorted( reloaded_descriptor.message_types, key=lambda v: v.name) if file_descriptor.message_types: file_descriptor.message_types = sorted( file_descriptor.message_types, key=lambda v: v.name) self.assertEquals(file_descriptor, reloaded_descriptor)
def testModule(self): self.assertEquals(descriptor.describe_file(test_util), descriptor.describe(test_util))
def testFindModule(self): self.assertEquals( descriptor.describe_file(registry), descriptor.import_descriptor_loader('protorpc.registry'))
def testFindModule(self): self.assertEquals(descriptor.describe_file(registry), descriptor.import_descriptor_loader('protorpc.registry'))
def format_proto_module(module, output, indent_space=2): file_descriptor = descriptor.describe_file(module) format_proto_file(file_descriptor, output, indent_space=indent_space)