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)
Beispiel #2
0
 def test_inject_from_multiple_providers_takes_last(self):
     my_instance = MyType()
     my_instance_2 = MyType()
     injector = Injector(bindings=[
         InstanceBinding(MyType, my_instance),
         InstanceBinding(MyType, my_instance_2),
     ])
     self.assertIs(my_instance_2, injector.inject(MyType))
Beispiel #3
0
    def test_multi_binding(self):
        instance = MyType()
        provider = MyProvider
        self.module.multi_bind(
            MyType,
            [
                self.module.bind_item(to_class=MyType),
                self.module.bind_item(to_instance=instance),
                self.module.bind_item(to_provider=provider),
            ],
            scope=PerLookupScope,
            named="my_name",
            override_bindings=False,
        )

        self.assertEqual(
            {
                FrozenTarget(List[MyType], "my_name"):
                RegisteredMultiBinding(
                    MultiBinding(
                        MyType,
                        [
                            ItemBinding(bound_class=MyType),
                            ItemBinding(bound_instance=instance),
                            ItemBinding(bound_provider=provider),
                        ],
                        scope=PerLookupScope,
                        named="my_name",
                        override_bindings=False,
                    ),
                    item_bindings=[
                        RegisteredBinding(
                            SelfBinding(MyType,
                                        scope=PerLookupScope,
                                        named="my_name"), ),
                        RegisteredBinding(
                            InstanceBinding(MyType, instance,
                                            named="my_name"), ),
                        RegisteredBinding(
                            ProviderBinding(MyType,
                                            provider,
                                            scope=PerLookupScope,
                                            named="my_name"), )
                    ]),
                FrozenTarget(MyProvider, "my_name"):
                RegisteredBinding(
                    SelfBinding(
                        MyProvider, scope=PerLookupScope, named="my_name")),
                FrozenTarget(MyType, "my_name"):
                RegisteredBinding(
                    InstanceBinding(MyType, instance, named="my_name"))
            }, self.module.binding_registry.get_bindings_by_target())
Beispiel #4
0
 def test_register_multi_binding_saves_binding_to_known_type_in_order(self):
     item_binding_1 = ItemBinding(MyType)
     registered_item_binding_1 = RegisteredBinding(SelfBinding(MyType))
     item_binding_2 = ItemBinding(bound_instance=MyType())
     registered_item_binding_2 = RegisteredBinding(
         InstanceBinding(MyType, item_binding_2.bound_instance))
     binding_1 = RegisteredMultiBinding(MultiBinding(
         MyType, [item_binding_1]),
                                        item_bindings=[
                                            registered_item_binding_1,
                                        ])
     binding_2 = RegisteredMultiBinding(MultiBinding(
         MyType, [item_binding_2], override_bindings=False),
                                        item_bindings=[
                                            registered_item_binding_2,
                                        ])
     self.binding_registry.register(binding_1)
     self.binding_registry.register(binding_2)
     registered_binding = self.binding_registry.get_binding(
         Target(List[MyType]))
     self.assertIsInstance(registered_binding, RegisteredMultiBinding)
     self.assertIsInstance(registered_binding.raw_binding, MultiBinding)
     self.assertEqual(
         [registered_item_binding_1, registered_item_binding_2],
         registered_binding.item_bindings)
 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)
Beispiel #7
0
    def test_bind_instance(self):
        my_instance = MyType()
        self.module.bind(MyType, to_instance=my_instance)

        self.assertEqual(
            {
                FrozenTarget(MyType):
                RegisteredBinding(InstanceBinding(MyType, my_instance)),
            }, self.module.binding_registry.get_bindings_by_target())
Beispiel #8
0
 def test_bind_provider_instance(self):
     self.module.bind(MyType, to_provider=self.my_provider)
     self.assertEqual(
         {
             FrozenTarget(MyType):
             RegisteredBinding(ProviderBinding(MyType, self.my_provider)),
             FrozenTarget(MyProvider):
             RegisteredBinding(InstanceBinding(MyProvider,
                                               self.my_provider)),
         }, self.module.binding_registry.get_bindings_by_target())
Beispiel #9
0
    def test_bind_with_name(self):
        my_instance = MyType()
        self.module.bind(MyType, to_instance=my_instance)
        self.module.bind(MyType, named="my_name")
        my_other_instance = OtherType()
        self.module.bind(OtherType,
                         to_instance=my_other_instance,
                         named="my_other_name")

        self.assertEqual(
            {
                FrozenTarget(MyType):
                RegisteredBinding(InstanceBinding(MyType, my_instance)),
                FrozenTarget(MyType, "my_name"):
                RegisteredBinding(SelfBinding(MyType, named="my_name")),
                FrozenTarget(OtherType, "my_other_name"):
                RegisteredBinding(
                    InstanceBinding(OtherType, my_other_instance,
                                    "my_other_name")),
            }, self.module.binding_registry.get_bindings_by_target())
    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)
Beispiel #11
0
 def setUp(self) -> None:
     self.binding_registry = BindingRegistry()
     mock_binding = create_autospec(Binding, spec_set=True)
     mock_binding.target = FrozenTarget(MyType)
     self.my_type_binding = RegisteredBinding(mock_binding)
     mock_named_binding = create_autospec(Binding, spec_set=True)
     mock_named_binding.target = FrozenTarget(MyType, "my_name")
     self.my_type_named_binding = RegisteredBinding(mock_named_binding)
     self.my_type_binding_2 = RegisteredBinding(
         InstanceBinding(MyType, MyType()))
     other_mock_binding = create_autospec(Binding, spec_set=True)
     other_mock_binding.target = FrozenTarget(OtherType)
     self.other_type_binding = RegisteredBinding(other_mock_binding)
Beispiel #12
0
    def test_register_class_binding_does_not_create_self_binding_if_target_exists(
            self):
        class MySubType(MyType):
            pass

        my_instance = MySubType()
        class_binding = RegisteredBinding(ClassBinding(MyType, MySubType))
        instance_binding = RegisteredBinding(
            InstanceBinding(MySubType, my_instance))
        self.binding_registry.register(instance_binding)
        self.binding_registry.register(class_binding)
        self.assertIs(class_binding,
                      self.binding_registry.get_binding(Target(MyType)))
        self.assertIs(instance_binding,
                      self.binding_registry.get_binding(Target(MySubType)))
Beispiel #13
0
    def test_register_provider_binding_with_instance_creates_additional_binding(
            self):
        class MyProvider(Provider[str]):
            def get(self) -> str:
                return "hello"

        provider_instance = MyProvider()
        provider_binding = RegisteredBinding(
            ProviderBinding(str, provider_instance, named="my_name"))
        self.binding_registry.register(provider_binding)

        self.assertEqual(
            {
                FrozenTarget(str, "my_name"):
                provider_binding,
                FrozenTarget(MyProvider, "my_name"):
                RegisteredBinding(
                    InstanceBinding(MyProvider, provider_instance, "my_name")),
            }, self.binding_registry.get_bindings_by_target())
 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)
 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))
Beispiel #16
0
 def test_inject_from_binding(self):
     my_instance = MyType()
     injector = Injector(bindings=[
         InstanceBinding(MyType, my_instance)
     ])
     self.assertIs(my_instance, injector.inject(MyType))