コード例 #1
0
ファイル: test_injection.py プロジェクト: illuin-tech/opyoid
    def test_named_list(self):
        class Class1:
            @named_arg("my_param", "type_1")
            @named_arg("my_other_param", "type_2")
            def __init__(self, my_param: List[str], my_other_param: List[str],
                         my_default_param: List[str]):
                self.my_param = my_param
                self.my_other_param = my_other_param
                self.my_default_param = my_default_param

        injector = Injector(bindings=[
            SelfBinding(Class1),
            MultiBinding(str, [
                ItemBinding(bound_instance="my_type_1"),
            ],
                         named="type_1"),
            MultiBinding(str, [
                ItemBinding(bound_instance="my_type_2"),
            ],
                         named="type_2"),
            MultiBinding(
                str,
                [
                    ItemBinding(bound_instance="my_default"),
                ],
            ),
        ])
        instance = injector.inject(Class1)
        self.assertIsInstance(instance, Class1)
        self.assertEqual(["my_type_1"], instance.my_param)
        self.assertEqual(["my_type_2"], instance.my_other_param)
        self.assertEqual(["my_default"], instance.my_default_param)
コード例 #2
0
ファイル: test_injection.py プロジェクト: illuin-tech/opyoid
 def test_list_injection_item_singletons(self):
     injector = Injector(bindings=[
         MultiBinding(MyClass, [ItemBinding(bound_class=MyClass)])
     ])
     list_instance = injector.inject(List[MyClass])
     tuple_instance = injector.inject(Tuple[MyClass])
     self.assertIs(list_instance[0], tuple_instance[0])
コード例 #3
0
ファイル: test_injection.py プロジェクト: illuin-tech/opyoid
 def test_list_direct_injection(self):
     class_list = Injector(bindings=[
         MultiBinding(MyClass, [ItemBinding(bound_class=MyClass)])
     ]).inject(List[MyClass])
     self.assertIsInstance(class_list, list)
     self.assertEqual(1, len(class_list))
     self.assertIsInstance(class_list[0], MyClass)
コード例 #4
0
ファイル: test_injection.py プロジェクト: illuin-tech/opyoid
 def test_set_direct_injection(self):
     class_set = Injector(bindings=[
         MultiBinding(MyClass, [ItemBinding(bound_class=MyClass)])
     ]).inject(Set[MyClass])
     self.assertIsInstance(class_set, set)
     self.assertEqual(1, len(list(class_set)))
     self.assertIsInstance(class_set.pop(), MyClass)
コード例 #5
0
    def test_inject_list_with_singleton_items(self):
        class SubClass1(MyClass):
            pass

        class SubClass2(MyClass):
            pass

        injector = Injector(bindings=[
            MultiBinding(MyClass,
                         [ItemBinding(SubClass1),
                          ItemBinding(SubClass2)])
        ])
        list_1 = injector.inject(List[MyClass])
        list_2 = injector.inject(List[MyClass])

        self.assertEqual(2, len(list_1))
        self.assertEqual(2, len(list_2))
        self.assertIsInstance(list_1[0], SubClass1)
        self.assertIs(list_1[0], list_2[0])
        self.assertIsInstance(list_1[1], SubClass2)
        self.assertIs(list_1[1], list_2[1])
コード例 #6
0
ファイル: test_injection.py プロジェクト: illuin-tech/opyoid
    def test_args_injection_with_multi_binding(self):
        class MyParentClass:
            def __init__(self, *my_param: MyClass):
                self.my_param = my_param

        injector = Injector(bindings=[
            SelfBinding(MyParentClass),
            MultiBinding(MyClass, [ItemBinding(bound_instance=MyClass())])
        ])
        parent = injector.inject(MyParentClass)
        self.assertIsInstance(parent, MyParentClass)
        self.assertIsInstance(parent.my_param, tuple)
        self.assertIsInstance(parent.my_param[0], MyClass)
コード例 #7
0
ファイル: test_injection.py プロジェクト: illuin-tech/opyoid
    def test_set_injection(self):
        class MyParentClass:
            def __init__(self, param: Set[MyClass]):
                self.param = param

        parent = Injector(bindings=[
            MultiBinding(MyClass, [ItemBinding(bound_class=MyClass)]),
            SelfBinding(MyParentClass)
        ]).inject(MyParentClass)
        self.assertIsInstance(parent, MyParentClass)
        self.assertIsInstance(parent.param, set)
        self.assertEqual(1, len(list(parent.param)))
        self.assertIsInstance(parent.param.pop(), MyClass)
コード例 #8
0
    def test_list_injection(self):
        class MyParentClass:
            def __init__(self, param: List[MyClass]):
                self.param = param

        parent = Injector(bindings=[
            MultiBinding(MyClass, [ItemBinding(MyClass)]),
            SelfBinding(MyParentClass)
        ]).inject(MyParentClass)
        self.assertIsInstance(parent, MyParentClass)
        self.assertIsInstance(parent.param, list)
        self.assertEqual(1, len(parent.param))
        self.assertIsInstance(parent.param[0], MyClass)