コード例 #1
0
class LoggerFactoryTests(TestCase):
    def setUp(self):
        self.loader = LoaderNamespace()
        self.loader.register_namespace('logging', 'logging')
        register_logging_factories(self.loader)

    def test_simple_logger(self):
        logger = self.loader.factory('logging:Logger', name='foo.bar.test.1')
        self.assertEqual(logger, getLogger('foo.bar.test.1'))

    def test_simple_logger_with_simple_handler(self):
        logger = self.loader.factory('logging:Logger',
                                     name='foo.bar.test.2',
                                     handlers=['logging:NullHandler'])
        self.assertEqual(logger, getLogger('foo.bar.test.2'))
        self.assertEqual(len(logger.handlers), 1)
        self.assertIsInstance(logger.handlers[0], NullHandler)
コード例 #2
0
 def setUp(self):
     self.loader = LoaderNamespace()
     self.loader.register_namespace('logging', 'logging')
     self.loader.register_namespace('sc', 'service_client')
     self.loader.register_namespace('sc-plugins', 'service_client.plugins')
     self.loader.register_namespace('here', __name__)
     register_logging_factories(self.loader)
     register_service_client_factories(self.loader)
コード例 #3
0
class LoggerPluginFactoryFactoryTests(TestCase):

    def setUp(self):
        self.loader = LoaderNamespace()
        self.loader.register_namespace('logging', 'logging')
        self.loader.register_namespace('sc-plugins', 'service_client.plugins')
        register_logging_factories(self.loader)
        register_service_client_factories(self.loader)

    def test_inner_logger_build(self):
        plugin = self.loader.factory('sc-plugins:InnerLogger', logger={'type': 'logging:Logger',
                                                                       'params': {'name': 'foo.bar.test.1',
                                                                                  'handlers': ['logging:NullHandler']}})
        self.assertIsInstance(plugin, InnerLogger)
        self.assertEqual(plugin.logger, getLogger('foo.bar.test.1'))
        self.assertEqual(len(plugin.logger.handlers), 1)
        self.assertIsInstance(plugin.logger.handlers[0], NullHandler)
コード例 #4
0
class BaseFactoryTests(TestCase):
    def setUp(self):
        self.loader = LoaderNamespace()
        self.loader.register_namespace('logging', 'logging')
        register_logging_factories(self.loader)

    def test_iter_loaded_named_item_list(self):
        factory = BaseFactory(self.loader, self.__class__)

        result = dict(
            factory.iter_loaded_named_item_list({
                'handler':
                'logging:NullHandler',
                'formatter':
                'logging:Formatter'
            }))

        self.assertEqual(len(result), 2)
        self.assertIn('handler', result)
        self.assertIsInstance(result['handler'], NullHandler)
        self.assertIn('formatter', result)
        self.assertIsInstance(result['formatter'], Formatter)

    def test_iter_loaded_named_item_list_none(self):
        factory = BaseFactory(self.loader, self.__class__)

        result = dict(factory.iter_loaded_named_item_list(None))

        self.assertEqual(len(result), 0)

    def test_iter_loaded_item_list_none(self):
        factory = BaseFactory(self.loader, self.__class__)

        result = list(factory.iter_loaded_item_list(None))

        self.assertEqual(len(result), 0)

    def test_load_item(self):
        factory = BaseFactory(self.loader, self.__class__)

        self.assertEqual(factory.load_item('foobar', str), 'foobar')
コード例 #5
0
class LoggingHandlerFactoryTests(TestCase):
    def setUp(self):
        self.loader = LoaderNamespace()
        self.loader.register_namespace('logging', 'logging')
        register_logging_factories(self.loader)

    def test_simple_handler(self):
        handler = self.loader.factory('logging:NullHandler')
        self.assertIsInstance(handler, NullHandler)
        self.assertEqual(len(handler.filters), 0)

    def test_simple_handler_with_simple_filter(self):
        handler = self.loader.factory('logging:NullHandler',
                                      filters=['logging:Filter'])
        self.assertIsInstance(handler, NullHandler)
        self.assertEqual(len(handler.filters), 1)
        self.assertIsInstance(handler.filters[0], Filter)

    def test_simple_handler_with_parametrized_filter(self):
        handler = self.loader.factory('logging:NullHandler',
                                      filters=[{
                                          'type': 'logging:Filter',
                                          'params': {
                                              'name': 'foo.bar'
                                          }
                                      }, {
                                          'type': 'logging:Filter'
                                      }, 'logging:Filter'])
        self.assertIsInstance(handler, NullHandler)
        self.assertEqual(len(handler.filters), 3)
        self.assertIsInstance(handler.filters[0], Filter)
        self.assertEqual(handler.filters[0].name, 'foo.bar')
        self.assertIsInstance(handler.filters[1], Filter)
        self.assertEqual(handler.filters[1].name, '')
        self.assertIsInstance(handler.filters[2], Filter)
        self.assertEqual(handler.filters[2].name, '')

    def test_simple_handler_with_simple_formatter(self):
        handler = self.loader.factory('logging:NullHandler',
                                      formatter='logging:Formatter')
        self.assertIsInstance(handler, NullHandler)
        self.assertIsInstance(handler.formatter, Formatter)

    def test_simple_handler_with_parametrized_formatter(self):
        handler = self.loader.factory('logging:NullHandler',
                                      formatter={
                                          'type': 'logging:Formatter',
                                          'params': {
                                              'datefmt': 'foo.bar'
                                          }
                                      })
        self.assertIsInstance(handler, NullHandler)
        self.assertIsInstance(handler.formatter, Formatter)
        self.assertEqual(handler.formatter.datefmt, 'foo.bar')
コード例 #6
0
 def setUp(self):
     self.loader = LoaderNamespace()
