def test_raises_exception_when_args_unknown(): def f(v): return v i = diay.Injector() with pytest.raises(diay.DiayException): i.call(f)
def test_plugin_lazy(): """ TestPlugin has a dependency which needs a provider, but whether we register the plugin or provider first should not matter. The plugin should be lazily instantiated. """ class Dep: def __init__(self, v): self.v = v def f(self): return self.v class TestPlugin(diay.Plugin): def __init__(self, dep: Dep): self.dep = dep @diay.provider def f(self) -> str: return self.dep.f() i = diay.Injector() i.register_plugin(TestPlugin) @i.provider def provide_dep() -> Dep: return Dep("foo") assert "foo" == i.get(str)
def test_provider_dependencies(): class A: def __init__(self, name): self.name = name class B: def __init__(self, a: A, name): self.a = a self.name = name @diay.provider def a() -> A: return A("a") @diay.provider def b(a: A) -> B: return B(a, "b") i = diay.Injector() i.register_provider(a) i.register_provider(b) b = i.get(B) assert isinstance(b, B) assert "b" == b.name assert isinstance(b.a, A) assert "a" == b.a.name
def test_raises_exception_when_init_args_unknown(): class A: def __init__(self, v): self.v = v i = diay.Injector() with pytest.raises(diay.DiayException): i.get(A)
def test_plugin(): class TestPlugin(diay.Plugin): @diay.provider def f(self) -> str: return "foo" i = diay.Injector() i.register_plugin(TestPlugin()) assert "foo" == i.get(str)
def test_can_get_class_with_init_defaults(): class A: def __init__(self, v="foobar"): self.v = v i = diay.Injector() a = i.get(A) assert isinstance(a, A) assert a.v == "foobar"
def test_call_injects_dependencies(): class A: pass def f(a: A): return a ret = diay.Injector().call(f) assert isinstance(ret, A)
def test_inject_decorator_class_property(): class A: pass @diay.inject("a", A) class B: pass b = diay.Injector().get(B) assert isinstance(b.a, A)
def test_inject_into_constructor(): class A: pass class B: def __init__(self, a: A): self.a = a b = diay.Injector().get(B) assert isinstance(b, B) assert isinstance(b.a, A)
def __init__(self, name, config=None, debug=None): self.name = name self.url_map = werkzeug.routing.Map() if not isinstance(config, Config): config = Config(config or {}) self.config = config if debug is not None: self.config["debug"] = bool(debug) self.injector = diay.Injector() self.injector.instances[Application] = self self.injector.instances[Config] = self.config
def test_provider(): class A: def __init__(self, name): self.name = name @diay.provider def f() -> A: return A("foo") i = diay.Injector() i.register_provider(f) a = i.get(A) assert isinstance(a, A) assert "foo" == a.name
def test_factory(): class A: pass class B(A): pass class C: def __init__(self, a: A): self.a = a i = diay.Injector() i.factories[A] = lambda: B() c = i.get(C) assert isinstance(c.a, B) assert isinstance(c.a, A)
def test_nested_inject(): class A: pass class B: def __init__(self, a: A): self.a = a class C: def __init__(self, b: B): self.b = b c = diay.Injector().get(C) assert isinstance(c, C) assert isinstance(c.b, B) assert isinstance(c.b.a, A)
def test_provider_singleton(): class_count = 0 class A: def __init__(self): nonlocal class_count class_count += 1 @diay.provider(singleton=True) def f() -> A: return A() i = diay.Injector() i.register_provider(f) a1 = i.get(A) a2 = i.get(A) assert isinstance(a1, A) assert a1 is a2
def test_injector_provider_decorator(): class A: def __init__(self, name): self.name = name i = diay.Injector() @i.provider def f1() -> A: return A("foo") a = i.get(A) assert isinstance(a, A) assert "foo" == a.name @i.provider(singleton=True) def f2() -> A: return A("foo") a = i.get(A) assert isinstance(a, A) assert "foo" == a.name
def test_call_with_argument_defaults(): def f(v="foobar"): return v i = diay.Injector() assert i.call(f) == "foobar"