def test_factory_container(): ioc = IocManager(stats=True) ioc.set_class(name='TSingleton1', cls=TSingleton1, singleton=True) ioc.set_class(name='TSingleton2', cls=TSingleton2, singleton=False) ioc.set_factory(name='TSingleton3', cls=MyIocFactory) ioc.set_class(name='TSingleton3dot1', cls=TSingleton3dot1, singleton=False) ioc.set_class(name='TSingleton3dot2', cls=TSingleton3dot2, singleton=False) ioc.set_class(name='TSingleton3dot3', cls=TSingleton3dot3, singleton=False) assert ioc.TSingleton1 is ioc.TSingleton1 ts3 = ioc.TSingleton3 assert isinstance(ts3, TSingleton3dot1) ioc.print_stats()
class Factory(IocFactory): @staticmethod def get_instance(ioc_manager: IocManager, name: str, frame_info: inspect.FrameInfo): if frame_info.function == 'test_factory_1': return ioc_manager.ClassA if name == 'factory1': return ioc_manager.ClassB return ioc_manager.ClassC ioc = IocManager() ioc.set_class(cls=ClassA) ioc.set_class(cls=ClassB) ioc.set_class(cls=ClassC) ioc.set_factory(name='factory1', cls=Factory) ioc.set_factory(name='factory2', cls=Factory) def test_factory_1(): assert ioc.factory1.__class__ == ClassA assert ioc.factory2.__class__ == ClassA def test_factory_2(): assert ioc.factory1.__class__ == ClassB assert ioc.factory2.__class__ == ClassC