forked from orbit-oss/flask
WIP
This commit is contained in:
parent
e77bc2d86c
commit
d0cf5ef394
6 changed files with 497 additions and 520 deletions
|
|
@ -16,106 +16,98 @@ import unittest
|
|||
from tests import TestFlask
|
||||
|
||||
|
||||
class TestAppContext(TestFlask):
|
||||
def test_basic_url_generation():
|
||||
app = flask.Flask(__name__)
|
||||
app.config['SERVER_NAME'] = 'localhost'
|
||||
app.config['PREFERRED_URL_SCHEME'] = 'https'
|
||||
|
||||
def test_basic_url_generation(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.config['SERVER_NAME'] = 'localhost'
|
||||
app.config['PREFERRED_URL_SCHEME'] = 'https'
|
||||
@app.route('/')
|
||||
def index():
|
||||
pass
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
pass
|
||||
with app.app_context():
|
||||
rv = flask.url_for('index')
|
||||
assert rv == 'https://localhost/'
|
||||
|
||||
with app.app_context():
|
||||
rv = flask.url_for('index')
|
||||
assert rv == 'https://localhost/'
|
||||
|
||||
def test_url_generation_requires_server_name(self):
|
||||
app = flask.Flask(__name__)
|
||||
with app.app_context():
|
||||
with pytest.raises(RuntimeError):
|
||||
flask.url_for('index')
|
||||
|
||||
def test_url_generation_without_context_fails(self):
|
||||
def test_url_generation_requires_server_name():
|
||||
app = flask.Flask(__name__)
|
||||
with app.app_context():
|
||||
with pytest.raises(RuntimeError):
|
||||
flask.url_for('index')
|
||||
|
||||
def test_request_context_means_app_context(self):
|
||||
app = flask.Flask(__name__)
|
||||
with app.test_request_context():
|
||||
assert flask.current_app._get_current_object() == app
|
||||
assert flask._app_ctx_stack.top == None
|
||||
def test_url_generation_without_context_fails():
|
||||
with pytest.raises(RuntimeError):
|
||||
flask.url_for('index')
|
||||
|
||||
def test_app_context_provides_current_app(self):
|
||||
app = flask.Flask(__name__)
|
||||
with app.app_context():
|
||||
assert flask.current_app._get_current_object() == app
|
||||
assert flask._app_ctx_stack.top == None
|
||||
def test_request_context_means_app_context():
|
||||
app = flask.Flask(__name__)
|
||||
with app.test_request_context():
|
||||
assert flask.current_app._get_current_object() == app
|
||||
assert flask._app_ctx_stack.top == None
|
||||
|
||||
def test_app_tearing_down(self):
|
||||
cleanup_stuff = []
|
||||
app = flask.Flask(__name__)
|
||||
@app.teardown_appcontext
|
||||
def cleanup(exception):
|
||||
cleanup_stuff.append(exception)
|
||||
def test_app_context_provides_current_app():
|
||||
app = flask.Flask(__name__)
|
||||
with app.app_context():
|
||||
assert flask.current_app._get_current_object() == app
|
||||
assert flask._app_ctx_stack.top == None
|
||||
|
||||
with app.app_context():
|
||||
pass
|
||||
def test_app_tearing_down():
|
||||
cleanup_stuff = []
|
||||
app = flask.Flask(__name__)
|
||||
@app.teardown_appcontext
|
||||
def cleanup(exception):
|
||||
cleanup_stuff.append(exception)
|
||||
|
||||
assert cleanup_stuff == [None]
|
||||
with app.app_context():
|
||||
pass
|
||||
|
||||
def test_app_tearing_down_with_previous_exception(self):
|
||||
cleanup_stuff = []
|
||||
app = flask.Flask(__name__)
|
||||
@app.teardown_appcontext
|
||||
def cleanup(exception):
|
||||
cleanup_stuff.append(exception)
|
||||
assert cleanup_stuff == [None]
|
||||
|
||||
try:
|
||||
raise Exception('dummy')
|
||||
except Exception:
|
||||
pass
|
||||
def test_app_tearing_down_with_previous_exception():
|
||||
cleanup_stuff = []
|
||||
app = flask.Flask(__name__)
|
||||
@app.teardown_appcontext
|
||||
def cleanup(exception):
|
||||
cleanup_stuff.append(exception)
|
||||
|
||||
with app.app_context():
|
||||
pass
|
||||
try:
|
||||
raise Exception('dummy')
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
assert cleanup_stuff == [None]
|
||||
with app.app_context():
|
||||
pass
|
||||
|
||||
def test_custom_app_ctx_globals_class(self):
|
||||
class CustomRequestGlobals(object):
|
||||
def __init__(self):
|
||||
self.spam = 'eggs'
|
||||
app = flask.Flask(__name__)
|
||||
app.app_ctx_globals_class = CustomRequestGlobals
|
||||
with app.app_context():
|
||||
assert flask.render_template_string('{{ g.spam }}') == 'eggs'
|
||||
assert cleanup_stuff == [None]
|
||||
|
||||
def test_context_refcounts(self):
|
||||
called = []
|
||||
app = flask.Flask(__name__)
|
||||
@app.teardown_request
|
||||
def teardown_req(error=None):
|
||||
called.append('request')
|
||||
@app.teardown_appcontext
|
||||
def teardown_app(error=None):
|
||||
called.append('app')
|
||||
@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''
|
||||
c = app.test_client()
|
||||
res = c.get('/')
|
||||
assert res.status_code == 200
|
||||
assert res.data == b''
|
||||
assert called == ['request', 'app']
|
||||
def test_custom_app_ctx_globals_class():
|
||||
class CustomRequestGlobals(object):
|
||||
def __init__(self):
|
||||
self.spam = 'eggs'
|
||||
app = flask.Flask(__name__)
|
||||
app.app_ctx_globals_class = CustomRequestGlobals
|
||||
with app.app_context():
|
||||
assert flask.render_template_string('{{ g.spam }}') == 'eggs'
|
||||
|
||||
|
||||
def suite():
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTest(unittest.makeSuite(TestAppContext))
|
||||
return suite
|
||||
def test_context_refcounts():
|
||||
called = []
|
||||
app = flask.Flask(__name__)
|
||||
@app.teardown_request
|
||||
def teardown_req(error=None):
|
||||
called.append('request')
|
||||
@app.teardown_appcontext
|
||||
def teardown_app(error=None):
|
||||
called.append('app')
|
||||
@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''
|
||||
c = app.test_client()
|
||||
res = c.get('/')
|
||||
assert res.status_code == 200
|
||||
assert res.data == b''
|
||||
assert called == ['request', 'app']
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue