Exemplo n.º 1
0
    def test_resolver_attr(self):

        container = DIContainer({})
        container.register('python_sys', {
            'type': 'mock.MagicMock',
            'args': {
                'version_info': attr('sys.version_info')
            }
        })

        py_sys = container.resolve('python_sys')
        self.assertEqual(py_sys.version_info, sys.version_info)
Exemplo n.º 2
0
 def test__del_singleton_on_register(self):
     """
     Passes of the singletons instance becomes deleted on reregistering
     the name and replace it.
     """
     type_conf = {
         "type": "mock.Mock",
         "singleton": True
     }
     container = DIContainer({
         "test": type_conf
     })
     t = container.resolve("test")
     self.assertTrue("test" in container.singletons)
     container.register("test", type_conf, replace=True)
     self.assertFalse("test" in container.singletons)
Exemplo n.º 3
0
    def test__register(self):
        """
        Passes if `runtime_config` is not configured and is configured at the
        end of the test. the second registration raises an error.
        """
        container = DIContainer({})

        def raises_not_registred():
            container.resolve('runtime_config')

        self.assertRaises(KeyError, raises_not_registred)
        container.register('runtime_config', {'type': 'mock.MagicMock'})
        runtime_config = container.resolve('runtime_config')
        self.assertIsNotNone(runtime_config)

        def raises_already_registred():
            container.register('runtime_config', {'type': 'mock.MagicMock'})

        self.assertRaises(KeyError, raises_already_registred)
Exemplo n.º 4
0
    def test_events(self):
        manager = DIContainer(TEST_DI_SETTINGS, event_dispatcher=mock.MagicMock)
        for name in ('jens',):
            manager.resolve(name)

        manager.resolve_type('jessica')
        manager.clear()
        manager.register('henrik_2', {
            'type': 'test.TestPerson',
            'singleton': True,
            'args': {'': ['Henrik', 'Blawatt', 24]}
        })

        for method in ('initialized', 'before_register', 'after_register',
                       'after_resolve', 'before_resolve', 'before_build_up',
                       'after_build_up', 'before_resolve_type', 'after_resolve_type',
                       'after_clear',):
            self.assertTrue(
                getattr(manager.event_dispatcher, method).called,
                'called method {0}.'.format(method)
            )
Exemplo n.º 5
0
    def test__hooks_called(self):

        # TODO: improve.

        container = DIContainer({'a': DIConfig(
            type='mock.Mock',
            args={'': [mock.Mock()], 'b': mock.Mock()}
        )}, event_dispatcher=mock.Mock())

        container.resolve('a')
        container.clear()
        container.resolve_type('a')
        container.register('b', DIConfig(type='mock.Mock'))

        for method in ('initialized', 'before_register', 'after_register',
                       'after_resolve', 'before_resolve', 'before_build_up',
                       'after_build_up', 'before_resolve_type', 'after_resolve_type',
                       'after_clear',):
            self.assertTrue(
                getattr(container.event_dispatcher, method).called,
                'method {0} not called.'.format(method))
Exemplo n.º 6
0
    def test__register_replace(self):
        """
        Passes if the configuration of `to_be_replaced` becomes replaced.
        """
        container = DIContainer({
            'to_be_replaced': {
                'type': 'mock.Mock',
                'properties': {
                    'id': '123456789'
                }
            }
        })

        inst = container.resolve("to_be_replaced")
        self.assertEqual(inst.id, '123456789')

        repl_config = {'type': 'mock.Mock', 'properties': {'id': '987654321'}}
        container.register("to_be_replaced", repl_config, True)

        inst = container.resolve("to_be_replaced")
        self.assertEqual(inst.id, '987654321')
