flask/tests/test_appctx.py

220 lines
5.0 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
2014-09-01 03:54:45 +08:00
tests.appctx
2014-09-21 22:47:38 +08:00
~~~~~~~~~~~~
Tests the application context.
2019-06-23 04:09:09 +08:00
:copyright: 2010 Pallets
:license: BSD-3-Clause
"""
2014-09-02 11:26:52 +08:00
import pytest
import flask
def test_basic_url_generation(app):
2014-09-04 02:50:54 +08:00
app.config['SERVER_NAME'] = 'localhost'
app.config['PREFERRED_URL_SCHEME'] = 'https'
@app.route('/')
def index():
pass
with app.app_context():
rv = flask.url_for('index')
assert rv == 'https://localhost/'
def test_url_generation_requires_server_name(app):
2014-09-04 02:50:54 +08:00
with app.app_context():
2014-09-02 11:26:52 +08:00
with pytest.raises(RuntimeError):
flask.url_for('index')
2014-09-04 02:50:54 +08:00
def test_url_generation_without_context_fails():
with pytest.raises(RuntimeError):
flask.url_for('index')
def test_request_context_means_app_context(app):
2014-09-04 02:50:54 +08:00
with app.test_request_context():
assert flask.current_app._get_current_object() == app
assert flask._app_ctx_stack.top is None
2014-09-04 02:50:54 +08:00
def test_app_context_provides_current_app(app):
2014-09-04 02:50:54 +08:00
with app.app_context():
assert flask.current_app._get_current_object() == app
assert flask._app_ctx_stack.top is None
2014-09-04 02:50:54 +08:00
def test_app_tearing_down(app):
2014-09-04 02:50:54 +08:00
cleanup_stuff = []
2014-09-04 02:50:54 +08:00
@app.teardown_appcontext
def cleanup(exception):
cleanup_stuff.append(exception)
with app.app_context():
pass
assert cleanup_stuff == [None]
def test_app_tearing_down_with_previous_exception(app):
2014-09-04 02:50:54 +08:00
cleanup_stuff = []
2014-09-04 02:50:54 +08:00
@app.teardown_appcontext
def cleanup(exception):
cleanup_stuff.append(exception)
try:
raise Exception('dummy')
except Exception:
pass
with app.app_context():
pass
assert cleanup_stuff == [None]
def test_app_tearing_down_with_handled_exception_by_except_block(app):
cleanup_stuff = []
@app.teardown_appcontext
def cleanup(exception):
cleanup_stuff.append(exception)
with app.app_context():
try:
raise Exception('dummy')
except Exception:
pass
assert cleanup_stuff == [None]
2014-09-04 02:50:54 +08:00
2017-07-10 22:10:47 +08:00
def test_app_tearing_down_with_handled_exception_by_app_handler(app, client):
app.config['PROPAGATE_EXCEPTIONS'] = True
cleanup_stuff = []
@app.teardown_appcontext
def cleanup(exception):
cleanup_stuff.append(exception)
@app.route('/')
def index():
raise Exception('dummy')
@app.errorhandler(Exception)
def handler(f):
return flask.jsonify(str(f))
with app.app_context():
2017-07-10 22:10:47 +08:00
client.get('/')
assert cleanup_stuff == [None]
2017-07-10 22:10:47 +08:00
def test_app_tearing_down_with_unhandled_exception(app, client):
app.config['PROPAGATE_EXCEPTIONS'] = True
cleanup_stuff = []
@app.teardown_appcontext
def cleanup(exception):
cleanup_stuff.append(exception)
@app.route('/')
def index():
raise Exception('dummy')
with pytest.raises(Exception):
with app.app_context():
2017-07-10 22:10:47 +08:00
client.get('/')
assert len(cleanup_stuff) == 1
assert isinstance(cleanup_stuff[0], Exception)
assert str(cleanup_stuff[0]) == 'dummy'
def test_app_ctx_globals_methods(app, app_ctx):
# get
assert flask.g.get('foo') is None
assert flask.g.get('foo', 'bar') == 'bar'
# __contains__
assert 'foo' not in flask.g
flask.g.foo = 'bar'
assert 'foo' in flask.g
# setdefault
flask.g.setdefault('bar', 'the cake is a lie')
flask.g.setdefault('bar', 'hello world')
assert flask.g.bar == 'the cake is a lie'
# pop
assert flask.g.pop('bar') == 'the cake is a lie'
with pytest.raises(KeyError):
flask.g.pop('bar')
assert flask.g.pop('bar', 'more cake') == 'more cake'
# __iter__
assert list(flask.g) == ['foo']
def test_custom_app_ctx_globals_class(app):
2014-09-04 02:50:54 +08:00
class CustomRequestGlobals(object):
def __init__(self):
self.spam = 'eggs'
2014-09-04 02:50:54 +08:00
app.app_ctx_globals_class = CustomRequestGlobals
with app.app_context():
assert flask.render_template_string('{{ g.spam }}') == 'eggs'
def test_context_refcounts(app, client):
2014-09-04 02:50:54 +08:00
called = []
2014-09-04 02:50:54 +08:00
@app.teardown_request
def teardown_req(error=None):
called.append('request')
2014-09-04 02:50:54 +08:00
@app.teardown_appcontext
def teardown_app(error=None):
called.append('app')
2014-09-04 02:50:54 +08:00
@app.route('/')
def index():
with flask._app_ctx_stack.top:
with flask._request_ctx_stack.top:
pass
env = flask._request_ctx_stack.top.request.environ
assert env['werkzeug.request'] is not None
return u''
res = client.get('/')
2014-09-04 02:50:54 +08:00
assert res.status_code == 200
assert res.data == b''
assert called == ['request', 'app']
def test_clean_pop(app):
app.testing = False
called = []
@app.teardown_request
def teardown_req(error=None):
1 / 0
@app.teardown_appcontext
def teardown_app(error=None):
called.append('TEARDOWN')
try:
with app.test_request_context():
called.append(flask.current_app.name)
except ZeroDivisionError:
pass
2018-02-28 22:50:49 +08:00
assert called == ['flask_test', 'TEARDOWN']
assert not flask.current_app