Exemplo n.º 1
0
 def test_accept_non_instance_binding_returns_false(self):
     self.assertFalse(self.adapter.accept(SelfBinding(MyType),
                                          self.context))
     self.assertFalse(
         self.adapter.accept(
             ProviderBinding(MyType, create_autospec(Provider)),
             self.context))
    def test_non_injectable_scope_raises_exception(self):
        self.state.provider_creator.get_provider.side_effect = [
            self.provider_provider,
            NoBindingFound(),
        ]

        with self.assertRaises(NonInjectableTypeError):
            self.adapter.create(RegisteredBinding(ProviderBinding(MyType, Provider)), self.context)
    def test_create_returns_provider_from_provider_instance_binding(self):
        provider = self.adapter.create(
            RegisteredBinding(ProviderBinding(MyType, self.provider)),
            self.context)

        instance = provider.get()
        self.assertIs(instance, self.instance)
        self.assertEqual(
            [], self.state.provider_creator.get_provider.call_args_list)
Exemplo n.º 4
0
    def test_create_creates_provider_for_provider_binding(self):
        provider = create_autospec(Provider, spec_set=True)
        provider.get.return_value = MyType()
        binding = ProviderBinding(MyType, provider)
        self.binding_registry.get_binding.return_value = RegisteredBinding(
            binding)
        self.binding_registry.__contains__.return_value = True
        self.provider_creator.get_provider.return_value = self.mock_scope_provider

        provider = self.provider_factory.create(self.context)
        instance = provider.get()
        self.assertIsInstance(instance, MyType)
    def test_create_from_provider_binding(self):
        provider = create_autospec(Provider, spec_set=True)
        instance = MyType()
        provider.get.return_value = instance
        binding = RegisteredMultiBinding(MultiBinding(MyType, [ItemBinding(bound_provider=provider)]), item_bindings=[
            RegisteredBinding(ProviderBinding(MyType, provider))
        ])

        provider = self.adapter.create(binding, self.context)

        list_instance = provider.get()
        self.assertEqual([instance], list_instance)
    def test_accept_non_self_binding_returns_false(self):
        class MySubType(MyType):
            pass

        self.assertFalse(
            self.adapter.accept(InstanceBinding(MyType, MyType()),
                                self.context))
        self.assertFalse(
            self.adapter.accept(ClassBinding(MyType, MySubType), self.context))
        self.assertFalse(
            self.adapter.accept(
                ProviderBinding(MyType, create_autospec(Provider)),
                self.context))
    def test_create_provider_from_provider_class_binding(self):
        self.state.provider_creator.get_provider.side_effect = [
            self.provider_provider,
            self.mock_scope_provider,
        ]

        provider = self.adapter.create(RegisteredBinding(ProviderBinding(MyType, Provider)), self.context)

        instance = provider.get()
        self.assertIs(instance, self.instance)
        self.assertEqual([
            call(self.context.get_child_context(Target(Provider))),
            call(self.context.get_child_context(Target(SingletonScope))),
        ], self.state.provider_creator.get_provider.call_args_list)
    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)
Exemplo n.º 9
0
    def test_provider_injection_from_provider_binding(self):
        class MyParentClass:
            def __init__(self, my_param: Provider[MyClass]):
                self.my_param = my_param

        class MyProvider(Provider[MyClass]):
            def get(self) -> MyClass:
                return MyClass()

        provider = MyProvider()
        injector = Injector(bindings=[
            ProviderBinding(MyClass, provider),
            SelfBinding(MyParentClass)
        ])
        parent = injector.inject(MyParentClass)
        self.assertIsInstance(parent, MyParentClass)
        self.assertIs(parent.my_param, provider)
        instance = parent.my_param.get()
        self.assertIsInstance(instance, MyClass)
Exemplo n.º 10
0
    def test_provider_instance_injection(self):
        class MyParent:
            def __init__(self, my_arg: MyClass, my_str: str):
                self.my_arg = my_arg
                self.my_str = my_str

        class MyParentProvider(Provider[MyParent]):
            def __init__(self, my_arg: MyClass):
                self.my_arg = my_arg

            def get(self) -> MyParent:
                return MyParent(self.my_arg, "hello")

        injector = Injector(bindings=[
            ProviderBinding(MyParent, MyParentProvider(MyClass())),
        ])
        my_parent = injector.inject(MyParent)
        self.assertIsInstance(my_parent.my_arg, MyClass)
        self.assertEqual("hello", my_parent.my_str)
    def test_create_scoped_provider(self):
        self.state.provider_creator.get_provider.side_effect = [
            self.provider_provider,
            FromInstanceProvider(ThreadScope()),
        ]

        provider = self.adapter.create(
            RegisteredBinding(ProviderBinding(MyType, Provider, scope=ThreadScope)),
            self.context,
        )

        instance = provider.get()
        self.assertIs(instance, self.instance)
        self.assertEqual(
            [
                call(self.context.get_child_context(Target(Provider))),
                call(self.context.get_child_context(Target(ThreadScope))),
            ],
            self.state.provider_creator.get_provider.call_args_list,
        )
        self.assertIsInstance(provider, ThreadScopedProvider)
 def test_accept_provider_binding_returns_true(self):
     self.assertTrue(
         self.adapter.accept(
             ProviderBinding(MyType, create_autospec(Provider,
                                                     spec_set=True)),
             self.context))