async def test_pyautofac(): builder = ContainerBuilder() builder.register_instance(Foo(2)) builder.register_class(Bar) builder.register_class(Zoo) container = builder.build() zoo = await container.resolve(Zoo) assert zoo.increment() == 5
async def test_pyautofac(): builder = ContainerBuilder() builder.register_instance(Foo(2)) builder.register_class(Bar) builder.register_class(Zoo) container = builder.build() zoo = await container.resolve(Zoo) assert zoo.increment() == 5
async def test_async_resource(): messages = [] builder = ContainerBuilder() builder.register_instance(messages).as_interface(list) builder.register_class(Foo).as_interface(IFoo) container = builder.build() instance = await container.resolve(IFoo) await container.dispose() assert messages == ['foo-init', 'foo-dispose']
async def test_async_resource_context_manager(): messages = [] builder = ContainerBuilder() builder.register_instance(messages).as_interface(list) builder.register_class(Singleton).single_instance() builder.register_class(Bar).single_instance() async with builder.build() as container: await container.resolve(Bar) assert messages == ['singleton-init', 'bar-init', 'bar-dispose', 'singleton-dispose']
async def test_async_resource(): messages = [] builder = ContainerBuilder() builder.register_instance(messages).as_interface(list) builder.register_class(Foo).as_interface(IFoo) container = builder.build() instance = await container.resolve(IFoo) await container.dispose() assert messages == ['foo-init', 'foo-dispose']
async def test_container_resolve(): org_foo = Foo() builder = ContainerBuilder() builder.register_instance(org_foo) builder.register_class(Bar).as_interface(BarInterface) container = builder.build() bar = await container.resolve(BarInterface) assert bar.foo is org_foo assert bar.return_1() == 1
async def test_async_resource_context_manager(): messages = [] builder = ContainerBuilder() builder.register_instance(messages).as_interface(list) builder.register_class(Singleton).single_instance() builder.register_class(Bar).single_instance() async with builder.build() as container: await container.resolve(Bar) assert messages == [ 'singleton-init', 'bar-init', 'bar-dispose', 'singleton-dispose' ]
async def test_parallel_resolve(): messages = [] builder = ContainerBuilder() builder.register_instance(messages).as_interface(list) builder.register_class(Singleton).single_instance() builder.register_class(Bar) async with builder.build() as container: async def resolve(): async with container.create_nested() as nested: await nested.resolve(Bar) await asyncio.gather(resolve(), resolve(), resolve()) assert len(messages) == 8 assert messages[0] == 'singleton-init' assert messages[-1] == 'singleton-dispose' assert sum(1 for msg in messages if msg == 'bar-init') == 3 assert sum(1 for msg in messages if msg == 'bar-dispose') == 3
async def test_async_resource_nested(): messages = [] builder = ContainerBuilder() builder.register_instance(messages).as_interface(list) builder.register_class(Singleton).single_instance() builder.register_class(Foo).as_interface(IFoo) container = builder.build() nested = container.create_nested() assert messages == [] await nested.resolve(Singleton) assert messages == ['singleton-init'] await nested.resolve(IFoo) assert messages == ['singleton-init', 'foo-init'] await nested.dispose() assert messages == ['singleton-init', 'foo-init', 'foo-dispose'] await container.dispose() assert messages == ['singleton-init', 'foo-init', 'foo-dispose', 'singleton-dispose']
async def test_parallel_resolve(): messages = [] builder = ContainerBuilder() builder.register_instance(messages).as_interface(list) builder.register_class(Singleton).single_instance() builder.register_class(Bar) async with builder.build() as container: async def resolve(): async with container.create_nested() as nested: await nested.resolve(Bar) await asyncio.gather(resolve(), resolve(), resolve()) assert len(messages) == 8 assert messages[0] == 'singleton-init' assert messages[-1] == 'singleton-dispose' assert sum(1 for msg in messages if msg == 'bar-init') == 3 assert sum(1 for msg in messages if msg == 'bar-dispose') == 3
async def test_async_resource_nested(): messages = [] builder = ContainerBuilder() builder.register_instance(messages).as_interface(list) builder.register_class(Singleton).single_instance() builder.register_class(Foo).as_interface(IFoo) container = builder.build() nested = container.create_nested() assert messages == [] await nested.resolve(Singleton) assert messages == ['singleton-init'] await nested.resolve(IFoo) assert messages == ['singleton-init', 'foo-init'] await nested.dispose() assert messages == ['singleton-init', 'foo-init', 'foo-dispose'] await container.dispose() assert messages == [ 'singleton-init', 'foo-init', 'foo-dispose', 'singleton-dispose' ]
async def test_container_wrong_instance(): builder = ContainerBuilder() with pytest.raises(NotSubclass): builder.register_instance(object()).as_interface(Foo)