Exemplo n.º 7
0
class DIContainerTestCase(unittest.TestCase):

    def setUp(self):
        self.manager = DIContainer(TEST_DI_SETTINGS)

    def test__person(self):
        p1 = self.manager.resolve('person')
        p2 = self.manager.resolve('person')

        self.assertIsNotNone(p1)
        self.assertIsNotNone(p2)

        self.assertNotEqual(p1, p2)

    def test__singleton(self):
        j1 = self.manager.resolve('jessica')
        j2 = self.manager.resolve('jessica')

        self.assertIsNotNone(j1)
        self.assertIsNotNone(j2)

        self.assertEqual(j1, j2)

    def test__relation(self):
        jessica = self.manager.resolve('jessica')
        jens = self.manager.resolve('jens')

        self.assertIsNotNone(jessica)
        self.assertIsNotNone(jens)

        self.assertEqual(jens.loves, jessica)
        self.assertEqual(jens.hobbies, ['Tennis', 'Programming'])

    def test__attr(self):
        jens1 = self.manager.resolve('jens')
        jens2 = self.manager.jens

        self.assertEqual(jens1, jens2)

        p1 = self.manager.person
        p2 = self.manager.person

        self.assertNotEqual(p1, p2)

    def test__lazy(self):
        self.assertIn('jens_nl', self.manager.singletons)
        self.assertNotIn('jens', self.manager.singletons)

    def test__register(self):
        self.manager.register('henrik', {
            'type': 'test.TestPerson',
            'singleton': True,
            'args': {'': ['Henrik', 'Blawatt', 24]}
        })

        henrik1 = self.manager.henrik
        henrik2 = self.manager.resolve('henrik')

        self.assertEqual(henrik1, henrik2)
        self.assertEqual(henrik1.first_name, 'Henrik')
        self.assertEqual(henrik1.last_name, 'Blawatt')
        self.assertEqual(henrik1.age, 24)

    def test_autopath(self):

        def create_tempmodule():
            with open(temp_file, 'w') as f:
                f.write(
                    "class AutoPathPerson(object):\n"
                    "    pass\n"
                )

        create_tempmodule()

        jens_autopath = self.manager.jens_autopath
        self.assertIsNotNone(jens_autopath)
        self.assertEqual(jens_autopath.first_name, 'Jens')
        self.assertEqual(jens_autopath.last_name, 'Blawatt')
        self.assertIn(temp_path, sys.path)

    def test_assertbasetype(self):

        def will_raise():
            o = self.manager.jens_assert_no_base
            assert o

        def will_not_raise():
            o = self.manager.jens_assert_base
            assert o

        self.assertRaises(TypeError, will_raise)

        try:
            will_not_raise()
        except Exception as e:
            self.fail('raised unexpected error: %s' % e)

    def test_logging_mod(self):
        objekt = self.manager.logging_mod
        self.assertTrue(isinstance(objekt, dict))
        module = objekt.get('logging')
        self.assertEqual(module.DEBUG, 10)

    def test_logging_ref(self):
        objekt = self.manager.logging_ref
        value = objekt.get('DEBUG')
        self.assertTrue(value, 10)

    def test_resolve_type(self):
        type_ = self.manager.resolve_type('jens')
        self.assertEqual(type_, TestPerson)

    def test_does_not_exist(self):
        def fnc():
            self.manager.does_not_exist
        self.assertRaises(AttributeError, fnc)

    def test_dir(self):
        values = dir(self.manager)
        for k in TEST_DI_SETTINGS.keys():
            self.assertIn(k, values)

    def test_register_registered(self):
        def fnc():
            self.manager.register('jens', dict(type='mock.Mock'))
        self.assertRaises(KeyError, fnc)

    def test_clear_singleton(self):
        def setUp():
            self.manager.singletons['foo'] = 'FOO'
            self.manager.singletons['bar'] = 'BAR'

        setUp()
        self.manager.clear('not-in-singletons')
        self.manager.clear('foo')
        self.assertNotIn('foo', self.manager.singletons)
        self.assertIn('bar', self.manager.singletons)

        self.manager.singletons = dict()

        setUp()
        self.manager.clear()
        self.assertNotIn('foo', self.manager.singletons)
        self.assertNotIn('bar', self.manager.singletons)

    def test_events(self):
        manager = DIContainer(TEST_DI_SETTINGS, event_dispatcher=mock.MagicMock)
        for name in ('jens',):
            manager.resolve(name)

        manager.resolve_type('jessica')
        manager.clear()
        manager.register('henrik_2', {
            'type': 'test.TestPerson',
            'singleton': True,
            'args': {'': ['Henrik', 'Blawatt', 24]}
        })

        for method in ('initialized', 'before_register', 'after_register',
                       'after_resolve', 'before_resolve', 'before_build_up',
                       'after_build_up', 'before_resolve_type', 'after_resolve_type',
                       'after_clear',):
            self.assertTrue(
                getattr(manager.event_dispatcher, method).called,
                'called method {0}.'.format(method)
            )

    def test_inject(self):
        @self.manager.inject(person='jens')
        def to_inject_function(person=None):
            self.assertIsNotNone(person)
            self.assertEqual(person.first_name, 'Jens')

        to_inject_function()

    def test_inject_force(self):
        @self.manager.inject(force=True, person='jens')
        def to_inject_function(person=None):
            self.assertIsNotNone(person)
            self.assertEqual(person.first_name, 'Jens')

        to_inject_function(person=self.manager.jens)

    def test_inject_noforce(self):
        @self.manager.inject(force=False, person='jens')
        def to_inject_function(person=None):
            self.assertIsNotNone(person)
            self.assertEqual(person.first_name, 'Jens')

        to_inject_function(person=self.manager.jens)

    def test_child_container(self):

        ccontainer = self.manager.create_child_container({
            'jens': {
                'type': 'test.TestPerson',
                'args': {
                    'first_name': 'Jens',
                    'last_name': 'Backhaus',
                    'age': 32
                }
            }
        })

        jens_bl = self.manager.resolve('jens')
        jens_ba = ccontainer.resolve('jens')

        self.assertNotEqual(jens_ba, jens_bl)
        self.assertEqual(jens_ba.first_name, jens_bl.first_name)
        self.assertEqual(jens_ba.last_name, 'Backhaus')

    def test_resolver_attr(self):

        container = DIContainer({})
        container.register('python_sys', {
            'type': 'mock.MagicMock',
            'args': {
                'version_info': attr('sys.version_info')
            }
        })

        py_sys = container.resolve('python_sys')
        self.assertEqual(py_sys.version_info, sys.version_info)