Browse Source

WIP

pull/1165/head
Markus Unterwaditzer 10 years ago
parent
commit
d0cf5ef394
  1. 19
      tests/__init__.py
  2. 24
      tests/conftest.py
  3. 26
      tests/test_appctx.py
  4. 46
      tests/test_ext.py
  5. 10
      tests/test_subclassing.py
  6. 74
      tests/test_templating.py

19
tests/__init__.py

@ -90,25 +90,6 @@ class TestFlask(object):
consistency.
"""
@pytest.fixture(autouse=True)
def setup_path(self, monkeypatch):
monkeypatch.syspath_prepend(
os.path.abspath(os.path.join(
os.path.dirname(__file__), 'test_apps'))
)
@pytest.fixture(autouse=True)
def leak_detector(self, request):
request.addfinalizer(self.ensure_clean_request_context)
def ensure_clean_request_context(self):
# make sure we're not leaking a request context since we are
# testing flask internally in debug mode in a few cases
leaks = []
while flask._request_ctx_stack.top is not None:
leaks.append(flask._request_ctx_stack.pop())
assert leaks == []
def setup_method(self, method):
self.setup()

24
tests/conftest.py

@ -6,12 +6,34 @@
:copyright: (c) 2014 by the Flask Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
import flask
import os
import sys
import pkgutil
import pytest
import sys
import textwrap
@pytest.fixture(autouse=True)
def setup_path(monkeypatch):
monkeypatch.syspath_prepend(
os.path.abspath(os.path.join(
os.path.dirname(__file__), 'test_apps'))
)
@pytest.fixture(autouse=True)
def leak_detector(request):
def ensure_clean_request_context():
# make sure we're not leaking a request context since we are
# testing flask internally in debug mode in a few cases
leaks = []
while flask._request_ctx_stack.top is not None:
leaks.append(flask._request_ctx_stack.pop())
assert leaks == []
request.addfinalizer(ensure_clean_request_context)
@pytest.fixture(params=(True, False))
def limit_loader(request, monkeypatch):
"""Patch pkgutil.get_loader to give loader without get_filename or archive.

26
tests/test_appctx.py

@ -16,9 +16,7 @@ import unittest
from tests import TestFlask
class TestAppContext(TestFlask):
def test_basic_url_generation(self):
def test_basic_url_generation():
app = flask.Flask(__name__)
app.config['SERVER_NAME'] = 'localhost'
app.config['PREFERRED_URL_SCHEME'] = 'https'
@ -31,29 +29,29 @@ class TestAppContext(TestFlask):
rv = flask.url_for('index')
assert rv == 'https://localhost/'
def test_url_generation_requires_server_name(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_url_generation_without_context_fails(self):
def test_url_generation_without_context_fails():
with pytest.raises(RuntimeError):
flask.url_for('index')
def test_request_context_means_app_context(self):
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_context_provides_current_app(self):
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
def test_app_tearing_down(self):
def test_app_tearing_down():
cleanup_stuff = []
app = flask.Flask(__name__)
@app.teardown_appcontext
@ -65,7 +63,7 @@ class TestAppContext(TestFlask):
assert cleanup_stuff == [None]
def test_app_tearing_down_with_previous_exception(self):
def test_app_tearing_down_with_previous_exception():
cleanup_stuff = []
app = flask.Flask(__name__)
@app.teardown_appcontext
@ -82,7 +80,7 @@ class TestAppContext(TestFlask):
assert cleanup_stuff == [None]
def test_custom_app_ctx_globals_class(self):
def test_custom_app_ctx_globals_class():
class CustomRequestGlobals(object):
def __init__(self):
self.spam = 'eggs'
@ -91,7 +89,7 @@ class TestAppContext(TestFlask):
with app.app_context():
assert flask.render_template_string('{{ g.spam }}') == 'eggs'
def test_context_refcounts(self):
def test_context_refcounts():
called = []
app = flask.Flask(__name__)
@app.teardown_request
@ -113,9 +111,3 @@ class TestAppContext(TestFlask):
assert res.status_code == 200
assert res.data == b''
assert called == ['request', 'app']
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestAppContext))
return suite

46
tests/test_ext.py

