Esempio n. 1
0
 def setUp(self):
     self.binding_registry = create_autospec(BindingRegistry, spec_set=True)
     self.provider_factory = FromBindingProviderFactory()
     self.provider_creator = create_autospec(ProviderCreator, spec_set=True)
     self.mock_scope_provider = create_autospec(Provider, spec_set=True)
     self.scope = PerLookupScope()
     self.mock_scope_provider.get.return_value = self.scope
     self.state = InjectionState(
         self.provider_creator,
         self.binding_registry,
     )
     self.context = InjectionContext(Target(MyType), self.state)
     self.str_context = InjectionContext(Target(str), self.state)
     self.int_context = InjectionContext(Target(int), self.state)
    def test_list_binding_with_named_arguments(self):
        self.binding_registry.register(
            RegisteredMultiBinding(
                MultiBinding(
                    MyType,
                    [
                        ItemBinding(bound_instance=self.named_instance),
                    ],
                    named="my_name",
                ),
                item_bindings=[
                    RegisteredBinding(InstanceBinding(MyType, self.named_instance, named="my_name"))
                ]
            )
        )
        self.binding_registry.register(
            RegisteredMultiBinding(
                MultiBinding(
                    MyType,
                    [
                        ItemBinding(bound_instance=self.my_instance),
                    ],
                ),
                item_bindings=[
                    RegisteredBinding(InstanceBinding(MyType, self.my_instance))
                ]
            )
        )
        self.binding_registry.register(RegisteredBinding(self.my_named_instance_binding))
        self.binding_registry.register(RegisteredBinding(SelfBinding(MyType)))

        context = InjectionContext(Target(List[MyType], "my_name"), self.state)
        provider = self.provider_creator.get_provider(context)
        list_instance = provider.get()
        self.assertEqual([self.named_instance], list_instance)
 def setUp(self) -> None:
     self.binding_registry = BindingRegistry()
     self.binding_registry.register(RegisteredBinding(InstanceBinding(SingletonScope, SingletonScope())))
     self.provider_creator = ProviderCreator()
     self.state = InjectionState(
         self.provider_creator,
         self.binding_registry,
     )
     self.context = InjectionContext(Target(MyType), self.state)
     self.other_context = InjectionContext(Target(MyOtherType), self.state)
     self.named_context = InjectionContext(Target(MyType, "my_name"), self.state)
     self.my_instance = MyType()
     self.my_instance_binding = InstanceBinding(MyType, self.my_instance)
     self.named_instance = MyType()
     self.my_named_instance_binding = InstanceBinding(MyType, self.named_instance, named="my_name")
     self.my_other_instance = MyOtherType()
     self.my_other_instance_binding = InstanceBinding(MyOtherType, self.my_other_instance)
 def test_list_implicit_binding(self):
     instance = MyType()
     self.binding_registry.register(RegisteredBinding(InstanceBinding(MyType, instance)))
     context = InjectionContext(Target(List[MyType]), self.state)
     provider = self.provider_creator.get_provider(context)
     self.assertIsInstance(provider, ListProvider)
     list_instance = provider.get()
     self.assertEqual([instance], list_instance)
    def test_optional_binding(self):
        self.binding_registry.register(RegisteredBinding(self.my_instance_binding))

        context = InjectionContext(Target(Optional[MyType]), self.state)
        provider = self.provider_creator.get_provider(context)
        self.assertIsInstance(provider, FromInstanceProvider)
        instance = provider.get()
        self.assertIs(self.my_instance, instance)
 def setUp(self):
     self.adapter = SelfBindingToProviderAdapter()
     self.state = InjectionState(
         create_autospec(ProviderCreator, spec_set=True),
         create_autospec(BindingRegistry, spec_set=True),
     )
     self.context = InjectionContext(Target(MyType), self.state)
     self.mock_scope_provider = create_autospec(Provider, spec_set=True)
     self.scope = PerLookupScope()
     self.mock_scope_provider.get.return_value = self.scope
    def test_missing_binding_raises_exception(self):
        class MyParentClass:
            def __init__(self, my_param: MyType):
                self.my_param = my_param

        my_parent_binding = SelfBinding(MyParentClass)
        context = InjectionContext(Target(MyParentClass), self.state)
        self.binding_registry.register(RegisteredBinding(my_parent_binding))

        with self.assertRaises(NonInjectableTypeError):
            self.provider_creator.get_provider(context)
    def test_type_binding_without_class_binding(self):
        class MyParentClass:
            def __init__(self, my_param: Type[MyType]):
                self.my_param = my_param

        parent_binding = SelfBinding(MyParentClass)
        self.binding_registry.register(RegisteredBinding(self.my_instance_binding))
        self.binding_registry.register(RegisteredBinding(parent_binding))
        context = InjectionContext(Target(Type[MyType]), self.state)

        with self.assertRaises(NonInjectableTypeError):
            self.provider_creator.get_provider(context)
    def test_type_binding_with_explicit_binding(self):
        class SubType(MyType):
            pass

        self.binding_registry.register(RegisteredBinding(InstanceBinding(Type[MyType], MyType)))
        self.binding_registry.register(RegisteredBinding(ClassBinding(MyType, SubType)))

        context = InjectionContext(Target(Type[MyType]), self.state)
        provider = self.provider_creator.get_provider(context)
        self.assertIsInstance(provider, FromInstanceProvider)
        instance = provider.get()
        self.assertIs(MyType, instance)
 def setUp(self):
     self.binding_registry = BindingRegistry()
     self.binding_registry.register(RegisteredBinding(InstanceBinding(SingletonScope, SingletonScope())))
     self.binding_registry.register(RegisteredBinding(InstanceBinding(ThreadScope, ThreadScope())))
     self.adapter = MultiBindingToProviderAdapter(
         FromRegisteredBindingProviderFactory()
     )
     self.my_instance = MyType()
     self.state = InjectionState(
         ProviderCreator(),
         self.binding_registry,
     )
     self.context = InjectionContext(Target(List[MyType]), self.state)
