Ejemplo n.º 1
0
def test_injector_descend2():
    class A:
        pass

    def fn(i: Injector, a: A):
        assert isinstance(a, A)
        assert a is ainst
        assert injector[A] is not a
        return i.exec(fn2)

    def fn2(i: Injector, a: A):
        assert isinstance(a, A)
        assert a is ainst
        assert i[A] is a
        return "OK"

    injector = Injector()
    injector.provide(A)

    iclone = injector.descend()
    ainst = iclone[A] = iclone[A]

    assert iclone.exec(fn) == "OK"

    iclone2 = iclone.descend()
    assert iclone2.exec(fn) == "OK"
Ejemplo n.º 2
0
def test_descend():
    injector = Injector()
    injector.provide(SelfInject)

    child = injector.descend()
    child.provide(injectable_func2)

    x = child[injectable_func2]
    assert isinstance(x, SelfInject)
Ejemplo n.º 3
0
def test_attr():
    class Request:
        injector: Inject[Injector]

        def verify(self):
            assert self.injector[Request] is self

    injector = Injector()
    injector.provide(Request)

    req_injector = injector.descend()
    req_injector[Request] = req_injector[Request]

    assert req_injector[Request] is req_injector[Request]

    def handle(req: Request):
        assert isinstance(req, Request)
        req.verify()

    handler = Injectable(handle)
    handler(req_injector)
Ejemplo n.º 4
0
async def handle_error(injector: Injector, error: Exception) -> bool:
    try:
        exc_injector = injector.descend()

        event = Event()
        exc_injector[Event] = event
        exc_injector[Exception] = error

        for hexc, hfn in _SERVER_ERROR:
            if isinstance(error, hexc):
                if not event.is_prevented:
                    await hfn(exc_injector)
                else:
                    break

        if event.is_prevented:
            return True
    except Exception:
        # TODO: internal server error
        raise
    else:
        raise error

    return False
Ejemplo n.º 5
0
def test_injector_descend():
    injector = Injector()
    injector.provide("X", "XValue", VALUE)
    subi = injector.descend()
    assert subi.get("X") == "XValue"