def testGetFileSet_All(self): request = registry.GetFileSetRequest() request.names = ['my-service1', 'my-service2'] response = self.registry_service.get_file_set(request) expected_file_set = descriptor.describe_file_set(list(self.modules.values())) self.assertIterEqual(expected_file_set.files, response.file_set.files)
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 __describe_file_set(self, names): """Get file-set for named services. Args: names: List of names to get file-set for. Returns: descriptor.FileSet containing all the descriptors for all modules ultimately referred to by all service types request by names parameter. """ service_modules = set() if names: for service in (self.__registry[name] for name in names): found_modules = self.__definition_to_modules.setdefault( service, set()) if not found_modules: found_modules.add(self.__modules[service.__module__]) for method_name in service.all_remote_methods(): method = getattr(service, method_name) for message_type in (method.remote.request_type, method.remote.response_type): found_modules.update( self.__find_modules_for_message(message_type)) service_modules.update(found_modules) return descriptor.describe_file_set(service_modules)
def testImportFileSetFromFile(self): """Test importing a whole file set from a file.""" file_set = descriptor.FileSet() file_set.files = [ self.MakeFileDescriptor(u'standalone'), self.MakeFileDescriptor(u'root.nested'), self.MakeFileDescriptor(u'root.nested.nested'), ] stream = StringIO.StringIO(protobuf.encode_message(file_set)) self.mox = mox.Mox() opener = self.mox.CreateMockAnything() opener('my-file.dat', 'rb').AndReturn(stream) self.mox.ReplayAll() modules = {} definition.import_file_set('my-file.dat', modules=modules, _open=opener) self.assertEquals( file_set, descriptor.describe_file_set([ modules['standalone'], modules['root.nested'], modules['root.nested.nested'], ]))
def testImportFileSet(self): """Test importing a whole file set.""" file_set = descriptor.FileSet() file_set.files = [ self.MakeFileDescriptor(u'standalone'), self.MakeFileDescriptor(u'root.nested'), self.MakeFileDescriptor(u'root.nested.nested'), ] root = types.ModuleType('root') nested = types.ModuleType('root.nested') root.nested = nested modules = { 'root': root, 'root.nested': nested, } 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( file_set, descriptor.describe_file_set([ modules['standalone'], modules['root.nested'], modules['root.nested.nested'], ]))
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 testImportFileSet(self): """Test importing a whole file set.""" file_set = descriptor.FileSet() file_set.files = [self.MakeFileDescriptor(u'standalone'), self.MakeFileDescriptor(u'root.nested'), self.MakeFileDescriptor(u'root.nested.nested'), ] root = types.ModuleType('root') nested = types.ModuleType('root.nested') root.nested = nested modules = { 'root': root, 'root.nested': nested, } 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(file_set, descriptor.describe_file_set( [modules['standalone'], modules['root.nested'], modules['root.nested.nested'], ]))
def testImportFileSetFromFile(self): """Test importing a whole file set from a file.""" file_set = descriptor.FileSet() file_set.files = [self.MakeFileDescriptor(u'standalone'), self.MakeFileDescriptor(u'root.nested'), self.MakeFileDescriptor(u'root.nested.nested'), ] stream = StringIO.StringIO(protobuf.encode_message(file_set)) self.mox = mox.Mox() opener = self.mox.CreateMockAnything() opener('my-file.dat', 'rb').AndReturn(stream) self.mox.ReplayAll() modules = {} definition.import_file_set('my-file.dat', modules=modules, _open=opener) self.assertEquals(file_set, descriptor.describe_file_set( [modules['standalone'], modules['root.nested'], modules['root.nested.nested'], ]))
def testGetFileSet_All(self): request = registry.GetFileSetRequest() request.names = ['my-service1', 'my-service2'] response = self.registry_service.get_file_set(request) expected_file_set = descriptor.describe_file_set(self.modules.values()) self.assertIterEqual(expected_file_set.files, response.file_set.files)
def testGetFileSet_DoNotReferenceOtherModules(self): request = registry.GetFileSetRequest() request.names = ['my-service2'] response = self.registry_service.get_file_set(request) # Service does not reference test_util, so will only describe this module. expected_file_set = descriptor.describe_file_set([self.modules[__name__]]) self.assertIterEqual(expected_file_set.files, response.file_set.files)
def testGetFileSet_ReferenceOtherModules(self): request = registry.GetFileSetRequest() request.names = ['my-service1'] response = self.registry_service.get_file_set(request) # Will suck in and describe the test_util module. expected_file_set = descriptor.describe_file_set(self.modules.values()) self.assertIterEqual(expected_file_set.files, response.file_set.files)
def testGetFileSet_ReferenceOtherModules(self): request = registry.GetFileSetRequest() request.names = ['my-service1'] response = self.registry_service.get_file_set(request) # Will suck in and describe the test_util module. expected_file_set = descriptor.describe_file_set(list(self.modules.values())) self.assertIterEqual(expected_file_set.files, response.file_set.files)
def testGetFileSet_All(self): request = registry.GetFileSetRequest() request.names = ['my-service1', 'my-service2'] response = self.registry_service.get_file_set(request) expected_file_set = descriptor.describe_file_set(self.modules.values()) expected_file_set.files.sort(key=lambda f: f.package) response.file_set.files.sort(key=lambda f: f.package) self.assertEquals(expected_file_set.files, response.file_set.files)
def testWithModules(self): """Test what happens when no modules provided.""" modules = [types.ModuleType('package1'), types.ModuleType('package1')] file1 = descriptor.FileDescriptor() file1.package = 'package1' file2 = descriptor.FileDescriptor() file2.package = 'package2' expected = descriptor.FileSet() expected.files = [file1, file1] described = descriptor.describe_file_set(modules) described.check_initialized() self.assertEquals(expected, described)
def testWithModules(self): """Test what happens when no modules provided.""" modules = [new.module('package1'), new.module('package1')] file1 = descriptor.FileDescriptor() file1.package = 'package1' file2 = descriptor.FileDescriptor() file2.package = 'package2' expected = descriptor.FileSet() expected.files = [file1, file1] described = descriptor.describe_file_set(modules) described.check_initialized() self.assertEquals(expected, described)
def __describe_file_set(self, names): """Get file-set for named services. Args: names: List of names to get file-set for. Returns: descriptor.FileSet containing all the descriptors for all modules ultimately referred to by all service types request by names parameter. """ service_modules = set() if names: for service in (self.__registry[name] for name in names): found_modules = self.__definition_to_modules.setdefault(service, set()) if not found_modules: found_modules.add(self.__modules[service.__module__]) for method_name in service.all_remote_methods(): method = getattr(service, method_name) for message_type in (method.remote.request_type, method.remote.response_type): found_modules.update(self.__find_modules_for_message(message_type)) service_modules.update(found_modules) return descriptor.describe_file_set(service_modules)
def testNoModules(self): """Test what happens when no modules provided.""" described = descriptor.describe_file_set([]) described.check_initialized() # The described FileSet.files will be None. self.assertEquals(descriptor.FileSet(), described)