Esempio n. 11
0
    def test_provider_binding(self):
        class MyInjectee:
            pass

        class MyProvider(Provider[MyInjectee]):
            def __init__(self, my_param: MyType):
                self.my_param = my_param

            def get(self) -> MyInjectee:
                return MyInjectee()

        provider_binding = ProviderBinding(MyInjectee, MyProvider)
        self.binding_registry.register(RegisteredBinding(self.my_instance_binding))
        self.binding_registry.register(RegisteredBinding(provider_binding))
        context = InjectionContext(Target(MyInjectee), self.state)

        self.provider_creator.get_provider(context)
    def test_create_named_provider(self):
        self.state.provider_creator.get_provider.side_effect = [
            self.provider_provider,
            self.mock_scope_provider,
        ]

        context = InjectionContext(Target(MyType, "my_name"), self.state)
        provider = self.adapter.create(
            RegisteredBinding(ProviderBinding(MyType, Provider, named="my_name")),
            context,
        )

        instance = provider.get()
        self.assertIs(instance, self.instance)
        self.assertEqual([
            call(context.get_child_context(Target(Provider, "my_name"))),
            call(context.get_child_context(Target(SingletonScope))),
        ], self.state.provider_creator.get_provider.call_args_list)
Esempio n. 13
0
 def test_list_binding_with_multi_binding(self):
     self.binding_registry.register(
         RegisteredMultiBinding(
             MultiBinding(
                 MyType,
                 [
                     ItemBinding(bound_instance=self.my_instance),
                     ItemBinding(MyType),
                 ],
             ),
             item_bindings=[
                 RegisteredBinding(InstanceBinding(MyType,
                                                   self.my_instance)),
                 RegisteredBinding(SelfBinding(MyType)),
             ]))
     context = InjectionContext(Target(List[MyType]), self.state)
     provider = self.provider_creator.get_provider(context)
     list_instance = provider.get()
     self.assertEqual([self.my_instance, ANY], list_instance)
     self.assertIsInstance(list_instance[1], MyType)
Esempio n. 14
0
 def test_set_binding_with_multi_binding(self):
     self.binding_registry.register(
         RegisteredMultiBinding(
             MultiBinding(
                 MyType,
                 [
                     ItemBinding(bound_instance=self.my_instance),
                     ItemBinding(MyType),
                 ],
             ),
             item_bindings=[
                 RegisteredBinding(InstanceBinding(MyType,
                                                   self.my_instance)),
                 RegisteredBinding(SelfBinding(MyType)),
             ]))
     context = InjectionContext(Target(Set[MyType]), self.state)
     provider = self.provider_creator.get_provider(context)
     self.assertIsInstance(provider, FromClassProvider)
     set_instance = provider.get()
     self.assertIn(self.my_instance, set_instance)
     self.assertEqual(2, len(set_instance))