Ejemplo n.º 1
0
    def test_simple_function(self):
        @Inject(container=CDIContainer)
        def function(container):
            self.assertIsNotNone(container)
            self.assertIsInstance(container, CDIContainer)

        DEFAULT_CONTAINER.call(function)
Ejemplo n.º 2
0
    def test_inject(self):
        @Inject(numbers=([int], 'default'))
        def function_with_injection(numbers):
            self.assertEqual(list(numbers), [0, 1])
            self.called = True

        DEFAULT_CONTAINER.call(function_with_injection)
        self.assertTrue(self.called)
Ejemplo n.º 3
0
 def test_sequential(self):
     singletons = [
         DEFAULT_CONTAINER.produce(SequentialClass)
         for x in range(SINGLETON_LIMIT)
     ]
     self.assertEqual(len(singletons), len(set(singletons)))
     singletons += [
         DEFAULT_CONTAINER.produce(SequentialClass)
         for x in range(SINGLETON_LIMIT)
     ]
     self.assertNotEqual(len(singletons), len(set(singletons)))
     self.assertEqual(len(set(singletons)), SINGLETON_LIMIT)
Ejemplo n.º 4
0
    def test_common_args(self):
        DEFAULT_CONTAINER.register_instance(2, int)
        DEFAULT_CONTAINER.register_instance(4.0, float)

        @Inject(int)
        @Inject(float, int)
        def sum_func(*args):
            self.assertIsInstance(args[0], float)
            self.assertIsInstance(args[1], int)
            self.assertIsInstance(args[1], int)
            self.assertGreaterEqual(len(args), 3)
            return sum(args)

        self.assertEqual(8, call(sum_func))
        self.assertEqual(11, call(sum_func, 3))
Ejemplo n.º 5
0
    def test_function_without_type(self):
        @Singleton()
        def get_singleton():
            return object()

        singletons = [
            DEFAULT_CONTAINER.produce(object) for x in range(SINGLETON_LIMIT)
        ]
        self.assertEqual(len(set(singletons)), 1)
Ejemplo n.º 6
0
    def test_function_with_type(self):
        @Singleton(produce_type=float)
        def get_singleton():
            return random()

        singletons = [
            DEFAULT_CONTAINER.produce(float) for x in range(SINGLETON_LIMIT)
        ]
        self.assertEqual(len(set(singletons)), 1)
Ejemplo n.º 7
0
    def test_simple_class(self):
        @Inject(container=CDIContainer)
        class Class(object):
            def __init__(self, container):
                self.container = container

        obj = DEFAULT_CONTAINER.produce(Class)
        self.assertIsNotNone(obj.container)
        self.assertIsInstance(obj.container, CDIContainer)
        self.assertEqual(DEFAULT_CONTAINER, obj.container)
Ejemplo n.º 8
0
    def test_random(self):
        singletons = []
        for i in range(SINGLETON_LIMIT):
            singletons = list(singletons)
            singletons += [
                DEFAULT_CONTAINER.produce(RandomClass)
                for x in range(SINGLETON_LIMIT)
            ]
            singletons = set(singletons)
            self.assertLessEqual(len(singletons), SINGLETON_LIMIT)

        self.assertAlmostEqual(len(singletons), SINGLETON_LIMIT)
Ejemplo n.º 9
0
    def test_subclass(self):
        @Inject(container=CDIContainer)
        class WithContainer(object):
            def __init__(self, container):
                self.cdi = container

        class Subclass(WithContainer):
            def __init__(self, *args, **kwargs):
                super(Subclass, self).__init__(*args, **kwargs)

        obj = DEFAULT_CONTAINER.produce(Subclass)
        self.assertIsNotNone(obj.cdi)
        self.assertIsInstance(obj.cdi, CDIContainer)