コード例 #7
0
class LoaderNamespaceTest(TestCase):
    def setUp(self):
        self.loader = LoaderNamespace()

    def test_register_module(self):
        self.loader.register_module('tests.fake.namespace1')
        self.loader.register_module('tests.fake.namespace2')

        self.assertEqual(list(self.loader.get_registered_modules()),
                         ['tests.fake.namespace1', 'tests.fake.namespace2'])

    def test_register_namespace(self):
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')

        self.assertEqual(list(self.loader.get_registered_modules()),
                         ['tests.fake.namespace1', 'tests.fake.namespace2'])

        self.assertEqual(
            self.loader.get_registered_namespaces(),
            OrderedDict([('fake1', 'tests.fake.namespace1'),
                         ('fake2', 'tests.fake.namespace2')]))

    def test_load_class_1(self):
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')
        self.loader.register_namespace('fake3', 'tests.fake.namespace3')

        from tests.fake.namespace1 import FakeClass1, FakeClass2, FakeClass3

        klass = self.loader.load_class('FakeClass1')
        self.assertEquals(klass, FakeClass1)

        klass = self.loader.load_class('FakeClass2')
        self.assertEquals(klass, FakeClass2)

        klass = self.loader.load_class('FakeClass3')
        self.assertEquals(klass, FakeClass3)

        klass = self.loader.load_class('subnamespace.FakeClass4')
        from tests.fake.namespace3.subnamespace import FakeClass4

        self.assertEquals(klass, FakeClass4)

        klass = self.loader.load_class(
            'fake3:subsubnamespace.subnamespace.FakeClass4')
        from tests.fake.namespace3.subsubnamespace.subnamespace import FakeClass4 as SubFakeClass4

        self.assertEquals(klass, SubFakeClass4)

    def test_load_class_2(self):
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')

        from tests.fake.namespace1 import FakeClass3
        from tests.fake.namespace2 import FakeClass1, FakeClass2

        klass = self.loader.load_class('fake2:FakeClass1')
        self.assertEquals(klass, FakeClass1)

        klass = self.loader.load_class('FakeClass2', namespace='fake2')
        self.assertEquals(klass, FakeClass2)

        klass = self.loader.load_class('FakeClass3')
        self.assertEquals(klass, FakeClass3)

    def test_unregister_module(self):
        self.loader.register_module('tests.fake.namespace2')
        self.loader.register_module('tests.fake.namespace1')

        self.loader.unregister_module('tests.fake.namespace2')

        from tests.fake.namespace1 import FakeClass1, FakeClass2, FakeClass3

        klass = self.loader.load_class('FakeClass1')
        self.assertEquals(klass, FakeClass1)

        klass = self.loader.load_class('FakeClass2')
        self.assertEquals(klass, FakeClass2)

        klass = self.loader.load_class('FakeClass3')
        self.assertEquals(klass, FakeClass3)

    def test_unregister_namespace(self):
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')

        self.loader.unregister_namespace('fake2')

        from tests.fake.namespace1 import FakeClass1, FakeClass2, FakeClass3

        klass = self.loader.load_class('FakeClass1')
        self.assertEquals(klass, FakeClass1)

        klass = self.loader.load_class('FakeClass2')
        self.assertEquals(klass, FakeClass2)

        klass = self.loader.load_class('FakeClass3')
        self.assertEquals(klass, FakeClass3)

    def test_unregister_module_fail(self):
        with self.assertRaises(NoRegisteredError):
            self.loader.unregister_module('tests.fake.namespace2')

    def test_unregister_namespace_fail(self):
        with self.assertRaises(NoRegisteredError):
            self.loader.unregister_namespace('fake2')

    def test_register_module_fail(self):
        self.loader.register_module('tests.fake.namespace1')
        self.loader.register_module('tests.fake.namespace2')
        with self.assertRaises(AlreadyRegisteredError):
            self.loader.register_module('tests.fake.namespace1')

    def test_register_namespace_fail(self):
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')
        with self.assertRaises(AlreadyRegisteredError):
            self.loader.register_namespace('fake1', 'tests.fake.namespace1')

    def test_load_fail_1(self):
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')
        with self.assertRaises(ImportError):
            self.loader.load_class('FakeClass3')

    def test_load_fail_2(self):
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')
        with self.assertRaises(ImportError):
            self.loader.load_class('fake2:FakeClass3')

    def test_load_fail_3(self):
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')
        with self.assertRaises(NoRegisteredError):
            self.loader.load_class('fake3:FakeClass3')

    def test_factory(self):
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')

        from tests.fake.namespace1 import FakeClass2, FakeClass3
        from tests.fake.namespace2 import FakeClass1

        obj = self.loader.factory('FakeClass1', var1='a', var2=2)
        self.assertIsInstance(obj, FakeClass1)
        self.assertEquals(obj.var1, 'a')
        self.assertEquals(obj.var2, 2)

        obj = self.loader.factory('fake1:FakeClass2')
        self.assertIsInstance(obj, FakeClass2)
        self.assertIsNone(obj.var1)
        self.assertIsNone(obj.var2)

        obj = self.loader.factory('FakeClass3', var1='a', var2=2)
        self.assertIsInstance(obj, FakeClass3)
        self.assertEquals(obj.var1, 'a')
        self.assertEquals(obj.var2, 2)
コード例 #8
0
 def setUp(self):
     self.loader = LoaderNamespace()
