コード例 #1
0
 def test_register_saves_binding_to_new_type(self):
     self.binding_registry.register(self.my_type_binding)
     self.binding_registry.register(self.my_type_named_binding)
     self.binding_registry.register(self.other_type_binding)
     self.assertEqual(
         {
             FrozenTarget(MyType): self.my_type_binding,
             FrozenTarget(MyType, "my_name"): self.my_type_named_binding,
             FrozenTarget(OtherType): self.other_type_binding,
         }, self.binding_registry.get_bindings_by_target())
コード例 #2
0
    def test_bind_class_to_another_class(self):
        self.module.bind(MyType, OtherType)

        self.assertEqual(
            {
                FrozenTarget(MyType):
                RegisteredBinding(ClassBinding(MyType, OtherType)),
                FrozenTarget(OtherType):
                RegisteredBinding(SelfBinding(OtherType)),
            }, self.module.binding_registry.get_bindings_by_target())
コード例 #3
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())
コード例 #4
0
    def test_bind_multiple_overrides_binding(self):
        self.module.bind(MyType, to_instance=self.my_instance)
        self.module.bind(MyType, OtherType)

        self.assertEqual(
            {
                FrozenTarget(MyType):
                RegisteredBinding(ClassBinding(MyType, OtherType)),
                FrozenTarget(OtherType):
                RegisteredBinding(SelfBinding(OtherType)),
            }, self.module.binding_registry.get_bindings_by_target())
コード例 #5
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())
コード例 #6
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)
コード例 #7
0
    def test_install(self):
        class OtherModule(Module):
            def configure(self) -> None:
                self.bind(MyType)
                self.bind(OtherType, named="my_name")

        module = OtherModule()
        self.module.install(module)
        self.assertEqual(
            {
                FrozenTarget(MyType):
                RegisteredBinding(SelfBinding(MyType)),
                FrozenTarget(OtherType, "my_name"):
                RegisteredBinding(SelfBinding(OtherType, named="my_name")),
            }, self.module.binding_registry.get_bindings_by_target())
コード例 #8
0
    def test_bind_class_to_itself(self):
        self.module.bind(MyType)

        self.assertEqual(
            {
                FrozenTarget(MyType): RegisteredBinding(SelfBinding(MyType)),
            }, self.module.binding_registry.get_bindings_by_target())
コード例 #9
0
 def test_bind_provider_class(self):
     self.module.bind(MyType,
                      to_provider=MyProvider,
                      scope=PerLookupScope,
                      named="my_name")
     self.assertEqual(
         {
             FrozenTarget(MyType, "my_name"):
             RegisteredBinding(
                 ProviderBinding(MyType, MyProvider, PerLookupScope,
                                 "my_name")),
             FrozenTarget(MyProvider, "my_name"):
             RegisteredBinding(
                 SelfBinding(
                     MyProvider, scope=PerLookupScope, named="my_name")),
         }, self.module.binding_registry.get_bindings_by_target())
コード例 #10
0
 def test_bind_with_scope(self):
     self.module.bind(MyType, scope=PerLookupScope)
     self.assertEqual(
         {
             FrozenTarget(MyType):
             RegisteredBinding(SelfBinding(MyType, PerLookupScope)),
         }, self.module.binding_registry.get_bindings_by_target())
コード例 #11
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())
コード例 #12
0
    def test_get_binding_from_string_with_name_conflict_raises_exception(self):
        class MyNewType:
            pass

        binding_1 = create_autospec(Binding, spec_set=True)
        binding_1.target = FrozenTarget(MyNewType)

        # pylint: disable=function-redefined
        class MyNewType:
            pass

        binding_2 = create_autospec(Binding, spec_set=True)
        binding_2.target = FrozenTarget(MyNewType)

        self.binding_registry.register(RegisteredBinding(binding_1))
        self.binding_registry.register(RegisteredBinding(binding_2))

        with self.assertRaises(NonInjectableTypeError):
            self.binding_registry.get_binding(Target("MyNewType"))
コード例 #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())
コード例 #14
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())
コード例 #15
0
    def test_decorator_enables_module_if_variable_is_set_to_expected_value(self):
        os.environ[self.env_var_name] = "true"

        @conditional_on_env_var(self.env_var_name, expected_value="true")
        class MyModule(Module):
            def configure(self) -> None:
                self.bind(MyType)

        module = MyModule()
        module.configure_once()
        self.assertEqual({FrozenTarget(MyType): ANY}, module.binding_registry.get_bindings_by_target())
コード例 #16
0
 def get_binding(self, target: Target[InjectedT]) -> Optional[RegisteredBinding]:
     if isinstance(target.type, str):
         possible_target_types = list(set(
             available_target.type
             for available_target in self._bindings_by_target
             if isinstance(available_target.type, type)
             and available_target.type.__name__ == target.type
         ))
         if len(possible_target_types) == 1:
             target.type = possible_target_types[0]
         elif possible_target_types:
             raise NonInjectableTypeError(
                 f"Could not find binding for '{target.type}': multiple types with this name found")
     frozen_target = FrozenTarget(target.type, target.named)
     return self._bindings_by_target.get(frozen_target)
コード例 #17
0
    def test_multi_binding_default_parameters(self):
        self.module.multi_bind(
            MyType,
            [
                self.module.bind_item(MyType),
            ],
        )

        self.assertEqual(
            {
                FrozenTarget(List[MyType]):
                RegisteredMultiBinding(
                    MultiBinding(
                        MyType,
                        [
                            ItemBinding(MyType),
                        ],
                        SingletonScope,
                        None,
                        True,
                    ),
                    item_bindings=[RegisteredBinding(SelfBinding(MyType), )]),
            }, self.module.binding_registry.get_bindings_by_target())
コード例 #18
0
 def target(self) -> FrozenTarget[InjectedT]:
     return FrozenTarget(self.target_type, self.named)