Ejemplo n.º 1
0
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'
Ejemplo n.º 2
0
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 == {}
Ejemplo n.º 3
0
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'
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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