def test_singleton(): ioc = IocManager() ioc.set_class(cls=ClassA) ioc.set_class(cls=ClassB, singleton=True) assert ioc.ClassA != ioc.ClassA assert ioc.ClassB == ioc.ClassB
def test_singleton_container(): ioc = IocManager(stats=True) ioc.set_class(name='singleton1', cls=TSingleton1, singleton=True) assert ioc.singleton1 is ioc.singleton1 ioc.print_stats()
def test_arguments(): ioc = IocManager() ioc.set_value(name='value_text', value='Some text') ioc.set_value(name='value_class', value=ClassA()) ioc.set_class(cls=ExampleClass) assert ioc.ExampleClass.__class__ == ExampleClass
def test_not_inject(): ioc = IocManager() ioc.set_class(cls=ClassA) ioc.set_class(cls=ClassB) ioc.set_class(cls=ExampleClass) assert ioc.ExampleClass.__class__ == ExampleClass
def test_singleton_args_diff_container(): ioc = IocManager(stats=True) ioc.set_class(name='singleton2', cls=TSingleton2, singleton=True) ioc.set_class(name='Parent1', cls=Parent1, singleton=True) assert ioc.singleton2 is not None ioc.print_stats()
def test_singleton_args_diff2_container(): print('TSingleton2=%s (%s)' % (TSingleton2.__name__, TSingleton2)) print('example_func=%s (%s)' % (example_func.__name__, example_func)) ioc = IocManager(stats=True) ioc.set_class(name='singleton2', cls=TSingleton2, singleton=True) ioc.set_class(name='Parent1', cls=Parent1, singleton=True) assert ioc.singleton2 is not None ioc.print_stats()
def test_singleton_args_container(): stats = {} ioc = IocManager(stats) ioc.set_class(name='TSingleton1', cls=TSingleton1, singleton=True) ioc.set_class(name='singleton2', cls=TSingleton2, singleton=True) assert ioc.singleton2 is ioc.singleton2 assert ioc.TSingleton1 is ioc.TSingleton1 assert ioc.singleton2.ts is ioc.TSingleton1 ioc.print_stats()
def test_multithread(): ioc = IocManager(stats=True) ioc.set_class(name='singleton1', cls=TSingleton1, singleton=True) ioc.set_class(name='singleton2', cls=TSingleton1, singleton=True, thread_local=True) assert ioc.singleton1 is ioc.singleton1 assert ioc.singleton2 is ioc.singleton2 storage = {} thread = threading.Thread(target=_set_vars(ioc, storage)) thread.start() thread.join() assert ioc.singleton1 is storage['singleton1'] assert ioc.singleton2 is not storage['singleton2']
def test_class_per_thread(): ioc = IocManager() ioc.set_class(name='singleton1', cls=ClassA, singleton=True) ioc.set_class(name='singleton2', cls=ClassA, singleton=True, thread_local=True) assert ioc.singleton1 == ioc.singleton1 assert ioc.singleton2 == ioc.singleton2 thread_storage = {} thread = threading.Thread(target=_set_vars(ioc, thread_storage)) thread.start() thread.join() assert ioc.singleton1 == thread_storage['singleton1'] assert ioc.singleton2 != thread_storage['singleton2']
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()
def test_arguments(): ioc = IocManager() ioc.set_class(cls=ClassA) ioc.set_class(name='arg2', cls=ClassB) ioc.set_class(name='arg3', cls=ClassC) ioc.set_class(cls=ExampleClass) assert ioc.ExampleClass.__class__ == ExampleClass
# noinspection PyUnusedLocal class ExampleClass2: def __init__(self, arg1: ClassC): pass # noinspection PyUnusedLocal class ExampleClass3(ExampleClass2): def __init__(self, **kwargs): super().__init__(**kwargs) ioc = IocManager() ioc.set_class(cls=ClassA) ioc.set_class(cls=ClassB) # noinspection PyStatementEffect def test_exception_container_not_defined(): with pytest.raises(AttributeError) as e: ioc.NotExists assert e.value.args[0] == "Name 'NotExists' does not exist" # noinspection PyStatementEffect def test_exception_missing_not_inject(): with pytest.raises(TypeError) as e: ioc.set_class(cls=ExampleClass1) ioc.ExampleClass1