@ -20,9 +20,9 @@ except ImportError:
from tests import TestFlask
from flask._compat import PY2
class TestExtImportHook(TestFlask):
def setup(self):
@pytest.fixture(autouse=True)
def importhook_setup(monkeypatch, request):
# we clear this out for various reasons. The most important one is
# that a real flaskext could be in there which would disable our
# fake package. Secondly we want to make sure that the flaskext
@ -32,7 +32,7 @@ class TestExtImportHook(TestFlask):
entry.startswith('flask_') or
entry.startswith('flaskext.') or
entry == 'flaskext') and value is not None:
sys.modules.pop(entry, None)
monkeypatch.delitem(sys.modules, entry)
from flask import ext
reload_module(ext)
@ -45,71 +45,73 @@ class TestExtImportHook(TestFlask):
import_hooks += 1
assert import_hooks == 1
def teardown(self):
def teardown():
from flask import ext
for key in ext.__dict__:
assert '.' not in key
def test_flaskext_new_simple_import_normal(self):
request.addfinalizer(teardown)
def test_flaskext_new_simple_import_normal():
from flask.ext.newext_simple import ext_id
assert ext_id == 'newext_simple'
def test_flaskext_new_simple_import_module(self):
def test_flaskext_new_simple_import_module():
from flask.ext import newext_simple
assert newext_simple.ext_id == 'newext_simple'
assert newext_simple.__name__ == 'flask_newext_simple'
def test_flaskext_new_package_import_normal(self):
def test_flaskext_new_package_import_normal():
from flask.ext.newext_package import ext_id
assert ext_id == 'newext_package'
def test_flaskext_new_package_import_module(self):
def test_flaskext_new_package_import_module():
from flask.ext import newext_package
assert newext_package.ext_id == 'newext_package'
assert newext_package.__name__ == 'flask_newext_package'
def test_flaskext_new_package_import_submodule_function(self):
def test_flaskext_new_package_import_submodule_function():
from flask.ext.newext_package.submodule import test_function
assert test_function() == 42
def test_flaskext_new_package_import_submodule(self):
def test_flaskext_new_package_import_submodule():
from flask.ext.newext_package import submodule
assert submodule.__name__ == 'flask_newext_package.submodule'
assert submodule.test_function() == 42
def test_flaskext_old_simple_import_normal(self):
def test_flaskext_old_simple_import_normal():
from flask.ext.oldext_simple import ext_id
assert ext_id == 'oldext_simple'
def test_flaskext_old_simple_import_module(self):
def test_flaskext_old_simple_import_module():
from flask.ext import oldext_simple
assert oldext_simple.ext_id == 'oldext_simple'
assert oldext_simple.__name__ == 'flaskext.oldext_simple'
def test_flaskext_old_package_import_normal(self):
def test_flaskext_old_package_import_normal():
from flask.ext.oldext_package import ext_id
assert ext_id == 'oldext_package'
def test_flaskext_old_package_import_module(self):
def test_flaskext_old_package_import_module():
from flask.ext import oldext_package
assert oldext_package.ext_id == 'oldext_package'
assert oldext_package.__name__ == 'flaskext.oldext_package'
def test_flaskext_old_package_import_submodule(self):
def test_flaskext_old_package_import_submodule():
from flask.ext.oldext_package import submodule
assert submodule.__name__ == 'flaskext.oldext_package.submodule'
assert submodule.test_function() == 42
def test_flaskext_old_package_import_submodule_function(self):
def test_flaskext_old_package_import_submodule_function():
from flask.ext.oldext_package.submodule import test_function
assert test_function() == 42
def test_flaskext_broken_package_no_module_caching(self):
def test_flaskext_broken_package_no_module_caching():
for x in range(2):
with pytest.raises(ImportError):
import flask.ext.broken
def test_no_error_swallowing(self):
def test_no_error_swallowing():
try:
import flask.ext.broken
except ImportError:
@ -131,9 +133,5 @@ class TestExtImportHook(TestFlask):
import os.path
assert os.path.join('flask_broken', '__init__.py') in \
next.tb_frame.f_code.co_filename
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestExtImportHook))
return suite
else:
1/0 # XXX

10
tests/test_subclassing.py

@ -16,9 +16,7 @@ from tests import TestFlask
from flask._compat import StringIO
class TestFlaskSubclassing(TestFlask):
def test_suppressed_exception_logging(self):
def test_suppressed_exception_logging():
class SuppressedFlask(flask.Flask):
def log_exception(self, exc_info):
pass
@ -38,9 +36,3 @@ class TestFlaskSubclassing(TestFlask):
err = out.getvalue()
assert err == ''
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestFlaskSubclassing))
return suite

74
tests/test_templating.py

