Skip to content
Myr

test_appctx.py

This file

Path

tests/test_appctx.py

170 lines · 4.4 KiB · Python

Last changed by

Merge pull request #1822 from pallets/bugfix/better-popArmin Ronacherbefore adoption8d7e7aa

History of this file

Take it

1# -*- coding: utf-8 -*-2"""3    tests.appctx4    ~~~~~~~~~~~~56    Tests the application context.78    :copyright: (c) 2015 by Armin Ronacher.9    :license: BSD, see LICENSE for more details.10"""1112import pytest1314import flask151617def test_basic_url_generation():18    app = flask.Flask(__name__)19    app.config['SERVER_NAME'] = 'localhost'20    app.config['PREFERRED_URL_SCHEME'] = 'https'2122    @app.route('/')23    def index():24        pass2526    with app.app_context():27        rv = flask.url_for('index')28        assert rv == 'https://localhost/'2930def test_url_generation_requires_server_name():31    app = flask.Flask(__name__)32    with app.app_context():33        with pytest.raises(RuntimeError):34            flask.url_for('index')3536def test_url_generation_without_context_fails():37    with pytest.raises(RuntimeError):38        flask.url_for('index')3940def test_request_context_means_app_context():41    app = flask.Flask(__name__)42    with app.test_request_context():43        assert flask.current_app._get_current_object() == app44    assert flask._app_ctx_stack.top is None4546def test_app_context_provides_current_app():47    app = flask.Flask(__name__)48    with app.app_context():49        assert flask.current_app._get_current_object() == app50    assert flask._app_ctx_stack.top is None5152def test_app_tearing_down():53    cleanup_stuff = []54    app = flask.Flask(__name__)55    @app.teardown_appcontext56    def cleanup(exception):57        cleanup_stuff.append(exception)5859    with app.app_context():60        pass6162    assert cleanup_stuff == [None]6364def test_app_tearing_down_with_previous_exception():65    cleanup_stuff = []66    app = flask.Flask(__name__)67    @app.teardown_appcontext68    def cleanup(exception):69        cleanup_stuff.append(exception)7071    try:72        raise Exception('dummy')73    except Exception:74        pass7576    with app.app_context():77        pass7879    assert cleanup_stuff == [None]8081def test_app_tearing_down_with_handled_exception():82    cleanup_stuff = []83    app = flask.Flask(__name__)84    @app.teardown_appcontext85    def cleanup(exception):86        cleanup_stuff.append(exception)8788    with app.app_context():89        try:90            raise Exception('dummy')91        except Exception:92            pass9394    assert cleanup_stuff == [None]9596def test_app_ctx_globals_methods():97    app = flask.Flask(__name__)98    with app.app_context():99        # get100        assert flask.g.get('foo') is None101        assert flask.g.get('foo', 'bar') == 'bar'102        # __contains__103        assert 'foo' not in flask.g104        flask.g.foo = 'bar'105        assert 'foo' in flask.g106        # setdefault107        flask.g.setdefault('bar', 'the cake is a lie')108        flask.g.setdefault('bar', 'hello world')109        assert flask.g.bar == 'the cake is a lie'110        # pop111        assert flask.g.pop('bar') == 'the cake is a lie'112        with pytest.raises(KeyError):113            flask.g.pop('bar')114        assert flask.g.pop('bar', 'more cake') == 'more cake'115        # __iter__116        assert list(flask.g) == ['foo']117118def test_custom_app_ctx_globals_class():119    class CustomRequestGlobals(object):120        def __init__(self):121            self.spam = 'eggs'122    app = flask.Flask(__name__)123    app.app_ctx_globals_class = CustomRequestGlobals124    with app.app_context():125        assert flask.render_template_string('{{ g.spam }}') == 'eggs'126127def test_context_refcounts():128    called = []129    app = flask.Flask(__name__)130    @app.teardown_request131    def teardown_req(error=None):132        called.append('request')133    @app.teardown_appcontext134    def teardown_app(error=None):135        called.append('app')136    @app.route('/')137    def index():138        with flask._app_ctx_stack.top:139            with flask._request_ctx_stack.top:140                pass141        env = flask._request_ctx_stack.top.request.environ142        assert env['werkzeug.request'] is not None143        return u''144    c = app.test_client()145    res = c.get('/')146    assert res.status_code == 200147    assert res.data == b''148    assert called == ['request', 'app']149150151def test_clean_pop():152    called = []153    app = flask.Flask(__name__)154155    @app.teardown_request156    def teardown_req(error=None):157        1 / 0158159    @app.teardown_appcontext160    def teardown_app(error=None):161        called.append('TEARDOWN')162163    try:164        with app.test_request_context():165            called.append(flask.current_app.name)166    except ZeroDivisionError:167        pass168169    assert called == ['test_appctx', 'TEARDOWN']170    assert not flask.current_app