Browse Source

Remove useless classes

pull/1165/head
Markus Unterwaditzer 11 years ago
parent
commit
af41dbe0c4
  1. 4
      tests/test_basic.py
  2. 1031
      tests/test_blueprints.py
  3. 4
      tests/test_config.py
  4. 12
      tests/test_deprecations.py
  5. 38
      tests/test_examples.py
  6. 21
      tests/test_helpers.py
  7. 4
      tests/test_regression.py
  8. 309
      tests/test_reqctx.py
  9. 262
      tests/test_signals.py
  10. 4
      tests/test_testing.py

4
tests/test_basic.py

@ -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__)

1031
tests/test_blueprints.py

File diff suppressed because it is too large Load Diff

4
tests/test_config.py

@ -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')

12
tests/test_deprecations.py

@ -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

38
tests/test_examples.py

@ -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

21
tests/test_helpers.py

@ -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

4
tests/test_regression.py

@ -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):

309
tests/test_reqctx.py

@ -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)
ctx = app.test_request_context()
ctx.push()
assert buffer == []
ctx.pop()
assert buffer == [None]
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 == []
assert buffer == [None]
def test_teardown_on_pop(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)
ctx = app.test_request_context() @app.route('/')
ctx.push() def index():
assert buffer == [] return None
ctx.pop()
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)
try:
raise Exception('dummy')
except Exception:
pass
with app.test_request_context(): @app.route('/', subdomain='foo')
assert buffer == [] def sub():
assert buffer == [None] return None
def test_proper_test_request_context(self): with app.test_request_context('/'):
app = flask.Flask(__name__) assert flask.url_for('index', _external=True) == \
app.config.update( 'http://localhost.localdomain:5000/'
SERVER_NAME='localhost.localdomain:5000'
)
@app.route('/') with app.test_request_context('/'):
def index(): assert flask.url_for('sub', _external=True) == \
return None 'http://foo.localhost.localdomain:5000/'
@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
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') try:
with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost:80'}): with app.test_request_context('/', environ_overrides={'HTTP_HOST': 'localhost'}):
pass 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')"
)
def test_context_binding(self): app.config.update(SERVER_NAME='localhost')
app = flask.Flask(__name__) with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost'}):
@app.route('/') pass
def index():
return 'Hello %s!' % flask.request.args['name'] app.config.update(SERVER_NAME='localhost:80')
@app.route('/meh') with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost:80'}):
def meh(): pass
return flask.request.url
def test_context_binding():
with app.test_request_context('/?name=World'): app = flask.Flask(__name__)
assert index() == 'Hello World!' @app.route('/')
with app.test_request_context('/meh'): def index():
assert meh() == 'http://localhost/meh' return 'Hello %s!' % flask.request.args['name']
assert flask._request_ctx_stack.top is None @app.route('/meh')
def meh():
def test_context_test(self): return flask.request.url
app = flask.Flask(__name__)
assert not flask.request with app.test_request_context('/?name=World'):
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() def test_manual_context_binding():
except RuntimeError: app = flask.Flask(__name__)
pass @app.route('/')
else: def index():
assert 0, 'expected runtime error' return 'Hello %s!' % flask.request.args['name']
def test_greenlet_context_copying(self): ctx = app.test_request_context('/?name=World')
app = flask.Flask(__name__) ctx.push()
greenlets = [] assert index() == 'Hello World!'
ctx.pop()
@app.route('/') try:
def index(): index()
reqctx = flask._request_ctx_stack.top.copy() except RuntimeError:
def g(): pass
assert not flask.request else:
assert not flask.current_app assert 0, 'expected runtime error'
with reqctx:
assert flask.request @pytest.mark.skipif(greenlet is None, reason='greenlet not installed')
assert flask.current_app == app def test_greenlet_context_copying():
assert flask.request.path == '/' app = flask.Flask(__name__)
assert flask.request.args['foo'] == 'bar' greenlets = []
assert not flask.request
return 42 @app.route('/')
greenlets.append(greenlet(g)) def index():
return 'Hello World!' reqctx = flask._request_ctx_stack.top.copy()
def g():
rv = app.test_client().get('/?foo=bar') assert not flask.request
assert rv.data == b'Hello World!' assert not flask.current_app
with reqctx:
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():
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')
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: @pytest.mark.skipif(greenlet is None, reason='greenlet not installed')
test_greenlet_context_copying = None def test_greenlet_context_copying_api():
test_greenlet_context_copying_api = None app = flask.Flask(__name__)
greenlets = []
def suite(): @app.route('/')
suite = unittest.TestSuite() def index():
suite.addTest(unittest.makeSuite(TestRequestContext)) reqctx = flask._request_ctx_stack.top.copy()
return suite @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!'
rv = app.test_client().get('/?foo=bar')
assert rv.data == b'Hello World!'
result = greenlets[0].run()
assert result == 42