@ -9,6 +9,8 @@
:license: BSD, see LICENSE for more details.
"""
import pytest
import flask
import unittest
import logging
@ -17,9 +19,7 @@ from jinja2 import TemplateNotFound
from tests import TestFlask
class TestTemplating(TestFlask):
def test_context_processing(self):
def test_context_processing():
app = flask.Flask(__name__)
@app.context_processor
def context_processor():
@ -30,7 +30,7 @@ class TestTemplating(TestFlask):
rv = app.test_client().get('/')
assert rv.data == b'<p>23|42'
def test_original_win(self):
def test_original_win():
app = flask.Flask(__name__)
@app.route('/')
def index():
@ -38,7 +38,7 @@ class TestTemplating(TestFlask):
rv = app.test_client().get('/')
assert rv.data == b'42'
def test_request_less_rendering(self):
def test_request_less_rendering():
app = flask.Flask(__name__)
app.config['WORLD_NAME'] = 'Special World'
@app.context_processor
@ -50,7 +50,7 @@ class TestTemplating(TestFlask):
'{{ foo }}')
assert rv == 'Hello Special World 42'
def test_standard_context(self):
def test_standard_context():
app = flask.Flask(__name__)
app.secret_key = 'development key'
@app.route('/')
@ -66,7 +66,7 @@ class TestTemplating(TestFlask):
rv = app.test_client().get('/?foo=42')
assert rv.data.split() == [b'42', b'23', b'False', b'aha']
def test_escaping(self):
def test_escaping():
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
@ -83,7 +83,7 @@ class TestTemplating(TestFlask):
b'<p>Hello World!'
]
def test_no_escaping(self):
def test_no_escaping():
app = flask.Flask(__name__)
with app.test_request_context():
assert flask.render_template_string(
@ -91,13 +91,13 @@ class TestTemplating(TestFlask):
assert flask.render_template('mail.txt', foo='<test>') == \
'<test> Mail'
def test_macros(self):
def test_macros():
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
assert macro('World') == 'Hello World!'
def test_template_filter(self):
def test_template_filter():
app = flask.Flask(__name__)
@app.template_filter()
def my_reverse(s):
@ -106,7 +106,7 @@ class TestTemplating(TestFlask):
assert app.jinja_env.filters['my_reverse'] == my_reverse
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba'
def test_add_template_filter(self):
def test_add_template_filter():
app = flask.Flask(__name__)
def my_reverse(s):
return s[::-1]
@ -115,7 +115,7 @@ class TestTemplating(TestFlask):
assert app.jinja_env.filters['my_reverse'] == my_reverse
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba'
def test_template_filter_with_name(self):
def test_template_filter_with_name():
app = flask.Flask(__name__)
@app.template_filter('strrev')
def my_reverse(s):
@ -124,7 +124,7 @@ class TestTemplating(TestFlask):
assert app.jinja_env.filters['strrev'] == my_reverse
assert app.jinja_env.filters['strrev']('abcd') == 'dcba'
def test_add_template_filter_with_name(self):
def test_add_template_filter_with_name():
app = flask.Flask(__name__)
def my_reverse(s):
return s[::-1]
@ -133,7 +133,7 @@ class TestTemplating(TestFlask):
assert app.jinja_env.filters['strrev'] == my_reverse
assert app.jinja_env.filters['strrev']('abcd') == 'dcba'
def test_template_filter_with_template(self):
def test_template_filter_with_template():
app = flask.Flask(__name__)
@app.template_filter()
def super_reverse(s):
@ -144,7 +144,7 @@ class TestTemplating(TestFlask):
rv = app.test_client().get('/')
assert rv.data == b'dcba'
def test_add_template_filter_with_template(self):
def test_add_template_filter_with_template():
app = flask.Flask(__name__)
def super_reverse(s):
return s[::-1]
@ -155,7 +155,7 @@ class TestTemplating(TestFlask):
rv = app.test_client().get('/')
assert rv.data == b'dcba'
def test_template_filter_with_name_and_template(self):
def test_template_filter_with_name_and_template():
app = flask.Flask(__name__)
@app.template_filter('super_reverse')
def my_reverse(s):
@ -166,7 +166,7 @@ class TestTemplating(TestFlask):
rv = app.test_client().get('/')
assert rv.data == b'dcba'
def test_add_template_filter_with_name_and_template(self):
def test_add_template_filter_with_name_and_template():
app = flask.Flask(__name__)
def my_reverse(s):
return s[::-1]
@ -177,7 +177,7 @@ class TestTemplating(TestFlask):
rv = app.test_client().get('/')
assert rv.data == b'dcba'
def test_template_test(self):
def test_template_test():
app = flask.Flask(__name__)
@app.template_test()
def boolean(value):
@ -186,7 +186,7 @@ class TestTemplating(TestFlask):
assert app.jinja_env.tests['boolean'] == boolean
assert app.jinja_env.tests['boolean'](False)
def test_add_template_test(self):
def test_add_template_test():
app = flask.Flask(__name__)
def boolean(value):
return isinstance(value, bool)
@ -195,7 +195,7 @@ class TestTemplating(TestFlask):
assert app.jinja_env.tests['boolean'] == boolean
assert app.jinja_env.tests['boolean'](False)
def test_template_test_with_name(self):
def test_template_test_with_name():
app = flask.Flask(__name__)
@app.template_test('boolean')
def is_boolean(value):
@ -204,7 +204,7 @@ class TestTemplating(TestFlask):
assert app.jinja_env.tests['boolean'] == is_boolean
assert app.jinja_env.tests['boolean'](False)
def test_add_template_test_with_name(self):
def test_add_template_test_with_name():
app = flask.Flask(__name__)
def is_boolean(value):
return isinstance(value, bool)
@ -213,7 +213,7 @@ class TestTemplating(TestFlask):
assert app.jinja_env.tests['boolean'] == is_boolean
assert app.jinja_env.tests['boolean'](False)
def test_template_test_with_template(self):
def test_template_test_with_template():
app = flask.Flask(__name__)
@app.template_test()
def boolean(value):
@ -224,7 +224,7 @@ class TestTemplating(TestFlask):
rv = app.test_client().get('/')
assert b'Success!' in rv.data
def test_add_template_test_with_template(self):
def test_add_template_test_with_template():
app = flask.Flask(__name__)
def boolean(value):
return isinstance(value, bool)
@ -235,7 +235,7 @@ class TestTemplating(TestFlask):
rv = app.test_client().get('/')
assert b'Success!' in rv.data
def test_template_test_with_name_and_template(self):
def test_template_test_with_name_and_template():
app = flask.Flask(__name__)
@app.template_test('boolean')
def is_boolean(value):
@ -246,7 +246,7 @@ class TestTemplating(TestFlask):
rv = app.test_client().get('/')
assert b'Success!' in rv.data
def test_add_template_test_with_name_and_template(self):
def test_add_template_test_with_name_and_template():
app = flask.Flask(__name__)
def is_boolean(value):
return isinstance(value, bool)
@ -257,7 +257,7 @@ class TestTemplating(TestFlask):
rv = app.test_client().get('/')
assert b'Success!' in rv.data
def test_add_template_global(self):
def test_add_template_global():
app = flask.Flask(__name__)
@app.template_global()
def get_stuff():
@ -269,7 +269,7 @@ class TestTemplating(TestFlask):
rv = flask.render_template_string('{{ get_stuff() }}')
assert rv == '42'
def test_custom_template_loader(self):
def test_custom_template_loader():
class MyFlask(flask.Flask):
def create_global_jinja_loader(self):
from jinja2 import DictLoader
@ -282,7 +282,7 @@ class TestTemplating(TestFlask):
rv = c.get('/')
assert rv.data == b'Hello Custom World!'
def test_iterable_loader(self):
def test_iterable_loader():
app = flask.Flask(__name__)
@app.context_processor
def context_processor():
@ -298,7 +298,7 @@ class TestTemplating(TestFlask):
rv = app.test_client().get('/')
assert rv.data == b'<h1>Jameson</h1>'
def test_templates_auto_reload(self):
def test_templates_auto_reload():
app = flask.Flask(__name__)
assert app.config['TEMPLATES_AUTO_RELOAD']
assert app.jinja_env.auto_reload
@ -306,7 +306,7 @@ class TestTemplating(TestFlask):
app.config['TEMPLATES_AUTO_RELOAD'] = False
assert not app.jinja_env.auto_reload
def test_template_loader_debugging(self):
def test_template_loader_debugging():
from blueprintapp import app
called = []
@ -331,20 +331,12 @@ class TestTemplating(TestFlask):
app.logger.handlers = [_TestHandler()]
app.config['EXPLAIN_TEMPLATE_LOADING'] = True
try:
with pytest.raises(TemplateNotFound) as excinfo:
c.get('/missing')
except TemplateNotFound as e:
assert 'missing_template.html' in str(e)
else:
self.fail('Expected template not found exception.')
assert 'missing_template.html' in str(excinfo.value)
finally:
app.logger.handlers[:] = old_handlers
app.config['EXPLAIN_TEMPLATE_LOADING'] = old_load_setting
assert len(called) == 1
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestTemplating))
return suite

Loading…
Cancel
Save