コード例 #9
0
class LoaderNamespaceTest(TestCase):

    def setUp(self):
        self.loader = LoaderNamespace()

    def test_register_module(self):
        self.loader.register_module('tests.fake.namespace1')
        self.loader.register_module('tests.fake.namespace2')

        self.assertEqual(list(self.loader.get_registered_modules()), ['tests.fake.namespace1',
                                                                      'tests.fake.namespace2'])

    def test_register_namespace(self):
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')

        self.assertEqual(list(self.loader.get_registered_modules()), ['tests.fake.namespace1',
                                                                      'tests.fake.namespace2'])

        self.assertEqual(self.loader.get_registered_namespaces(), OrderedDict([('fake1', 'tests.fake.namespace1'),
                                                                               ('fake2', 'tests.fake.namespace2')]))

    def test_load_class_1(self):
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')
        self.loader.register_namespace('fake3', 'tests.fake.namespace3')

        from tests.fake.namespace1 import FakeClass1, FakeClass2, FakeClass3

        klass = self.loader.load_class('FakeClass1')
        self.assertEquals(klass, FakeClass1)

        klass = self.loader.load_class('FakeClass2')
        self.assertEquals(klass, FakeClass2)

        klass = self.loader.load_class('FakeClass3')
        self.assertEquals(klass, FakeClass3)

        klass = self.loader.load_class('subnamespace.FakeClass4')
        from tests.fake.namespace3.subnamespace import FakeClass4

        self.assertEquals(klass, FakeClass4)

        klass = self.loader.load_class('fake3:subsubnamespace.subnamespace.FakeClass4')
        from tests.fake.namespace3.subsubnamespace.subnamespace import FakeClass4 as SubFakeClass4

        self.assertEquals(klass, SubFakeClass4)

    def test_load_class_2(self):
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')

        from tests.fake.namespace1 import FakeClass3
        from tests.fake.namespace2 import FakeClass1, FakeClass2

        klass = self.loader.load_class('fake2:FakeClass1')
        self.assertEquals(klass, FakeClass1)

        klass = self.loader.load_class('FakeClass2', namespace='fake2')
        self.assertEquals(klass, FakeClass2)

        klass = self.loader.load_class('FakeClass3')
        self.assertEquals(klass, FakeClass3)

    def test_unregister_module(self):
        self.loader.register_module('tests.fake.namespace2')
        self.loader.register_module('tests.fake.namespace1')

        self.loader.unregister_module('tests.fake.namespace2')

        from tests.fake.namespace1 import FakeClass1, FakeClass2, FakeClass3

        klass = self.loader.load_class('FakeClass1')
        self.assertEquals(klass, FakeClass1)

        klass = self.loader.load_class('FakeClass2')
        self.assertEquals(klass, FakeClass2)

        klass = self.loader.load_class('FakeClass3')
        self.assertEquals(klass, FakeClass3)

    def test_unregister_namespace(self):
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')

        self.loader.unregister_namespace('fake2')

        from tests.fake.namespace1 import FakeClass1, FakeClass2, FakeClass3

        klass = self.loader.load_class('FakeClass1')
        self.assertEquals(klass, FakeClass1)

        klass = self.loader.load_class('FakeClass2')
        self.assertEquals(klass, FakeClass2)

        klass = self.loader.load_class('FakeClass3')
        self.assertEquals(klass, FakeClass3)

    def test_unregister_module_fail(self):
        with self.assertRaises(NoRegisteredError):
            self.loader.unregister_module('tests.fake.namespace2')

    def test_unregister_namespace_fail(self):
        with self.assertRaises(NoRegisteredError):
            self.loader.unregister_namespace('fake2')

    def test_register_module_fail(self):
        self.loader.register_module('tests.fake.namespace1')
        self.loader.register_module('tests.fake.namespace2')
        with self.assertRaises(AlreadyRegisteredError):
            self.loader.register_module('tests.fake.namespace1')

    def test_register_namespace_fail(self):
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')
        with self.assertRaises(AlreadyRegisteredError):
            self.loader.register_namespace('fake1', 'tests.fake.namespace1')

    def test_load_fail_1(self):
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')
        with self.assertRaises(ImportError):
            self.loader.load_class('FakeClass3')

    def test_load_fail_2(self):
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')
        with self.assertRaises(ImportError):
            self.loader.load_class('fake2:FakeClass3')

    def test_load_fail_3(self):
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')
        with self.assertRaises(NoRegisteredError):
            self.loader.load_class('fake3:FakeClass3')

    def test_factory(self):
        self.loader.register_namespace('fake2', 'tests.fake.namespace2')
        self.loader.register_namespace('fake1', 'tests.fake.namespace1')

        from tests.fake.namespace1 import FakeClass2, FakeClass3
        from tests.fake.namespace2 import FakeClass1

        obj = self.loader.factory('FakeClass1', var1='a', var2=2)
        self.assertIsInstance(obj, FakeClass1)
        self.assertEquals(obj.var1, 'a')
        self.assertEquals(obj.var2, 2)

        obj = self.loader.factory('fake1:FakeClass2')
        self.assertIsInstance(obj, FakeClass2)
        self.assertIsNone(obj.var1)
        self.assertIsNone(obj.var2)

        obj = self.loader.factory('FakeClass3', var1='a', var2=2)
        self.assertIsInstance(obj, FakeClass3)
        self.assertEquals(obj.var1, 'a')
        self.assertEquals(obj.var2, 2)