Ejemplo n.º 10
0
    def test_with_context_args(self):
        DEFAULT_CONTAINER.register_instance(1, int, context='1')
        DEFAULT_CONTAINER.register_instance(2, int, context='2')
        DEFAULT_CONTAINER.register_instance(3, int, context='3')

        @Inject(int, _context='3')
        @Inject(int, _context='2')
        @Inject(int, _context='1')
        def sum_func(*args):
            for i, arg in zip(range(1, len(args) + 1), args):
                self.assertEqual(i, arg)
            return sum(args)

        self.assertEqual(6, call(sum_func))
        self.assertEqual(10, call(sum_func, 4))
Ejemplo n.º 11
0
 def test_priority(self):
     self.assertEqual(DEFAULT_CONTAINER.produce(int), 0)
     self.assertEqual(list(DEFAULT_CONTAINER.produce([int])), [0, 1])
Ejemplo n.º 12
0
 def test_unique(self):
     obj1 = DEFAULT_CONTAINER.produce(SingletonClass)
     obj2 = DEFAULT_CONTAINER.produce(SingletonClass)
     self.assertEqual(obj1, obj2)
     self.assertEqual(obj1.index, obj2.index)
Ejemplo n.º 13
0
 def setUp(self):
     self.container = DEFAULT_CONTAINER.sub_container()
Ejemplo n.º 14
0
 def test_simple_class(self):
     frc = WithForwardReferenceClass()
     DEFAULT_CONTAINER.register_instance(frc)
     self.assertIs(frc, call(frc.kwargs_inject))
Ejemplo n.º 15
0
 def test_method_with_injection(self):
     DEFAULT_CONTAINER.register_instance(self)
     complex_class = new(ComplexClass)
     call(complex_class.method_with_injection)
Ejemplo n.º 16
0
 def test_with_subcontainer(self):
     container = DEFAULT_CONTAINER.sub_container()
     container.register_instance(2, priority=0)
     self.assertEqual(list(container.produce([int])), [0, 2, 1])
     container.register_instance(-1)
     self.assertEqual(list(container.produce([int])), [-1, 0, 1])
Ejemplo n.º 17
0
    def test_simple_func(self):
        @Inject(_name_as_context=True)
        def sub_func(a: int, b: int) -> int:
            return a - b

        DEFAULT_CONTAINER.register_instance(4, int, context='a')
        DEFAULT_CONTAINER.register_instance(2, int, context='b')
        self.assertEqual(2, call(sub_func))

        DEFAULT_CONTAINER.register_instance(2, int, context='a')
        DEFAULT_CONTAINER.register_instance(2, int, context='b')
        self.assertEqual(0, call(sub_func))

        DEFAULT_CONTAINER.register_instance(2, int, context='a')
        DEFAULT_CONTAINER.register_instance(4, int, context='b')
        self.assertEqual(-2, call(sub_func))
Ejemplo n.º 18
0
    def test_inheritance(self):
        test_case = self
        DEFAULT_CONTAINER.register_instance('inheritance', str)
        DEFAULT_CONTAINER.register_instance({}, dict)
        DEFAULT_CONTAINER.register_instance([], list)

        @Inject(str, some_object=object)
        class A(object):
            def __init__(self, some_str, some_object):
                test_case.assertIsInstance(some_str, str)
                test_case.assertIsInstance(some_object, object)

        @Inject(some_object=object, some_dict=dict)
        class B(A):
            def __init__(self, some_str, some_object, some_dict):
                test_case.assertIsInstance(some_str, str)
                test_case.assertIsInstance(some_object, object)
                test_case.assertIsInstance(some_dict, dict)

        @Inject(some_object=object, some_list=list)
        class C(A):
            def __init__(self, some_str, some_object, some_list):
                test_case.assertIsInstance(some_str, str)
                test_case.assertIsInstance(some_object, object)
                test_case.assertIsInstance(some_list, list)

        DEFAULT_CONTAINER.call(A)
        DEFAULT_CONTAINER.call(B)
        DEFAULT_CONTAINER.call(C)