262
tests/test_signals.py

@ -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 = flask.Flask(__name__) @app.route('/')
def index():
@app.route('/') return flask.render_template('simple_template.html', whiskey=42)
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:
flask.template_rendered.connect(record, app) app.test_client().get('/')
try: assert len(recorded) == 1
app.test_client().get('/') template, context = recorded[0]
assert len(recorded) == 1 assert template.name == 'simple_template.html'
template, context = recorded[0] assert context['whiskey'] == 42
assert template.name == 'simple_template.html' finally:
assert context['whiskey'] == 42 flask.template_rendered.disconnect(record, app)
finally:
flask.template_rendered.disconnect(record, app) def test_request_signals():
app = flask.Flask(__name__)
def test_request_signals(self): calls = []
app = flask.Flask(__name__)
calls = [] def before_request_signal(sender):
calls.append('before-signal')
def before_request_signal(sender):
calls.append('before-signal') def after_request_signal(sender, response):
assert response.data == b'stuff'
def after_request_signal(sender, response): calls.append('after-signal')
assert response.data == b'stuff'
calls.append('after-signal') @app.before_request
def before_request_handler():
@app.before_request calls.append('before-handler')
def before_request_handler():
calls.append('before-handler') @app.after_request
def after_request_handler(response):
@app.after_request calls.append('after-handler')
def after_request_handler(response): response.data = 'stuff'
calls.append('after-handler') return response
response.data = 'stuff'
return response @app.route('/')
def index():
@app.route('/') calls.append('handler')
def index(): return 'ignored anyway'
calls.append('handler')
return 'ignored anyway' flask.request_started.connect(before_request_signal, app)
flask.request_finished.connect(after_request_signal, app)
flask.request_started.connect(before_request_signal, app)
flask.request_finished.connect(after_request_signal, app) try:
rv = app.test_client().get('/')
try: assert rv.data == b'stuff'
rv = app.test_client().get('/')
assert rv.data == b'stuff' assert calls == ['before-signal', 'before-handler', 'handler',
'after-handler', 'after-signal']
assert calls == ['before-signal', 'before-handler', 'handler', finally:
'after-handler', 'after-signal'] flask.request_started.disconnect(before_request_signal, app)
finally: flask.request_finished.disconnect(after_request_signal, app)
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__)
def test_request_exception_signal(self): recorded = []
app = flask.Flask(__name__)
recorded = [] @app.route('/')
def index():
@app.route('/') 1 // 0
def index():
1 // 0 def record(sender, exception):
recorded.append(exception)
def record(sender, exception):
recorded.append(exception) flask.got_request_exception.connect(record, app)
try:
flask.got_request_exception.connect(record, app) assert app.test_client().get('/').status_code == 500
try: assert len(recorded) == 1
assert app.test_client().get('/').status_code == 500 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
assert isinstance(recorded[0], ZeroDivisionError) message, category = recorded[0]
finally: assert message == 'This is a flash message'
flask.got_request_exception.disconnect(record, app) assert category == 'notice'
finally:
def test_appcontext_signals(self): flask.message_flashed.disconnect(record, app)
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)

4
tests/test_testing.py

@ -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…
Cancel
Save