def test_child_scope(): TestKey = Key('TestKey') TestKey2 = Key('TestKey2') def parent_module(binder): binder.bind(TestKey, to=object, scope=singleton) def first_child_module(binder): binder.bind(TestKey2, to=object, scope=singleton) def second_child_module(binder): binder.bind(TestKey2, to='marker', scope=singleton) injector = Injector(modules=[parent_module]) first_child_injector = injector.create_child_injector( modules=[first_child_module]) second_child_injector = injector.create_child_injector( modules=[second_child_module]) assert first_child_injector.get(TestKey) is first_child_injector.get( TestKey) assert first_child_injector.get(TestKey) is second_child_injector.get( TestKey) assert first_child_injector.get(TestKey2) is not second_child_injector.get( TestKey2)
def test_child_scope(): TestKey = NewType('TestKey', str) TestKey2 = NewType('TestKey2', str) def parent_module(binder): binder.bind(TestKey, to='in parent', scope=singleton) def first_child_module(binder): binder.bind(TestKey2, to='in first child', scope=singleton) def second_child_module(binder): binder.bind(TestKey2, to='in second child', scope=singleton) injector = Injector(modules=[parent_module]) first_child_injector = injector.create_child_injector( modules=[first_child_module]) second_child_injector = injector.create_child_injector( modules=[second_child_module]) assert first_child_injector.get(TestKey) is first_child_injector.get( TestKey) assert first_child_injector.get(TestKey) is second_child_injector.get( TestKey) assert first_child_injector.get(TestKey2) is not second_child_injector.get( TestKey2)
def prepare_nested_injectors(): def configure(binder): binder.bind(str, to='asd') parent = Injector(configure) child = parent.create_child_injector() return parent, child
def test_child_injector(): class ParentScopedService: pass class ChildScopedService: pass def configure_parent(binder: Binder): binder.bind(ParentScopedService, scope=singleton) def configure_child(binder: Binder): binder.bind(ChildScopedService, scope=singleton) parent_injector = Injector(configure_parent, auto_bind=False) child_injector = parent_injector.create_child_injector(configure_child, auto_bind=False) assert parent_injector.get(ParentScopedService) == parent_injector.get( ParentScopedService) assert child_injector.get(ParentScopedService) == child_injector.get( ParentScopedService) with pytest.raises(UnsatisfiedRequirement): parent_injector.get(ChildScopedService) assert child_injector.get(ChildScopedService) == child_injector.get( ChildScopedService) # Is this supposed to work or not? # https://injector.readthedocs.io/en/latest/terminology.html#child-injectors with pytest.raises(UnsatisfiedRequirement): parent_injector.get(ChildScopedService)
def prepare_nested_injectors(): def configure(binder): binder.bind(str, to='asd') parent = Injector(configure) child = parent.create_child_injector() return parent, child
def shop2(): injector = Injector([CommerceModule]) #Hocemo da promenimo da se za PaymentService koristi # CashPaymentService iz Library_GoodFilter_Cash_ShopModule # umesto # CreditCardPaymentService iz CommerceModule child = injector.create_child_injector(Library_GoodFilter_Cash_ShopModule) return child.get(Shop)
def test_child_scope(): TestKey = Key('TestKey') TestKey2 = Key('TestKey2') def parent_module(binder): binder.bind(TestKey, to=object, scope=singleton) def first_child_module(binder): binder.bind(TestKey2, to=object, scope=singleton) def second_child_module(binder): binder.bind(TestKey2, to='marker', scope=singleton) injector = Injector(modules=[parent_module]) first_child_injector = injector.create_child_injector(modules=[first_child_module]) second_child_injector = injector.create_child_injector(modules=[second_child_module]) assert first_child_injector.get(TestKey) is first_child_injector.get(TestKey) assert first_child_injector.get(TestKey) is second_child_injector.get(TestKey) assert first_child_injector.get(TestKey2) is not second_child_injector.get(TestKey2)
def test_assisted_builder_injection_is_safe_to_use_with_child_injectors(): class X: @inject def __init__(self, builder: AssistedBuilder[NeedsAssistance]): self.builder = builder i1 = Injector() i2 = i1.create_child_injector() b1 = i1.get(X).builder b2 = i2.get(X).builder assert (b1._injector, b2._injector) == (i1, i2)
def test_custom_scopes_work_as_expected_with_child_injectors(): class CustomSingletonScope(SingletonScope): pass custom_singleton = ScopeDecorator(CustomSingletonScope) def parent_module(binder): binder.bind(str, to='parent value', scope=custom_singleton) def child_module(binder): binder.bind(str, to='child value', scope=custom_singleton) parent = Injector(modules=[parent_module]) child = parent.create_child_injector(modules=[child_module]) print('parent, child: %s, %s' % (parent, child)) assert parent.get(str) == 'parent value' assert child.get(str) == 'child value'
def test_custom_scopes_work_as_expected_with_child_injectors(): class CustomSingletonScope(SingletonScope): pass custom_singleton = ScopeDecorator(CustomSingletonScope) def parent_module(binder): binder.bind(str, to='parent value', scope=custom_singleton) def child_module(binder): binder.bind(str, to='child value', scope=custom_singleton) parent = Injector(modules=[parent_module]) child = parent.create_child_injector(modules=[child_module]) print('parent, child: %s, %s' % (parent, child)) assert parent.get(str) == 'parent value' assert child.get(str) == 'child value'
def test_child_injector_rebinds_arguments_for_parent_scope(): I = Key("interface") Cls = Key("test_class") class A: @inject def __init__(self, val: I): self.val = val def configure_parent(binder): binder.bind(Cls, to=A) binder.bind(I, to="Parent") def configure_child(binder): binder.bind(I, to="Child") parent = Injector(configure_parent) assert parent.get(Cls).val == "Parent" child = parent.create_child_injector(configure_child) assert child.get(Cls).val == "Child"
def test_child_injector_rebinds_arguments_for_parent_scope(): class Cls: val = "" class A(Cls): @inject def __init__(self, val: str): self.val = val def configure_parent(binder): binder.bind(Cls, to=A) binder.bind(str, to="Parent") def configure_child(binder): binder.bind(str, to="Child") parent = Injector(configure_parent) assert parent.get(Cls).val == "Parent" child = parent.create_child_injector(configure_child) assert child.get(Cls).val == "Child"
def test_child_injector_rebinds_arguments_for_parent_scope(): I = Key("interface") Cls = Key("test_class") class A(object): @inject(val=I) def __init__(self, val): self.val = val def configure_parent(binder): binder.bind(Cls, to=A) binder.bind(I, to="Parent") def configure_child(binder): binder.bind(I, to="Child") parent = Injector(configure_parent) assert (parent.get(Cls).val == "Parent") child = parent.create_child_injector(configure_child) assert (child.get(Cls).val == "Child")