forked from orbit-oss/flask
Remove useless classes
This commit is contained in:
parent
d0cf5ef394
commit
af41dbe0c4
10 changed files with 739 additions and 818 deletions
|
|
@ -26,7 +26,7 @@ from werkzeug.http import parse_date
|
||||||
from werkzeug.routing import BuildError
|
from werkzeug.routing import BuildError
|
||||||
|
|
||||||
|
|
||||||
class TestBasicFunctionality(TestFlask):
|
class TestBasicFunctionality(object):
|
||||||
|
|
||||||
def test_options_work(self):
|
def test_options_work(self):
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
@ -1295,7 +1295,7 @@ class TestBasicFunctionality(TestFlask):
|
||||||
assert sorted(flask.g) == ['bar', 'foo']
|
assert sorted(flask.g) == ['bar', 'foo']
|
||||||
|
|
||||||
|
|
||||||
class TestSubdomain(TestFlask):
|
class TestSubdomain(object):
|
||||||
|
|
||||||
def test_basic_support(self):
|
def test_basic_support(self):
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -27,7 +27,7 @@ TEST_KEY = 'foo'
|
||||||
SECRET_KEY = 'devkey'
|
SECRET_KEY = 'devkey'
|
||||||
|
|
||||||
|
|
||||||
class TestConfig(TestFlask):
|
class TestConfig(object):
|
||||||
|
|
||||||
def common_object_test(self, app):
|
def common_object_test(self, app):
|
||||||
assert app.secret_key == 'devkey'
|
assert app.secret_key == 'devkey'
|
||||||
|
|
@ -181,7 +181,7 @@ class TestConfig(TestFlask):
|
||||||
assert 'bar stuff 2' == bar_options['STUFF_2']
|
assert 'bar stuff 2' == bar_options['STUFF_2']
|
||||||
|
|
||||||
|
|
||||||
class TestInstance(TestFlask):
|
class TestInstance(object):
|
||||||
def test_explicit_instance_paths(self, apps_tmpdir):
|
def test_explicit_instance_paths(self, apps_tmpdir):
|
||||||
with pytest.raises(ValueError) as excinfo:
|
with pytest.raises(ValueError) as excinfo:
|
||||||
flask.Flask(__name__, instance_path='instance')
|
flask.Flask(__name__, instance_path='instance')
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
tests.deprecations
|
tests.deprecations
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
Tests deprecation support.
|
Tests deprecation support. Not used currently.
|
||||||
|
|
||||||
:copyright: (c) 2014 by Armin Ronacher.
|
:copyright: (c) 2014 by Armin Ronacher.
|
||||||
:license: BSD, see LICENSE for more details.
|
:license: BSD, see LICENSE for more details.
|
||||||
|
|
@ -12,13 +12,3 @@
|
||||||
import flask
|
import flask
|
||||||
import unittest
|
import unittest
|
||||||
from tests import TestFlask, catch_warnings
|
from tests import TestFlask, catch_warnings
|
||||||
|
|
||||||
|
|
||||||
class TestDeprecations(TestFlask):
|
|
||||||
"""not used currently"""
|
|
||||||
|
|
||||||
|
|
||||||
def suite():
|
|
||||||
suite = unittest.TestSuite()
|
|
||||||
suite.addTest(unittest.makeSuite(TestDeprecations))
|
|
||||||
return suite
|
|
||||||
|
|
|
||||||
|
|
@ -1,38 +0,0 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
|
||||||
tests.examples
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
Tests the examples.
|
|
||||||
|
|
||||||
:copyright: (c) 2014 by Armin Ronacher.
|
|
||||||
:license: BSD, see LICENSE for more details.
|
|
||||||
"""
|
|
||||||
import os
|
|
||||||
import unittest
|
|
||||||
from tests import add_to_path
|
|
||||||
|
|
||||||
|
|
||||||
def setup_path():
|
|
||||||
example_path = os.path.join(os.path.dirname(__file__),
|
|
||||||
os.pardir, os.pardir, 'examples')
|
|
||||||
add_to_path(os.path.join(example_path, 'flaskr'))
|
|
||||||
add_to_path(os.path.join(example_path, 'minitwit'))
|
|
||||||
|
|
||||||
|
|
||||||
def suite():
|
|
||||||
setup_path()
|
|
||||||
suite = unittest.TestSuite()
|
|
||||||
try:
|
|
||||||
from minitwit_tests import TestMiniTwit
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
suite.addTest(unittest.makeSuite(TestMiniTwit))
|
|
||||||
try:
|
|
||||||
from flaskr_tests import TestFlaskr
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
suite.addTest(unittest.makeSuite(TestFlaskr))
|
|
||||||
return suite
|
|
||||||
|
|
@ -29,7 +29,7 @@ def has_encoding(name):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
class TestJSON(TestFlask):
|
class TestJSON(object):
|
||||||
|
|
||||||
def test_json_bad_requests(self):
|
def test_json_bad_requests(self):
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
@ -241,7 +241,7 @@ class TestJSON(TestFlask):
|
||||||
except AssertionError:
|
except AssertionError:
|
||||||
assert lines == sorted_by_str
|
assert lines == sorted_by_str
|
||||||
|
|
||||||
class TestSendfile(TestFlask):
|
class TestSendfile(object):
|
||||||
|
|
||||||
def test_send_file_regular(self):
|
def test_send_file_regular(self):
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
@ -423,7 +423,7 @@ class TestSendfile(TestFlask):
|
||||||
rv.close()
|
rv.close()
|
||||||
|
|
||||||
|
|
||||||
class TestLogging(TestFlask):
|
class TestLogging(object):
|
||||||
|
|
||||||
def test_logger_cache(self):
|
def test_logger_cache(self):
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
@ -566,7 +566,7 @@ class TestLogging(TestFlask):
|
||||||
assert flask.url_for('myview', _method='POST') == '/myview/create'
|
assert flask.url_for('myview', _method='POST') == '/myview/create'
|
||||||
|
|
||||||
|
|
||||||
class TestNoImports(TestFlask):
|
class TestNoImports(object):
|
||||||
"""Test Flasks are created without import.
|
"""Test Flasks are created without import.
|
||||||
|
|
||||||
Avoiding ``__import__`` helps create Flask instances where there are errors
|
Avoiding ``__import__`` helps create Flask instances where there are errors
|
||||||
|
|
@ -584,7 +584,7 @@ class TestNoImports(TestFlask):
|
||||||
assert False, 'Flask(import_name) is importing import_name.'
|
assert False, 'Flask(import_name) is importing import_name.'
|
||||||
|
|
||||||
|
|
||||||
class TestStreaming(TestFlask):
|
class TestStreaming(object):
|
||||||
|
|
||||||
def test_streaming_with_context(self):
|
def test_streaming_with_context(self):
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
@ -641,14 +641,3 @@ class TestStreaming(TestFlask):
|
||||||
rv = c.get('/?name=World')
|
rv = c.get('/?name=World')
|
||||||
assert rv.data == b'Hello World!'
|
assert rv.data == b'Hello World!'
|
||||||
assert called == [42]
|
assert called == [42]
|
||||||
|
|
||||||
|
|
||||||
def suite():
|
|
||||||
suite = unittest.TestSuite()
|
|
||||||
if flask.json_available:
|
|
||||||
suite.addTest(unittest.makeSuite(TestJSON))
|
|
||||||
suite.addTest(unittest.makeSuite(TestSendfile))
|
|
||||||
suite.addTest(unittest.makeSuite(TestLogging))
|
|
||||||
suite.addTest(unittest.makeSuite(TestNoImports))
|
|
||||||
suite.addTest(unittest.makeSuite(TestStreaming))
|
|
||||||
return suite
|
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ class _NoLeakAsserter(object):
|
||||||
|
|
||||||
@pytest.mark.skipif(os.environ.get('RUN_FLASK_MEMORY_TESTS') != '1',
|
@pytest.mark.skipif(os.environ.get('RUN_FLASK_MEMORY_TESTS') != '1',
|
||||||
reason='Turned off due to envvar.')
|
reason='Turned off due to envvar.')
|
||||||
class TestMemory(TestFlask):
|
class TestMemory(object):
|
||||||
|
|
||||||
def assert_no_leak(self):
|
def assert_no_leak(self):
|
||||||
return _NoLeakAsserter(self)
|
return _NoLeakAsserter(self)
|
||||||
|
|
@ -88,7 +88,7 @@ class TestMemory(TestFlask):
|
||||||
safe_join('/foo', '..')
|
safe_join('/foo', '..')
|
||||||
|
|
||||||
|
|
||||||
class TestException(TestFlask):
|
class TestException(object):
|
||||||
|
|
||||||
def test_aborting(self):
|
def test_aborting(self):
|
||||||
class Foo(Exception):
|
class Foo(Exception):
|
||||||
|
|
|
||||||
|
|
@ -20,177 +20,166 @@ except ImportError:
|
||||||
from tests import TestFlask
|
from tests import TestFlask
|
||||||
|
|
||||||
|
|
||||||
class TestRequestContext(TestFlask):
|
def test_teardown_on_pop():
|
||||||
|
buffer = []
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
@app.teardown_request
|
||||||
|
def end_of_request(exception):
|
||||||
|
buffer.append(exception)
|
||||||
|
|
||||||
def test_teardown_on_pop(self):
|
ctx = app.test_request_context()
|
||||||
buffer = []
|
ctx.push()
|
||||||
app = flask.Flask(__name__)
|
assert buffer == []
|
||||||
@app.teardown_request
|
ctx.pop()
|
||||||
def end_of_request(exception):
|
assert buffer == [None]
|
||||||
buffer.append(exception)
|
|
||||||
|
|
||||||
ctx = app.test_request_context()
|
def test_teardown_with_previous_exception():
|
||||||
ctx.push()
|
buffer = []
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
@app.teardown_request
|
||||||
|
def end_of_request(exception):
|
||||||
|
buffer.append(exception)
|
||||||
|
|
||||||
|
try:
|
||||||
|
raise Exception('dummy')
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
with app.test_request_context():
|
||||||
assert buffer == []
|
assert buffer == []
|
||||||
ctx.pop()
|
assert buffer == [None]
|
||||||
assert buffer == [None]
|
|
||||||
|
|
||||||
def test_teardown_with_previous_exception(self):
|
def test_proper_test_request_context():
|
||||||
buffer = []
|
app = flask.Flask(__name__)
|
||||||
app = flask.Flask(__name__)
|
app.config.update(
|
||||||
@app.teardown_request
|
SERVER_NAME='localhost.localdomain:5000'
|
||||||
def end_of_request(exception):
|
)
|
||||||
buffer.append(exception)
|
|
||||||
|
|
||||||
try:
|
@app.route('/')
|
||||||
raise Exception('dummy')
|
def index():
|
||||||
except Exception:
|
return None
|
||||||
|
|
||||||
|
@app.route('/', subdomain='foo')
|
||||||
|
def sub():
|
||||||
|
return None
|
||||||
|
|
||||||
|
with app.test_request_context('/'):
|
||||||
|
assert flask.url_for('index', _external=True) == \
|
||||||
|
'http://localhost.localdomain:5000/'
|
||||||
|
|
||||||
|
with app.test_request_context('/'):
|
||||||
|
assert flask.url_for('sub', _external=True) == \
|
||||||
|
'http://foo.localhost.localdomain:5000/'
|
||||||
|
|
||||||
|
try:
|
||||||
|
with app.test_request_context('/', environ_overrides={'HTTP_HOST': 'localhost'}):
|
||||||
pass
|
pass
|
||||||
|
except ValueError as e:
|
||||||
with app.test_request_context():
|
assert str(e) == (
|
||||||
assert buffer == []
|
"the server name provided "
|
||||||
assert buffer == [None]
|
"('localhost.localdomain:5000') does not match the "
|
||||||
|
"server name from the WSGI environment ('localhost')"
|
||||||
def test_proper_test_request_context(self):
|
|
||||||
app = flask.Flask(__name__)
|
|
||||||
app.config.update(
|
|
||||||
SERVER_NAME='localhost.localdomain:5000'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@app.route('/')
|
app.config.update(SERVER_NAME='localhost')
|
||||||
def index():
|
with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost'}):
|
||||||
return None
|
pass
|
||||||
|
|
||||||
@app.route('/', subdomain='foo')
|
app.config.update(SERVER_NAME='localhost:80')
|
||||||
def sub():
|
with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost:80'}):
|
||||||
return None
|
pass
|
||||||
|
|
||||||
with app.test_request_context('/'):
|
def test_context_binding():
|
||||||
assert flask.url_for('index', _external=True) == \
|
app = flask.Flask(__name__)
|
||||||
'http://localhost.localdomain:5000/'
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
return 'Hello %s!' % flask.request.args['name']
|
||||||
|
@app.route('/meh')
|
||||||
|
def meh():
|
||||||
|
return flask.request.url
|
||||||
|
|
||||||
with app.test_request_context('/'):
|
with app.test_request_context('/?name=World'):
|
||||||
assert flask.url_for('sub', _external=True) == \
|
|
||||||
'http://foo.localhost.localdomain:5000/'
|
|
||||||
|
|
||||||
try:
|
|
||||||
with app.test_request_context('/', environ_overrides={'HTTP_HOST': 'localhost'}):
|
|
||||||
pass
|
|
||||||
except ValueError as e:
|
|
||||||
assert str(e) == (
|
|
||||||
"the server name provided "
|
|
||||||
"('localhost.localdomain:5000') does not match the "
|
|
||||||
"server name from the WSGI environment ('localhost')"
|
|
||||||
)
|
|
||||||
|
|
||||||
app.config.update(SERVER_NAME='localhost')
|
|
||||||
with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost'}):
|
|
||||||
pass
|
|
||||||
|
|
||||||
app.config.update(SERVER_NAME='localhost:80')
|
|
||||||
with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost:80'}):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def test_context_binding(self):
|
|
||||||
app = flask.Flask(__name__)
|
|
||||||
@app.route('/')
|
|
||||||
def index():
|
|
||||||
return 'Hello %s!' % flask.request.args['name']
|
|
||||||
@app.route('/meh')
|
|
||||||
def meh():
|
|
||||||
return flask.request.url
|
|
||||||
|
|
||||||
with app.test_request_context('/?name=World'):
|
|
||||||
assert index() == 'Hello World!'
|
|
||||||
with app.test_request_context('/meh'):
|
|
||||||
assert meh() == 'http://localhost/meh'
|
|
||||||
assert flask._request_ctx_stack.top is None
|
|
||||||
|
|
||||||
def test_context_test(self):
|
|
||||||
app = flask.Flask(__name__)
|
|
||||||
assert not flask.request
|
|
||||||
assert not flask.has_request_context()
|
|
||||||
ctx = app.test_request_context()
|
|
||||||
ctx.push()
|
|
||||||
try:
|
|
||||||
assert flask.request
|
|
||||||
assert flask.has_request_context()
|
|
||||||
finally:
|
|
||||||
ctx.pop()
|
|
||||||
|
|
||||||
def test_manual_context_binding(self):
|
|
||||||
app = flask.Flask(__name__)
|
|
||||||
@app.route('/')
|
|
||||||
def index():
|
|
||||||
return 'Hello %s!' % flask.request.args['name']
|
|
||||||
|
|
||||||
ctx = app.test_request_context('/?name=World')
|
|
||||||
ctx.push()
|
|
||||||
assert index() == 'Hello World!'
|
assert index() == 'Hello World!'
|
||||||
|
with app.test_request_context('/meh'):
|
||||||
|
assert meh() == 'http://localhost/meh'
|
||||||
|
assert flask._request_ctx_stack.top is None
|
||||||
|
|
||||||
|
def test_context_test():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
assert not flask.request
|
||||||
|
assert not flask.has_request_context()
|
||||||
|
ctx = app.test_request_context()
|
||||||
|
ctx.push()
|
||||||
|
try:
|
||||||
|
assert flask.request
|
||||||
|
assert flask.has_request_context()
|
||||||
|
finally:
|
||||||
ctx.pop()
|
ctx.pop()
|
||||||
try:
|
|
||||||
index()
|
|
||||||
except RuntimeError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
assert 0, 'expected runtime error'
|
|
||||||
|
|
||||||
def test_greenlet_context_copying(self):
|
def test_manual_context_binding():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
greenlets = []
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
return 'Hello %s!' % flask.request.args['name']
|
||||||
|
|
||||||
@app.route('/')
|
ctx = app.test_request_context('/?name=World')
|
||||||
def index():
|
ctx.push()
|
||||||
reqctx = flask._request_ctx_stack.top.copy()
|
assert index() == 'Hello World!'
|
||||||
def g():
|
ctx.pop()
|
||||||
assert not flask.request
|
try:
|
||||||
assert not flask.current_app
|
index()
|
||||||
with reqctx:
|
except RuntimeError:
|
||||||
assert flask.request
|
pass
|
||||||
assert flask.current_app == app
|
else:
|
||||||
assert flask.request.path == '/'
|
assert 0, 'expected runtime error'
|
||||||
assert flask.request.args['foo'] == 'bar'
|
|
||||||
assert not flask.request
|
|
||||||
return 42
|
|
||||||
greenlets.append(greenlet(g))
|
|
||||||
return 'Hello World!'
|
|
||||||
|
|
||||||
rv = app.test_client().get('/?foo=bar')
|
@pytest.mark.skipif(greenlet is None, reason='greenlet not installed')
|
||||||
assert rv.data == b'Hello World!'
|
def test_greenlet_context_copying():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
greenlets = []
|
||||||
|
|
||||||
result = greenlets[0].run()
|
@app.route('/')
|
||||||
assert result == 42
|
def index():
|
||||||
|
reqctx = flask._request_ctx_stack.top.copy()
|
||||||
def test_greenlet_context_copying_api(self):
|
def g():
|
||||||
app = flask.Flask(__name__)
|
assert not flask.request
|
||||||
greenlets = []
|
assert not flask.current_app
|
||||||
|
with reqctx:
|
||||||
@app.route('/')
|
|
||||||
def index():
|
|
||||||
reqctx = flask._request_ctx_stack.top.copy()
|
|
||||||
@flask.copy_current_request_context
|
|
||||||
def g():
|
|
||||||
assert flask.request
|
assert flask.request
|
||||||
assert flask.current_app == app
|
assert flask.current_app == app
|
||||||
assert flask.request.path == '/'
|
assert flask.request.path == '/'
|
||||||
assert flask.request.args['foo'] == 'bar'
|
assert flask.request.args['foo'] == 'bar'
|
||||||
return 42
|
assert not flask.request
|
||||||
greenlets.append(greenlet(g))
|
return 42
|
||||||
return 'Hello World!'
|
greenlets.append(greenlet(g))
|
||||||
|
return 'Hello World!'
|
||||||
|
|
||||||
rv = app.test_client().get('/?foo=bar')
|
rv = app.test_client().get('/?foo=bar')
|
||||||
assert rv.data == b'Hello World!'
|
assert rv.data == b'Hello World!'
|
||||||
|
|
||||||
result = greenlets[0].run()
|
result = greenlets[0].run()
|
||||||
assert result == 42
|
assert result == 42
|
||||||
|
|
||||||
# Disable test if we don't have greenlets available
|
@pytest.mark.skipif(greenlet is None, reason='greenlet not installed')
|
||||||
if greenlet is None:
|
def test_greenlet_context_copying_api():
|
||||||
test_greenlet_context_copying = None
|
app = flask.Flask(__name__)
|
||||||
test_greenlet_context_copying_api = None
|
greenlets = []
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
reqctx = flask._request_ctx_stack.top.copy()
|
||||||
|
@flask.copy_current_request_context
|
||||||
|
def g():
|
||||||
|
assert flask.request
|
||||||
|
assert flask.current_app == app
|
||||||
|
assert flask.request.path == '/'
|
||||||
|
assert flask.request.args['foo'] == 'bar'
|
||||||
|
return 42
|
||||||
|
greenlets.append(greenlet(g))
|
||||||
|
return 'Hello World!'
|
||||||
|
|
||||||
def suite():
|
rv = app.test_client().get('/?foo=bar')
|
||||||
suite = unittest.TestSuite()
|
assert rv.data == b'Hello World!'
|
||||||
suite.addTest(unittest.makeSuite(TestRequestContext))
|
|
||||||
return suite
|
result = greenlets[0].run()
|
||||||
|
assert result == 42
|
||||||
|
|
|
||||||
|
|
@ -25,136 +25,134 @@ pytestmark = pytest.mark.skipif(
|
||||||
reason='Signals require the blinker library.'
|
reason='Signals require the blinker library.'
|
||||||
)
|
)
|
||||||
|
|
||||||
class TestSignals(TestFlask):
|
def test_template_rendered():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
def test_template_rendered(self):
|
@app.route('/')
|
||||||
app = flask.Flask(__name__)
|
def index():
|
||||||
|
return flask.render_template('simple_template.html', whiskey=42)
|
||||||
|
|
||||||
@app.route('/')
|
recorded = []
|
||||||
def index():
|
|
||||||
return flask.render_template('simple_template.html', whiskey=42)
|
|
||||||
|
|
||||||
recorded = []
|
def record(sender, template, context):
|
||||||
|
recorded.append((template, context))
|
||||||
|
|
||||||
def record(sender, template, context):
|
flask.template_rendered.connect(record, app)
|
||||||
recorded.append((template, context))
|
try:
|
||||||
|
app.test_client().get('/')
|
||||||
|
assert len(recorded) == 1
|
||||||
|
template, context = recorded[0]
|
||||||
|
assert template.name == 'simple_template.html'
|
||||||
|
assert context['whiskey'] == 42
|
||||||
|
finally:
|
||||||
|
flask.template_rendered.disconnect(record, app)
|
||||||
|
|
||||||
flask.template_rendered.connect(record, app)
|
def test_request_signals():
|
||||||
try:
|
app = flask.Flask(__name__)
|
||||||
app.test_client().get('/')
|
calls = []
|
||||||
|
|
||||||
|
def before_request_signal(sender):
|
||||||
|
calls.append('before-signal')
|
||||||
|
|
||||||
|
def after_request_signal(sender, response):
|
||||||
|
assert response.data == b'stuff'
|
||||||
|
calls.append('after-signal')
|
||||||
|
|
||||||
|
@app.before_request
|
||||||
|
def before_request_handler():
|
||||||
|
calls.append('before-handler')
|
||||||
|
|
||||||
|
@app.after_request
|
||||||
|
def after_request_handler(response):
|
||||||
|
calls.append('after-handler')
|
||||||
|
response.data = 'stuff'
|
||||||
|
return response
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
calls.append('handler')
|
||||||
|
return 'ignored anyway'
|
||||||
|
|
||||||
|
flask.request_started.connect(before_request_signal, app)
|
||||||
|
flask.request_finished.connect(after_request_signal, app)
|
||||||
|
|
||||||
|
try:
|
||||||
|
rv = app.test_client().get('/')
|
||||||
|
assert rv.data == b'stuff'
|
||||||
|
|
||||||
|
assert calls == ['before-signal', 'before-handler', 'handler',
|
||||||
|
'after-handler', 'after-signal']
|
||||||
|
finally:
|
||||||
|
flask.request_started.disconnect(before_request_signal, app)
|
||||||
|
flask.request_finished.disconnect(after_request_signal, app)
|
||||||
|
|
||||||
|
def test_request_exception_signal():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
recorded = []
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
1 // 0
|
||||||
|
|
||||||
|
def record(sender, exception):
|
||||||
|
recorded.append(exception)
|
||||||
|
|
||||||
|
flask.got_request_exception.connect(record, app)
|
||||||
|
try:
|
||||||
|
assert app.test_client().get('/').status_code == 500
|
||||||
|
assert len(recorded) == 1
|
||||||
|
assert isinstance(recorded[0], ZeroDivisionError)
|
||||||
|
finally:
|
||||||
|
flask.got_request_exception.disconnect(record, app)
|
||||||
|
|
||||||
|
def test_appcontext_signals():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
recorded = []
|
||||||
|
|
||||||
|
def record_push(sender, **kwargs):
|
||||||
|
recorded.append('push')
|
||||||
|
|
||||||
|
def record_pop(sender, **kwargs):
|
||||||
|
recorded.append('pop')
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
return 'Hello'
|
||||||
|
|
||||||
|
flask.appcontext_pushed.connect(record_push, app)
|
||||||
|
flask.appcontext_popped.connect(record_pop, app)
|
||||||
|
try:
|
||||||
|
with app.test_client() as c:
|
||||||
|
rv = c.get('/')
|
||||||
|
assert rv.data == b'Hello'
|
||||||
|
assert recorded == ['push']
|
||||||
|
assert recorded == ['push', 'pop']
|
||||||
|
finally:
|
||||||
|
flask.appcontext_pushed.disconnect(record_push, app)
|
||||||
|
flask.appcontext_popped.disconnect(record_pop, app)
|
||||||
|
|
||||||
|
def test_flash_signal():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
app.config['SECRET_KEY'] = 'secret'
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
flask.flash('This is a flash message', category='notice')
|
||||||
|
return flask.redirect('/other')
|
||||||
|
|
||||||
|
recorded = []
|
||||||
|
|
||||||
|
def record(sender, message, category):
|
||||||
|
recorded.append((message, category))
|
||||||
|
|
||||||
|
flask.message_flashed.connect(record, app)
|
||||||
|
try:
|
||||||
|
client = app.test_client()
|
||||||
|
with client.session_transaction():
|
||||||
|
client.get('/')
|
||||||
assert len(recorded) == 1
|
assert len(recorded) == 1
|
||||||
template, context = recorded[0]
|
message, category = recorded[0]
|
||||||
assert template.name == 'simple_template.html'
|
assert message == 'This is a flash message'
|
||||||
assert context['whiskey'] == 42
|
assert category == 'notice'
|
||||||
finally:
|
finally:
|
||||||
flask.template_rendered.disconnect(record, app)
|
flask.message_flashed.disconnect(record, app)
|
||||||
|
|
||||||
def test_request_signals(self):
|
|
||||||
app = flask.Flask(__name__)
|
|
||||||
calls = []
|
|
||||||
|
|
||||||
def before_request_signal(sender):
|
|
||||||
calls.append('before-signal')
|
|
||||||
|
|
||||||
def after_request_signal(sender, response):
|
|
||||||
assert response.data == b'stuff'
|
|
||||||
calls.append('after-signal')
|
|
||||||
|
|
||||||
@app.before_request
|
|
||||||
def before_request_handler():
|
|
||||||
calls.append('before-handler')
|
|
||||||
|
|
||||||
@app.after_request
|
|
||||||
def after_request_handler(response):
|
|
||||||
calls.append('after-handler')
|
|
||||||
response.data = 'stuff'
|
|
||||||
return response
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
def index():
|
|
||||||
calls.append('handler')
|
|
||||||
return 'ignored anyway'
|
|
||||||
|
|
||||||
flask.request_started.connect(before_request_signal, app)
|
|
||||||
flask.request_finished.connect(after_request_signal, app)
|
|
||||||
|
|
||||||
try:
|
|
||||||
rv = app.test_client().get('/')
|
|
||||||
assert rv.data == b'stuff'
|
|
||||||
|
|
||||||
assert calls == ['before-signal', 'before-handler', 'handler',
|
|
||||||
'after-handler', 'after-signal']
|
|
||||||
finally:
|
|
||||||
flask.request_started.disconnect(before_request_signal, app)
|
|
||||||
flask.request_finished.disconnect(after_request_signal, app)
|
|
||||||
|
|
||||||
def test_request_exception_signal(self):
|
|
||||||
app = flask.Flask(__name__)
|
|
||||||
recorded = []
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
def index():
|
|
||||||
1 // 0
|
|
||||||
|
|
||||||
def record(sender, exception):
|
|
||||||
recorded.append(exception)
|
|
||||||
|
|
||||||
flask.got_request_exception.connect(record, app)
|
|
||||||
try:
|
|
||||||
assert app.test_client().get('/').status_code == 500
|
|
||||||
assert len(recorded) == 1
|
|
||||||
assert isinstance(recorded[0], ZeroDivisionError)
|
|
||||||
finally:
|
|
||||||
flask.got_request_exception.disconnect(record, app)
|
|
||||||
|
|
||||||
def test_appcontext_signals(self):
|
|
||||||
app = flask.Flask(__name__)
|
|
||||||
recorded = []
|
|
||||||
|
|
||||||
def record_push(sender, **kwargs):
|
|
||||||
recorded.append('push')
|
|
||||||
|
|
||||||
def record_pop(sender, **kwargs):
|
|
||||||
recorded.append('pop')
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
def index():
|
|
||||||
return 'Hello'
|
|
||||||
|
|
||||||
flask.appcontext_pushed.connect(record_push, app)
|
|
||||||
flask.appcontext_popped.connect(record_pop, app)
|
|
||||||
try:
|
|
||||||
with app.test_client() as c:
|
|
||||||
rv = c.get('/')
|
|
||||||
assert rv.data == b'Hello'
|
|
||||||
assert recorded == ['push']
|
|
||||||
assert recorded == ['push', 'pop']
|
|
||||||
finally:
|
|
||||||
flask.appcontext_pushed.disconnect(record_push, app)
|
|
||||||
flask.appcontext_popped.disconnect(record_pop, app)
|
|
||||||
|
|
||||||
def test_flash_signal(self):
|
|
||||||
app = flask.Flask(__name__)
|
|
||||||
app.config['SECRET_KEY'] = 'secret'
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
def index():
|
|
||||||
flask.flash('This is a flash message', category='notice')
|
|
||||||
return flask.redirect('/other')
|
|
||||||
|
|
||||||
recorded = []
|
|
||||||
|
|
||||||
def record(sender, message, category):
|
|
||||||
recorded.append((message, category))
|
|
||||||
|
|
||||||
flask.message_flashed.connect(record, app)
|
|
||||||
try:
|
|
||||||
client = app.test_client()
|
|
||||||
with client.session_transaction():
|
|
||||||
client.get('/')
|
|
||||||
assert len(recorded) == 1
|
|
||||||
message, category = recorded[0]
|
|
||||||
assert message == 'This is a flash message'
|
|
||||||
assert category == 'notice'
|
|
||||||
finally:
|
|
||||||
flask.message_flashed.disconnect(record, app)
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ from tests import TestFlask
|
||||||
from flask._compat import text_type
|
from flask._compat import text_type
|
||||||
|
|
||||||
|
|
||||||
class TestTestTools(TestFlask):
|
class TestTestTools(object):
|
||||||
|
|
||||||
def test_environ_defaults_from_config(self):
|
def test_environ_defaults_from_config(self):
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
@ -213,7 +213,7 @@ class TestTestTools(TestFlask):
|
||||||
assert 'vodka' in flask.request.args
|
assert 'vodka' in flask.request.args
|
||||||
|
|
||||||
|
|
||||||
class TestSubdomain(TestFlask):
|
class TestSubdomain(object):
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def app(self, request):
|
def app(self, request):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue