def test_injector_own_kwonly(): injector = Injector() class Config(dict): def __init__(self): super().__init__(some_key="OK") def get_kwarg(config: Config, *, name, type): assert name == "some_key" assert type is str return config[name] def fn(*, some_key: str): assert some_key == "OK" return "NICE" injector.provide(Config) injector.provide(fn, provide=[KwOnly(get_kwarg)]) assert injector.get(fn) == "NICE" with pytest.raises(InjectError) as exc: assert injector.exec(fn) == "NICE" exc.match( "Not found suitable value for: <ValueResolver name=some_key id=<class 'str'>>" )
def test_injector_exec_object(): BINST = "BInstance" injector = Injector() class A: pass class B: def __call__(self, a: A): assert isinstance(a, A) return "BBBB" def fn1(arg: B): assert isinstance(arg, B) return "fn1" def fn2(arg: BINST): assert arg == "BBBB" return "fn2" injector.provide(A) injector.provide(B) injector.provide(BINST, B()) assert injector.exec(fn1) == "fn1" assert injector.exec(fn2) == "fn2"
def test_injector_attribute_inheritance(): injector = Injector() class A: pass class B: a: Inject[A] def __init__(self): assert isinstance(self.a, A) class X: pass class C(B): x: Inject[X] def __init__(self): super().__init__() assert isinstance(self.x, X) injector.provide(A) injector.provide(X) c = injector.exec(C) assert isinstance(c, C) assert isinstance(c.a, A) assert isinstance(c.x, X)
def test_forwardref_fn(): def fn(a: "A"): assert isinstance(a, A) return "OK" injector = Injector() injector.provide(A) assert injector.exec(fn) == "OK"
def test_injector_kwonly_error(): injector = Injector() class Config(dict): def __init__(self): super().__init__(some_key="OK") def get_kwarg(config: Config, *, name, type, xx): pass def fn(*, some_key: str): pass injector.provide(Config) injector.provide(KwOnly(get_kwarg)) with pytest.raises(InjectError) as exc: injector.exec(fn) exc.match("^Not found suitable value for")
def test_injector_setitem(): class A: pass injector = Injector() injector[A] = A() def fn(a: A): assert isinstance(a, A) return "OK" assert injector.exec(fn) == "OK" del injector[A] with pytest.raises(InjectError) as exc: injector.exec(fn) == "OK" exc.match("Not found suitable value for")
def test_injector_exec_cls(): injector = Injector() class A: pass class B: def __init__(self, a: A): assert isinstance(a, A) injector.provide(A) assert isinstance(injector.exec(B), B)
def test_injector_exec_fn(): injector = Injector() class A: pass def x(a: A): assert isinstance(a, A) return "OK" injector.provide(A) assert injector.exec(x) == "OK"
def test_injector_recursion(): injector = Injector() class A: def __init__(self, a: "A") -> None: pass injector.provide(A) injector.provide("A", A) def x(a: A): return "OK" with pytest.raises(RecursionError): assert injector.exec(x) == "OK"
def test_injector_attribute(): injector = Injector() class A: pass class B: a: Inject[A] def __init__(self): assert isinstance(self.a, A) injector.provide(A) b = injector.exec(B) assert isinstance(b, B) assert isinstance(b.a, A)
def test_generic_param(): T = TypeVar("T") class A(Generic[T]): b: Inject[T] class B: pass def fn(a: A[B]): assert isinstance(a, A) assert isinstance(a.b, B) return "OK" injector = Injector() injector.provide(A) injector.provide(B) assert injector.exec(fn) == "OK"
def test_injector_kwonly2(): injector = Injector() class Config(dict): def __init__(self): super().__init__(some_key="OK") def __call__(self, *, name, type): assert name == "some_key" assert type is str return self[name] def fn(*, some_key: str): assert some_key == "OK" return "NICE" injector.provide(KwOnly(Config())) assert injector.exec(fn) == "NICE"
def test_token(): TOKEN = Token("__token__") assert repr(TOKEN) == "<class 'test_injector.__token__'>" class A: pass def fn(a: TOKEN): assert isinstance(a, A) return "OK" injector = Injector() injector.provide(TOKEN, A) assert injector.exec(fn) == "OK" with pytest.raises(TypeError) as exc: Token(None) exc.match("argument 1 must be str")
def test_injector_kwonly(): injector = Injector() class Config(dict): def __init__(self): super().__init__(some_key="OK") def get_kwarg(config: Config, *, name, type): assert name == "some_key" assert type is str return config[name] def fn(*, some_key: str): assert some_key == "OK" return "NICE" injector.provide(Config) injector.provide(KwOnly(get_kwarg)) assert injector.exec(fn) == "NICE"
def test_func_forward(): injector = Injector() injector.provide(SelfInject) x = injector.exec(fn_def_forward) assert isinstance(x, SelfInject)
def test_func_default(): injector = Injector() injector.exec(fn_def_arg1)
def fn(i: Injector, a: A): assert isinstance(a, A) assert a is ainst assert injector[A] is not a return i.exec(fn2)