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 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 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)
def _get_provider(targets: List[Target[InjectedT]], parent_context: InjectionContext) -> Optional[Provider[InjectedT]]: for target_index, target in enumerate(targets): context = parent_context.get_child_context(target, allow_jit_provider=target_index == len(targets) - 1) try: return context.get_provider() except NoBindingFound: pass return None
def _get_provider( targets: List[Target[InjectedT]], parent_context: InjectionContext) -> Optional[Provider[InjectedT]]: for target in targets: context = parent_context.get_child_context(target) try: return context.get_provider() except NoBindingFound: pass return None
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)
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_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))
class TestProviderBindingToProviderAdapter(unittest.TestCase): def setUp(self): self.adapter = ProviderBindingToProviderAdapter() self.state = InjectionState( create_autospec(ProviderCreator, spec_set=True), create_autospec(BindingRegistry, spec_set=True), ) self.provider = create_autospec(Provider, spec_set=True) self.instance = MyType() self.provider.get.return_value = self.instance self.provider_provider = create_autospec(Provider, spec_set=True) self.provider_provider.get.return_value = self.provider self.mock_scope_provider = create_autospec(Provider, spec_set=True) self.scope = PerLookupScope() self.mock_scope_provider.get.return_value = self.scope self.context = InjectionContext(Target(MyType), self.state) def test_accept_provider_binding_returns_true(self): self.assertTrue( self.adapter.accept( ProviderBinding(MyType, create_autospec(Provider, spec_set=True)), self.context)) def test_accept_non_provider_binding_returns_false(self): self.assertFalse(self.adapter.accept(SelfBinding(MyType), self.context)) self.assertFalse( self.adapter.accept(InstanceBinding(MyType, MyType()), 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) 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) 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_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)
class TestSelfBindingToProviderAdapter(unittest.TestCase): 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_accept_self_binding_returns_true(self): self.assertTrue(self.adapter.accept(SelfBinding(MyType), self.context)) 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_without_args(self): self.state.provider_creator.get_provider.return_value = self.mock_scope_provider provider = self.adapter.create(RegisteredBinding(SelfBinding(MyType)), self.context) self.state.provider_creator.get_provider.assert_called_once_with( self.context.get_child_context(Target(SingletonScope)), ) self.assertIsInstance(provider, FromClassProvider) instance = provider.get() self.assertIsInstance(instance, MyType) def test_create_provider_with_default_constructor(self): class MyOtherType: pass self.state.provider_creator.get_provider.return_value = self.mock_scope_provider provider = self.adapter.create( RegisteredBinding(SelfBinding(MyOtherType)), self.context) self.state.provider_creator.get_provider.assert_called_once_with( self.context.get_child_context(Target(SingletonScope)), ) self.assertIsInstance(provider, FromClassProvider) instance = provider.get() self.assertIsInstance(instance, MyOtherType) def test_create_scoped_provider(self): mock_scope_provider = create_autospec(Provider, spec_set=True) mock_scope_provider.get.return_value = ThreadScope() self.state.provider_creator.get_provider.return_value = mock_scope_provider provider = self.adapter.create( RegisteredBinding(SelfBinding(MyType, scope=ThreadScope)), self.context) self.state.provider_creator.get_provider.assert_called_once_with( self.context.get_child_context(Target(ThreadScope))) self.assertIsInstance(provider, ThreadScopedProvider) instance = provider.get() self.assertIsInstance(instance, MyType) def test_create_provider_with_parameters(self): class MyOtherType: def __init__(self, arg_1: str, arg_2: int, *args: float, arg_3: bool, **kwargs): self.args = [arg_1, arg_2, *args] self.kwargs = { "arg_3": arg_3, **kwargs, } mock_provider_1 = create_autospec(Provider) mock_provider_1.get.return_value = "my_arg_1" mock_provider_2 = create_autospec(Provider) mock_provider_2.get.return_value = 2 mock_provider_3 = create_autospec(Provider) mock_provider_3.get.return_value = [1.2, 3.4] mock_provider_4 = create_autospec(Provider) mock_provider_4.get.return_value = True self.state.provider_creator.get_provider.side_effect = [ mock_provider_1, mock_provider_2, mock_provider_3, mock_provider_4, self.mock_scope_provider, ] provider = self.adapter.create( RegisteredBinding(SelfBinding(MyOtherType)), self.context) instance = provider.get() self.assertIsInstance(instance, MyOtherType) self.assertEqual(["my_arg_1", 2, 1.2, 3.4], instance.args) self.assertEqual({"arg_3": True}, instance.kwargs) self.assertEqual([ call(self.context.get_child_context(Target(str, "arg_1"))), call(self.context.get_child_context(Target(int, "arg_2"))), call(self.context.get_child_context(Target(List[float], "args"))), call(self.context.get_child_context(Target(bool, "arg_3"))), call(self.context.get_child_context(Target(SingletonScope))), ], self.state.provider_creator.get_provider.call_args_list) def test_create_provider_from_named_binding(self): class MyOtherType: def __init__(self, arg: str): self.arg = arg mock_provider_1 = create_autospec(Provider) mock_provider_1.get.return_value = "my_arg_1" self.state.provider_creator.get_provider.side_effect = [ NoBindingFound, mock_provider_1, self.mock_scope_provider, ] provider = self.adapter.create( RegisteredBinding(SelfBinding(MyOtherType, named="my_name")), self.context) instance = provider.get() self.assertIsInstance(instance, MyOtherType) self.assertEqual("my_arg_1", instance.arg) self.assertEqual([ call(self.context.get_child_context(Target(str, "arg"))), call(self.context.get_child_context(Target(str))), call(self.context.get_child_context(Target(SingletonScope))), ], self.state.provider_creator.get_provider.call_args_list) def test_create_provider_with_named_positional_argument(self): class MyOtherType: @named_arg("arg", "my_name") def __init__(self, arg: str): self.arg = arg mock_provider_1 = create_autospec(Provider) mock_provider_1.get.return_value = "my_arg_1" self.state.provider_creator.get_provider.side_effect = [ mock_provider_1, self.mock_scope_provider, ] provider = self.adapter.create( RegisteredBinding(SelfBinding(MyOtherType)), self.context) instance = provider.get() self.assertIsInstance(instance, MyOtherType) self.assertEqual("my_arg_1", instance.arg) self.assertEqual([ call(self.context.get_child_context(Target(str, "my_name"))), call(self.context.get_child_context(Target(SingletonScope))), ], self.state.provider_creator.get_provider.call_args_list) def test_create_provider_with_named_args(self): class MyOtherType: @named_arg("arg", "my_name") def __init__(self, *arg: str): self.arg = arg mock_provider_1 = create_autospec(Provider) mock_provider_1.get.return_value = ["my_arg_1"] self.state.provider_creator.get_provider.side_effect = [ mock_provider_1, self.mock_scope_provider, ] provider = self.adapter.create( RegisteredBinding(SelfBinding(MyOtherType)), self.context) instance = provider.get() self.assertIsInstance(instance, MyOtherType) self.assertEqual(("my_arg_1", ), instance.arg) self.assertEqual([ call(self.context.get_child_context(Target(List[str], "my_name"))), call(self.context.get_child_context(Target(SingletonScope))), ], self.state.provider_creator.get_provider.call_args_list) def test_create_provider_with_missing_parameters_raises_exception(self): class MyOtherType: def __init__(self, arg: str) -> None: self.arg = arg self.state.provider_creator.get_provider.side_effect = NoBindingFound with self.assertRaises(NonInjectableTypeError): self.adapter.create(RegisteredBinding(SelfBinding(MyOtherType)), self.context) def test_create_provider_with_default_parameter(self): class MyOtherType: def __init__(self, arg: str = "default_value") -> None: self.arg = arg self.state.provider_creator.get_provider.side_effect = [ NoBindingFound, NoBindingFound, self.mock_scope_provider, ] provider = self.adapter.create( RegisteredBinding(SelfBinding(MyOtherType)), self.context) instance = provider.get() self.assertEqual(instance.arg, "default_value") def test_create_provider_without_type_hint(self): class MyOtherType: def __init__(self, arg="default_value") -> None: self.arg = arg self.state.provider_creator.get_provider.side_effect = [ self.mock_scope_provider, ] provider = self.adapter.create( RegisteredBinding(SelfBinding(MyOtherType)), self.context) instance = provider.get() self.assertEqual(instance.arg, "default_value") def test_non_injectable_scope_raises_exception(self): self.state.provider_creator.get_provider.side_effect = NoBindingFound() with self.assertRaises(NonInjectableTypeError): self.adapter.create(RegisteredBinding(SelfBinding(MyType)), self.context)