Exemplo n.º 1
0
def test_DispatchResource(loop, app):  # noqa
    class Res(Resource):
        pass

    class CoroRes(InitCoroMixin, Resource):
        calls_ainit = 0

        @asyncio.coroutine
        def __ainit__(self):
            self.calls_ainit += 1

    add_child(app, 'aiohttp_traversal.ext.resources.Root', 'simple', Res)
    add_child(app, Root, 'coro', CoroRes)

    request = MagicMock(name='request')
    request.app = app

    root = Root(app)

    res_simple = loop.run_until_complete(iter(root['simple']))
    assert isinstance(res_simple, Res)
    assert res_simple.name == 'simple'
    assert res_simple.__parent__ is root

    res_coro = loop.run_until_complete(iter(root['coro']))
    assert isinstance(res_coro, CoroRes)
    assert res_coro.name == 'coro'
    assert res_coro.__parent__ is root
    assert res_coro.calls_ainit == 1

    with pytest.raises(KeyError):
        loop.run_until_complete(iter(root['not_exist']))
Exemplo n.º 2
0
def create_app():
    app = Application(router=TraversalRouter())  # create main application instance
    app.router.set_root_factory(lambda request, app=app: Root(app))  # set root factory
    app.router.bind_view(Root, HelloView)  # add view for '/'
    app.router.bind_view(Root, HelloJSON, 'json')  # add view for '/json'

    return app
Exemplo n.º 3
0
    def __init__(self, *args, **kwargs):
        kwargs.setdefault('router', TraversalRouter())
        kwargs.setdefault('middlewares', [exc_handlers_middleware])
        super().__init__(*args, **kwargs)

        self.router.set_root_factory(lambda request, app=self: Root(app))

        self['settings'] = {}
Exemplo n.º 4
0
def create_app():
    app = Application(router=TraversalRouter())

    app.middlewares.append(json_error_middleware)

    app.router.set_root_factory(lambda request, app=app: Root(app))
    app.router.bind_view(Root, Counter)

    app['counter'] = 0

    return app