def test_deepcopy_kwargs(self): provider = providers.Coroutine(_example) dependent_provider1 = providers.Callable(list) dependent_provider2 = providers.Callable(dict) provider.add_kwargs(a1=dependent_provider1, a2=dependent_provider2) provider_copy = providers.deepcopy(provider) dependent_provider_copy1 = provider_copy.kwargs['a1'] dependent_provider_copy2 = provider_copy.kwargs['a2'] self.assertNotEqual(provider.kwargs, provider_copy.kwargs) self.assertIs(dependent_provider1.provides, dependent_provider_copy1.provides) self.assertIsNot(dependent_provider1, dependent_provider_copy1) self.assertIs(dependent_provider2.provides, dependent_provider_copy2.provides) self.assertIsNot(dependent_provider2, dependent_provider_copy2)
"""`Coroutine` providers example with async / await syntax. Current example works only fot Python 3.5+. """ import asyncio import dependency_injector.providers as providers async def coroutine_function(arg1, arg2): """Sample coroutine function.""" await asyncio.sleep(0.1) return arg1, arg2 coroutine_provider = providers.Coroutine(coroutine_function, arg1=1, arg2=2) if __name__ == '__main__': loop = asyncio.get_event_loop() arg1, arg2 = loop.run_until_complete(coroutine_provider()) assert (arg1, arg2) == (1, 2) assert asyncio.iscoroutinefunction(coroutine_provider)
class Container(containers.DeclarativeContainer): coroutine_provider = providers.Coroutine(coroutine, arg1=1, arg2=2)
def test_set_kwargs(): provider = providers.Coroutine(example) \ .add_kwargs(init_arg3=3, init_arg4=4) \ .set_kwargs(init_arg3=4, init_arg4=5) assert provider.kwargs == dict(init_arg3=4, init_arg4=5)
def test_init_with_coroutine(): assert isinstance(providers.Coroutine(example), providers.Coroutine)
async def test_call_with_context_args_and_kwargs(): provider = providers.Coroutine(example, 1) result = await provider(2, arg3=3, arg4=4) assert result == (1, 2, 3, 4)
async def test_call_with_positional_and_keyword_args(): provider = providers.Coroutine(example, 1, 2, arg3=3, arg4=4) result = await provider() assert result == (1, 2, 3, 4)
def test_set_provides_returns_self(): provider = providers.Coroutine() assert provider.set_provides(example) is provider
def test_call_with_positional_and_keyword_args(self): provider = providers.Coroutine(_example, 1, 2, arg3=3, arg4=4) self.assertTupleEqual(run(provider()), (1, 2, 3, 4))
def test_call_with_positional_args(self): provider = providers.Coroutine(_example, 1, 2, 3, 4) self.assertTupleEqual(self._run(provider()), (1, 2, 3, 4))
def test_init_with_coroutine(self): self.assertTrue(providers.Coroutine(_example))
def setUp(self): self.delegated = providers.Coroutine(_example) self.delegate = providers.CoroutineDelegate(self.delegated)
def test_repr(self): provider = providers.Coroutine(_example) self.assertEqual( repr(provider), '<dependency_injector.providers.' 'Coroutine({0}) at {1}>'.format(repr(_example), hex(id(provider))))
def test_clear_kwargs(self): provider = providers.Coroutine(_example) \ .add_kwargs(init_arg3=3, init_arg4=4) \ .clear_kwargs() self.assertEqual(provider.kwargs, dict())
def test_call_with_keyword_args(self): provider = providers.Coroutine(_example, arg1=1, arg2=2, arg3=3, arg4=4) self.assertTupleEqual(self._run(provider()), (1, 2, 3, 4))
def test_init_with_not_coroutine(): with raises(errors.Error): providers.Coroutine(lambda: None)
def test_repr(): provider = providers.Coroutine(example) assert repr(provider) == ( "<dependency_injector.providers." "Coroutine({0}) at {1}>".format(repr(example), hex(id(provider))) )
def test_call_with_context_args(self): provider = providers.Coroutine(_example, 1, 2) self.assertTupleEqual(self._run(provider(3, 4)), (1, 2, 3, 4))
async def test_call_with_keyword_args(): provider = providers.Coroutine(example, arg1=1, arg2=2, arg3=3, arg4=4) result = await provider() assert result == (1, 2, 3, 4)
def test_call_with_context_args_and_kwargs(self): provider = providers.Coroutine(_example, 1) self.assertTupleEqual( self._run(provider(2, arg3=3, arg4=4)), (1, 2, 3, 4), )
async def test_call_with_context_args(): provider = providers.Coroutine(example, 1, 2) result = await provider(3, 4) assert result == (1, 2, 3, 4)
def test_fluent_interface(self): provider = providers.Coroutine(_example) \ .add_args(1, 2) \ .add_kwargs(arg3=3, arg4=4) self.assertTupleEqual(self._run(provider()), (1, 2, 3, 4))
async def test_fluent_interface(): provider = providers.Coroutine(example) \ .add_args(1, 2) \ .add_kwargs(arg3=3, arg4=4) result = await provider() assert result == (1, 2, 3, 4)
def test_set_args(self): provider = providers.Coroutine(_example) \ .add_args(1, 2) \ .set_args(3, 4) self.assertEqual(provider.args, (3, 4))
def test_set_args(): provider = providers.Coroutine(example) \ .add_args(1, 2) \ .set_args(3, 4) assert provider.args == (3, 4)
def test_set_kwargs(self): provider = providers.Coroutine(_example) \ .add_kwargs(init_arg3=3, init_arg4=4) \ .set_kwargs(init_arg3=4, init_arg4=5) self.assertEqual(provider.kwargs, dict(init_arg3=4, init_arg4=5))
def test_clear_args(): provider = providers.Coroutine(example) \ .add_args(1, 2) \ .clear_args() assert provider.args == tuple()
def test_clear_args(self): provider = providers.Coroutine(_example) \ .add_args(1, 2) \ .clear_args() self.assertEqual(provider.args, tuple())
class WebApi(containers.DeclarativeContainer): """Web API container.""" config = providers.Configuration('web_api') domain: DomainModel = providers.DependenciesContainer() web_app = providers.Factory( webapi.app.create_web_app, base_path=config.base_path, routes=[ # Subscriptions webapi.app.route( method='GET', path='/newsfeed/{newsfeed_id}/subscriptions/', handler=providers.Coroutine( webapi.handlers.subscriptions.get_subscriptions_handler, subscription_service=domain.subscription_service, ), ), webapi.app.route( method='POST', path='/newsfeed/{newsfeed_id}/subscriptions/', handler=providers.Coroutine( webapi.handlers.subscriptions.post_subscription_handler, subscription_service=domain.subscription_service, ), ), webapi.app.route( method='DELETE', path='/newsfeed/{newsfeed_id}/subscriptions/{subscription_id}/', handler=providers.Coroutine( webapi.handlers.subscriptions.delete_subscription_handler, subscription_service=domain.subscription_service, ), ), webapi.app.route( method='GET', path='/newsfeed/{newsfeed_id}/subscribers/subscriptions/', handler=providers.Coroutine( webapi.handlers.subscriptions. get_subscriber_subscriptions_handler, subscription_service=domain.subscription_service, ), ), # Events webapi.app.route( method='GET', path='/newsfeed/{newsfeed_id}/events/', handler=providers.Coroutine( webapi.handlers.events.get_events_handler, event_repository=domain.event_repository, ), ), webapi.app.route( method='POST', path='/newsfeed/{newsfeed_id}/events/', handler=providers.Coroutine( webapi.handlers.events.post_event_handler, event_dispatcher_service=domain.event_dispatcher_service, ), ), webapi.app.route( method='DELETE', path='/newsfeed/{newsfeed_id}/events/{event_id}/', handler=providers.Coroutine( webapi.handlers.events.delete_event_handler, event_dispatcher_service=domain.event_dispatcher_service, ), ), # Miscellaneous webapi.app.route( method='GET', path='/status/', handler=providers.Coroutine( webapi.handlers.misc.get_status_handler, ), ), webapi.app.route( method='GET', path='/docs/', handler=providers.Coroutine( webapi.handlers.misc.get_openapi_schema_handler, base_path=config.base_path, ), ), ], )
def test_clear_kwargs(): provider = providers.Coroutine(example) \ .add_kwargs(init_arg3=3, init_arg4=4) \ .clear_kwargs() assert provider.kwargs == dict()