コード例 #10
0
 def setUp(self):
     self.loader = LoaderNamespace()
     self.loader.register_namespace('logging', 'logging')
     register_logging_factories(self.loader)
コード例 #11
0
class ServiceClientFactoryTests(TestCase):

    def setUp(self):
        self.loader = LoaderNamespace()
        self.loader.register_namespace('logging', 'logging')
        self.loader.register_namespace('sc', 'service_client')
        self.loader.register_namespace('sc-plugins', 'service_client.plugins')
        self.loader.register_namespace('here', __name__)
        register_logging_factories(self.loader)
        register_service_client_factories(self.loader)

    def test_service_client_basic(self):

        definition = {'name': 'test1',
                      'spec': {'test': {'path': 'baz'}},
                      'parser': 'sc:json.json_decoder',
                      'serializer': 'sc:json.json_encoder',
                      'logger': {'type': 'logging:Logger',
                                 'params': {'name': 'foo.bar.test.2',
                                            'handlers': ['logging:NullHandler']}}}

        sc = self.loader.factory('sc:ServiceClient', **definition)

        self.assertIsInstance(sc, ServiceClient)
        self.assertEqual(sc.name, 'test1')
        self.assertEqual(sc.spec, definition['spec'])
        self.assertEqual(sc.parser, json_decoder)
        self.assertEqual(sc.serializer, json_encoder)
        self.assertEqual(sc.logger, getLogger('foo.bar.test.2'))
        self.assertEqual(len(sc.logger.handlers), 1)
        self.assertIsInstance(sc.logger.handlers[0], NullHandler)

    def test_service_client_with_plugins(self):
        definition = {'name': 'test1',
                      'spec': {'test': {'path': 'baz'}},
                      'plugins': ['sc-plugins:PathTokens',
                                  {'type': 'sc-plugins:InnerLogger',
                                   'params': {'logger': {'type': 'logging:Logger',
                                                         'params': {'name': 'foo.bar.test.3',
                                                                    'handlers': ['logging:NullHandler']}}}}]}

        sc = self.loader.factory('sc:ServiceClient', **definition)

        self.assertIsInstance(sc, ServiceClient)
        self.assertEqual(sc.name, 'test1')
        self.assertEqual(sc.spec, definition['spec'])
        self.assertEqual(len(sc._plugins), 2)
        self.assertIsInstance(sc._plugins[0], PathTokens)
        self.assertIsInstance(sc._plugins[1], InnerLogger)
        self.assertEqual(sc._plugins[1].logger, getLogger('foo.bar.test.3'))
        self.assertEqual(len(sc._plugins[1].logger.handlers), 1)
        self.assertIsInstance(sc._plugins[1].logger.handlers[0], NullHandler)

    def test_service_client_with_spec_loader(self):
        definition = {'name': 'test1',
                      'spec_loader': {'type': 'here:fake_loader',
                                      'params': {'value': 'baz'}}}

        sc = self.loader.factory('sc:ServiceClient', **definition)

        self.assertIsInstance(sc, ServiceClient)
        self.assertEqual(sc.name, 'test1')
        self.assertEqual(sc.spec, {'test': {'path': 'baz'}})