def test_component_context(): """Ensure the context element of a Component works.""" app = _create_app() ctx = { 'current_user': None, 'limit': 10, 'offset': 10, 'foo': 'bar', } comp = Component(app=app, context=ctx) assert comp.context assert comp.context == ctx assert comp._context == ctx # now replace the context in place new_ctx = {'baz': 'qux', 'limit': 20, 'new_key': 'new'} comp.update_context(new_ctx) assert comp.context == new_ctx assert comp._context == new_ctx assert 'current_user' not in comp.context assert 'offset' not in comp.context assert comp.context['limit'] == 20 assert comp.context['new_key'] == 'new'
def test_clear_context(): """Ensure we can clear the context""" app = _create_app() ctx = { 'ctxkey': 'bar', } comp = Component(app=app, context=ctx) assert comp.context == ctx comp.clear_context() assert comp.context == {}
def test_component_creation_with_app(): """Ensure we can create a Component with an app.""" app = _create_app() comp = Component(app=app) assert comp.app is comp._app is app assert comp.config['FOO'] == 'BAR'
def test_component_creation_with_init_app(): """Ensure we can create a Component without an app.""" app = _create_app() comp = Component() comp.init_app(app) with app.app_context(): assert comp.app == app assert comp._app is None assert comp.config['FOO'] == 'BAR' # now test with some context ctx = { 'current_user': None, 'limit': 10, 'offset': 10, 'foo': 'bar', } # @TODO: detect that this is already registered and update the default # context... comp.init_app(app, context=ctx) assert comp.app == app assert comp.context == ctx
def test_component_implicit_current_app(): """Ensure that a component without app knowledge falls back to current_app """ app = _create_app() comp = Component() with app.test_request_context('/'): assert comp.app == app # and with no proxies bound, give me a RuntimeError with pytest.raises(RuntimeError): assert comp.app != app
def test_component_context_no_default_eager_app(): """Ensure that context works without a default context.""" app = _create_app() comp = Component(app=app) new_ctx = {'foo': 'bar'} assert comp._context is DEFAULT_DICT assert comp.context is DEFAULT_DICT comp.update_context(new_ctx) assert comp.context == new_ctx assert comp._context == new_ctx comp.clear_context() assert comp.context is DEFAULT_DICT assert comp._context is DEFAULT_DICT
def test_component_raises_when_no_app(): """Ensure that Component.app raises if nothing is present.""" err_msg = ("This component hasn't been initialized yet and an app context" " doesn't exist.") with pytest.raises(RuntimeError, message=err_msg): Component().app