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
|
||||
|
||||
|
||||
class TestBasicFunctionality(TestFlask):
|
||||
class TestBasicFunctionality(object):
|
||||
|
||||
def test_options_work(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
|
@ -1295,7 +1295,7 @@ class TestBasicFunctionality(TestFlask):
|
|||
assert sorted(flask.g) == ['bar', 'foo']
|
||||
|
||||
|
||||
class TestSubdomain(TestFlask):
|
||||
class TestSubdomain(object):
|
||||
|
||||
def test_basic_support(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -27,7 +27,7 @@ TEST_KEY = 'foo'
|
|||
SECRET_KEY = 'devkey'
|
||||
|
||||
|
||||
class TestConfig(TestFlask):
|
||||
class TestConfig(object):
|
||||
|
||||
def common_object_test(self, app):
|
||||
assert app.secret_key == 'devkey'
|
||||
|
|
@ -181,7 +181,7 @@ class TestConfig(TestFlask):
|
|||
assert 'bar stuff 2' == bar_options['STUFF_2']
|
||||
|
||||
|
||||
class TestInstance(TestFlask):
|
||||
class TestInstance(object):
|
||||
def test_explicit_instance_paths(self, apps_tmpdir):
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
flask.Flask(__name__, instance_path='instance')
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
tests.deprecations
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests deprecation support.
|
||||
Tests deprecation support. Not used currently.
|
||||
|
||||
:copyright: (c) 2014 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
|
|
@ -12,13 +12,3 @@
|
|||
import flask
|
||||
import unittest
|
||||
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
|
||||
|
||||
|
||||
class TestJSON(TestFlask):
|
||||
class TestJSON(object):
|
||||
|
||||
def test_json_bad_requests(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
|
@ -241,7 +241,7 @@ class TestJSON(TestFlask):
|
|||
except AssertionError:
|
||||
assert lines == sorted_by_str
|
||||
|
||||
class TestSendfile(TestFlask):
|
||||
class TestSendfile(object):
|
||||
|
||||
def test_send_file_regular(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
|
@ -423,7 +423,7 @@ class TestSendfile(TestFlask):
|
|||
rv.close()
|
||||
|
||||
|
||||
class TestLogging(TestFlask):
|
||||
class TestLogging(object):
|
||||
|
||||
def test_logger_cache(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
|
@ -566,7 +566,7 @@ class TestLogging(TestFlask):
|
|||
assert flask.url_for('myview', _method='POST') == '/myview/create'
|
||||
|
||||
|
||||
class TestNoImports(TestFlask):
|
||||
class TestNoImports(object):
|
||||
"""Test Flasks are created without import.
|
||||
|
||||
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.'
|
||||
|
||||
|
||||
class TestStreaming(TestFlask):
|
||||
class TestStreaming(object):
|
||||
|
||||
def test_streaming_with_context(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
|
@ -641,14 +641,3 @@ class TestStreaming(TestFlask):
|
|||
rv = c.get('/?name=World')
|
||||
assert rv.data == b'Hello World!'
|
||||
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',
|
||||
reason='Turned off due to envvar.')
|
||||
class TestMemory(TestFlask):
|
||||
class TestMemory(object):
|
||||
|
||||
def assert_no_leak(self):
|
||||
return _NoLeakAsserter(self)
|
||||
|
|
@ -88,7 +88,7 @@ class TestMemory(TestFlask):
|
|||
safe_join('/foo', '..')
|
||||
|
||||
|
||||
class TestException(TestFlask):
|
||||
class TestException(object):
|
||||
|
||||
def test_aborting(self):
|
||||
class Foo(Exception):
|
||||
|
|
|
|||
|
|
@ -20,177 +20,166 @@ except ImportError:
|
|||
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):
|
||||
buffer = []
|
||||
app = flask.Flask(__name__)
|
||||
@app.teardown_request
|
||||
def end_of_request(exception):
|
||||
buffer.append(exception)
|
||||
ctx = app.test_request_context()
|
||||
ctx.push()
|
||||
assert buffer == []
|
||||
ctx.pop()
|
||||
assert buffer == [None]
|
||||
|
||||
ctx = app.test_request_context()
|
||||
ctx.push()
|
||||
def test_teardown_with_previous_exception():
|
||||
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 == []
|
||||
ctx.pop()
|
||||
assert buffer == [None]
|
||||
assert buffer == [None]
|
||||
|
||||
def test_teardown_with_previous_exception(self):
|
||||
buffer = []
|
||||
app = flask.Flask(__name__)
|
||||
@app.teardown_request
|
||||
def end_of_request(exception):
|
||||
buffer.append(exception)
|
||||
def test_proper_test_request_context():
|
||||
app = flask.Flask(__name__)
|
||||
app.config.update(
|
||||
SERVER_NAME='localhost.localdomain:5000'
|
||||
)
|
||||
|
||||
try:
|
||||
raise Exception('dummy')
|
||||
except Exception:
|
||||
@app.route('/')
|
||||
def index():
|
||||
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
|
||||
|
||||
with app.test_request_context():
|
||||
assert buffer == []
|
||||
assert buffer == [None]
|
||||
|
||||
def test_proper_test_request_context(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.config.update(
|
||||
SERVER_NAME='localhost.localdomain:5000'
|
||||
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.route('/')
|
||||
def index():
|
||||
return None
|
||||
app.config.update(SERVER_NAME='localhost')
|
||||
with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost'}):
|
||||
pass
|
||||
|
||||
@app.route('/', subdomain='foo')
|
||||
def sub():
|
||||
return None
|
||||
app.config.update(SERVER_NAME='localhost:80')
|
||||
with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost:80'}):
|
||||
pass
|
||||
|
||||
with app.test_request_context('/'):
|
||||
assert flask.url_for('index', _external=True) == \
|
||||
'http://localhost.localdomain:5000/'
|
||||
def test_context_binding():
|
||||
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('/'):
|
||||
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()
|
||||
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():
|
||||
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()
|
||||
try:
|
||||
index()
|
||||
except RuntimeError:
|
||||
pass
|
||||
else:
|
||||
assert 0, 'expected runtime error'
|
||||
|
||||
def test_greenlet_context_copying(self):
|
||||
app = flask.Flask(__name__)
|
||||
greenlets = []
|
||||
def test_manual_context_binding():
|
||||
app = flask.Flask(__name__)
|
||||
@app.route('/')
|
||||
def index():
|
||||
return 'Hello %s!' % flask.request.args['name']
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
reqctx = flask._request_ctx_stack.top.copy()
|
||||
def g():
|
||||
assert not flask.request
|
||||
assert not flask.current_app
|
||||
with reqctx:
|
||||
assert flask.request
|
||||
assert flask.current_app == app
|
||||
assert flask.request.path == '/'
|
||||
assert flask.request.args['foo'] == 'bar'
|
||||
assert not flask.request
|
||||
return 42
|
||||
greenlets.append(greenlet(g))
|
||||
return 'Hello World!'
|
||||
ctx = app.test_request_context('/?name=World')
|
||||
ctx.push()
|
||||
assert index() == 'Hello World!'
|
||||
ctx.pop()
|
||||
try:
|
||||
index()
|
||||
except RuntimeError:
|
||||
pass
|
||||
else:
|
||||
assert 0, 'expected runtime error'
|
||||
|
||||
rv = app.test_client().get('/?foo=bar')
|
||||
assert rv.data == b'Hello World!'
|
||||
@pytest.mark.skipif(greenlet is None, reason='greenlet not installed')
|
||||
def test_greenlet_context_copying():
|
||||
app = flask.Flask(__name__)
|
||||
greenlets = []
|
||||
|
||||
result = greenlets[0].run()
|
||||
assert result == 42
|
||||
|
||||
def test_greenlet_context_copying_api(self):
|
||||
app = flask.Flask(__name__)
|
||||
greenlets = []
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
reqctx = flask._request_ctx_stack.top.copy()
|
||||
@flask.copy_current_request_context
|
||||
def g():
|
||||
@app.route('/')
|
||||
def index():
|
||||
reqctx = flask._request_ctx_stack.top.copy()
|
||||
def g():
|
||||
assert not flask.request
|
||||
assert not flask.current_app
|
||||
with reqctx:
|
||||
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!'
|
||||
assert not flask.request
|
||||
return 42
|
||||
greenlets.append(greenlet(g))
|
||||
return 'Hello World!'
|
||||
|
||||
rv = app.test_client().get('/?foo=bar')
|
||||
assert rv.data == b'Hello World!'
|
||||
rv = app.test_client().get('/?foo=bar')
|
||||
assert rv.data == b'Hello World!'
|
||||
|
||||
result = greenlets[0].run()
|
||||
assert result == 42
|
||||
result = greenlets[0].run()
|
||||
assert result == 42
|
||||
|
||||
# Disable test if we don't have greenlets available
|
||||
if greenlet is None:
|
||||
test_greenlet_context_copying = None
|
||||
test_greenlet_context_copying_api = None
|
||||
@pytest.mark.skipif(greenlet is None, reason='greenlet not installed')
|
||||
def test_greenlet_context_copying_api():
|
||||
app = flask.Flask(__name__)
|
||||
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():
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTest(unittest.makeSuite(TestRequestContext))
|
||||
return suite
|
||||
rv = app.test_client().get('/?foo=bar')
|
||||
assert rv.data == b'Hello World!'
|
||||
|
||||
result = greenlets[0].run()
|
||||
assert result == 42
|
||||
|
|
|
|||
|
|
@ -25,136 +25,134 @@ pytestmark = pytest.mark.skipif(
|
|||
reason='Signals require the blinker library.'
|
||||
)
|
||||
|
||||
class TestSignals(TestFlask):
|
||||
def test_template_rendered():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_template_rendered(self):
|
||||
app = flask.Flask(__name__)
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('simple_template.html', whiskey=42)
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('simple_template.html', whiskey=42)
|
||||
recorded = []
|
||||
|
||||
recorded = []
|
||||
def record(sender, template, context):
|
||||
recorded.append((template, context))
|
||||
|
||||
def record(sender, template, context):
|
||||
recorded.append((template, context))
|
||||
flask.template_rendered.connect(record, app)
|
||||
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)
|
||||
try:
|
||||
app.test_client().get('/')
|
||||
def test_request_signals():
|
||||
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():
|
||||
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
|
||||
template, context = recorded[0]
|
||||
assert template.name == 'simple_template.html'
|
||||
assert context['whiskey'] == 42
|
||||
finally:
|
||||
flask.template_rendered.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)
|
||||
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
|
||||
|
||||
|
||||
class TestTestTools(TestFlask):
|
||||
class TestTestTools(object):
|
||||
|
||||
def test_environ_defaults_from_config(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
|
@ -213,7 +213,7 @@ class TestTestTools(TestFlask):
|
|||
assert 'vodka' in flask.request.args
|
||||
|
||||
|
||||
class TestSubdomain(TestFlask):
|
||||
class TestSubdomain(object):
|
||||
|
||||
@pytest.fixture
|
||||
def app(self, request):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue