def test_functions_positional_arguments(self): sut = context_impl.BaseScopesContext() @decorators.inject(context=sut) class Component: pass @decorators.inject(context=sut, name='my_function') def f(a: int, b, c: types.Yadi[Component]): pass fun = sut.get_bean('my_function') fun(1, 2)
def test_functions_keyword_argument(self): sut = context_impl.BaseScopesContext() @decorators.inject(context=sut) class Component: pass @decorators.inject(context=sut, name='my_function') def f(a: int, b, c: types.Yadi[Component] = None, d: str = None): self.assertTrue(isinstance(c, Component)) fun = sut.get_bean('my_function') fun(1, 2, d='hello')
def test_base_scopes_prototype(self): sut = context_impl.BaseScopesContext() class Component1I(metaclass=abc.ABCMeta): pass class Component2I(metaclass=abc.ABCMeta): pass @decorators.inject(context=sut, scope=context.PROTOTYPE, name='a component 1') class Component1(Component1I): pass @decorators.inject(context=sut, name='a component 2') class Component2(Component2I): def __init__(self, f1: types.Yadi[Component1I], f2: types.Yadi[Component1], f3: types.Yadi['a component 1']): self.f1, self.f2, self.f3 = f1, f2, f3 @decorators.inject(context=sut, name='a component 3') class Component3(Component2I): def __init__(self, f1: types.Yadi[Component1I], f2: types.Yadi[Component1], f3: types.Yadi['a component 1']): self.f1, self.f2, self.f3 = f1, f2, f3 c2 = sut.get_bean('a component 2') # type: Component2 c3 = sut.get_bean('a component 3') # type: Component3 self.assertEqual(_ScopedProxy, type(c2.f1)) self.assertEqual(_ScopedProxy, type(c2.f2)) self.assertEqual(_ScopedProxy, type(c2.f3)) self.assertEqual(_ScopedProxy, type(c3.f1)) self.assertEqual(_ScopedProxy, type(c3.f2)) self.assertEqual(_ScopedProxy, type(c3.f3)) instances = [c2.f1, c2.f2, c2.f3, c3.f1, c3.f2, c3.f3] elements_to_skip = { i * (len(instances) + 1) for i in range(len(instances)) } for i, couple in enumerate(itertools.product(instances, instances)): if i in elements_to_skip: continue ca, cb = couple self.assertIsNot(ca, cb)
def test_functions_of_functions(self): sut = context_impl.BaseScopesContext() @decorators.inject(context=sut, name='another_function') def h(x, y, z=None): pass @decorators.inject(context=sut, name='my_function') def f(a: int, b, c: types.Yadi['another_function'] = None, d: str = None): c(a, b, z=d) sut.get_bean('my_function')(12, 23, d=5)
def test_base_scopes_singleton(self): sut = context_impl.BaseScopesContext() class Component1I(metaclass=abc.ABCMeta): pass class Component2I(metaclass=abc.ABCMeta): pass @decorators.inject(context=sut, scope=context.SINGLETON, name='a component 1') class Component1(Component1I): _COUNTER = 0 def __init__(self): assert 0 == self.__class__._COUNTER self.__class__._COUNTER += 1 @decorators.inject(context=sut, name='a component 2') class Component2(Component2I): def __init__(self, f1: types.Yadi[Component1I], f2: types.Yadi[Component1], f3: types.Yadi['a component 1']): self.f1, self.f2, self.f3 = f1, f2, f3 @decorators.inject(context=sut, name='a component 3') class Component3(Component2I): def __init__(self, f1: types.Yadi[Component1I], f2: types.Yadi[Component1], f3: types.Yadi['a component 1']): self.f1, self.f2, self.f3 = f1, f2, f3 c2 = sut.get_bean('a component 2') # type: Component2 c3 = sut.get_bean('a component 3') # type: Component3 self.assertEqual(Component1, type(c2.f1)) self.assertEqual(Component1, type(c2.f2)) self.assertEqual(Component1, type(c2.f3)) self.assertEqual(Component1, type(c3.f1)) self.assertEqual(Component1, type(c3.f2)) self.assertEqual(Component1, type(c3.f3)) instances = [c2.f1, c2.f2, c2.f3, c3.f1, c3.f2, c3.f3] for i, couple in enumerate(itertools.product(instances, instances)): ca, cb = couple self.assertIs(ca, cb)
def text_functions_and_objects(self): sut = context_impl.BaseScopesContext() @decorators.inject(context=sut) class Component: pass @decorators.inject(context=sut, name='another_function') def h(x, y, z=None): self.assertTrue(isinstance(x, Component)) @decorators.inject(context=sut, name='my_function') def f(a: types.Yadi[Component], b, c: types.Yadi['another_function'] = None, d: str = None): c(a, b, z=d) sut.get_bean('my_function')(23, d=5)
def test_base_scopes_context_with_names(self): sut = context_impl.BaseScopesContext() class Component1I(metaclass=abc.ABCMeta): pass class Component2I(metaclass=abc.ABCMeta): pass @decorators.inject(context=sut, name='a component') class Component2(Component2I): def __init__(self, c1: types.Yadi[Component1I]): self.c1 = c1 @decorators.inject(context=sut) class Component1(Component1I): pass c2 = sut.get_bean('a component') # type: Component2 self.assertEqual(Component1, type(c2.c1))
def test_base_scopes_context_with_types(self): sut = context_impl.BaseScopesContext() class Component1I(metaclass=abc.ABCMeta): pass class Component2I(metaclass=abc.ABCMeta): pass @decorators.inject(context=sut) class Component2(Component2I): def __init__(self, c1: types.Yadi[Component1I]): self.c1 = c1 @decorators.inject(context=sut) class Component1(Component1I): pass c2_class = sut.get_bean(Component2) # type: Component2 self.assertEqual(Component1, type(c2_class.c1)) c2_interface = sut.get_bean(Component2I) # type: Component2 self.assertIs(c2_class, c2_interface)