async def test_cached(): builder = ContainerBuilder() builder.register_class(Foo).as_interface(IFoo).per_lifetime() container = builder.build() s1 = await container.resolve(IFoo) s2 = await container.resolve(IFoo) assert s1 is s2
async def test_always_new(): builder = ContainerBuilder() builder.register_class(Foo).as_interface(IFoo).always_new() container = builder.build() s1 = await container.resolve(IFoo) s2 = await container.resolve(IFoo) assert s1 is not s2
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_single_instance(): builder = ContainerBuilder() builder.register_class(Foo).as_interface(IFoo) builder.register_class(Singleton).single_instance() container = builder.build() s1 = await container.resolve(Singleton) s2 = await container.resolve(Singleton) assert s1 is s2
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_nesting_no_singleton(): builder = ContainerBuilder() builder.register_class(Foo).as_interface(IFoo) builder.register_class(Singleton).single_instance() container = builder.build() nested = container.create_nested() s1 = await container.resolve(IFoo) s2 = await nested.resolve(IFoo) assert s1 is not s2
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_factory_2(): builder = ContainerBuilder() builder.register_class(Foo) builder.register_class(Bar).per_lifetime() container = builder.build() foo = await container.resolve(Foo) b1 = await foo.get_bar() b2 = await foo.get_bar() assert b1 is b2
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_on_the_fly_instance(): org_foo = Foo() builder = ContainerBuilder() builder.register_class(Bar).as_interface(BarInterface) container = builder.build() with pytest.raises(NotRegistered): await container.resolve(BarInterface) container.add_instance(org_foo) await container.resolve(BarInterface)
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_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_broken_constructor(): builder = ContainerBuilder() builder.register_class(BrokenConstructor) container = builder.build() with pytest.raises(NotAnnotatedConstructorParam): await container.resolve(BrokenConstructor)