WIP
This commit is contained in:
parent
e77bc2d86c
commit
d0cf5ef394
6 changed files with 497 additions and 520 deletions
|
|
@ -90,25 +90,6 @@ class TestFlask(object):
|
||||||
consistency.
|
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):
|
def setup_method(self, method):
|
||||||
self.setup()
|
self.setup()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,34 @@
|
||||||
:copyright: (c) 2014 by the Flask Team, see AUTHORS for more details.
|
:copyright: (c) 2014 by the Flask Team, see AUTHORS for more details.
|
||||||
:license: BSD, see LICENSE for more details.
|
:license: BSD, see LICENSE for more details.
|
||||||
"""
|
"""
|
||||||
|
import flask
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
import pkgutil
|
import pkgutil
|
||||||
import pytest
|
import pytest
|
||||||
import sys
|
|
||||||
import textwrap
|
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))
|
@pytest.fixture(params=(True, False))
|
||||||
def limit_loader(request, monkeypatch):
|
def limit_loader(request, monkeypatch):
|
||||||
"""Patch pkgutil.get_loader to give loader without get_filename or archive.
|
"""Patch pkgutil.get_loader to give loader without get_filename or archive.
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,7 @@ import unittest
|
||||||
from tests import TestFlask
|
from tests import TestFlask
|
||||||
|
|
||||||
|
|
||||||
class TestAppContext(TestFlask):
|
def test_basic_url_generation():
|
||||||
|
|
||||||
def test_basic_url_generation(self):
|
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
app.config['SERVER_NAME'] = 'localhost'
|
app.config['SERVER_NAME'] = 'localhost'
|
||||||
app.config['PREFERRED_URL_SCHEME'] = 'https'
|
app.config['PREFERRED_URL_SCHEME'] = 'https'
|
||||||
|
|
@ -31,29 +29,29 @@ class TestAppContext(TestFlask):
|
||||||
rv = flask.url_for('index')
|
rv = flask.url_for('index')
|
||||||
assert rv == 'https://localhost/'
|
assert rv == 'https://localhost/'
|
||||||
|
|
||||||
def test_url_generation_requires_server_name(self):
|
def test_url_generation_requires_server_name():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
with pytest.raises(RuntimeError):
|
with pytest.raises(RuntimeError):
|
||||||
flask.url_for('index')
|
flask.url_for('index')
|
||||||
|
|
||||||
def test_url_generation_without_context_fails(self):
|
def test_url_generation_without_context_fails():
|
||||||
with pytest.raises(RuntimeError):
|
with pytest.raises(RuntimeError):
|
||||||
flask.url_for('index')
|
flask.url_for('index')
|
||||||
|
|
||||||
def test_request_context_means_app_context(self):
|
def test_request_context_means_app_context():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
with app.test_request_context():
|
with app.test_request_context():
|
||||||
assert flask.current_app._get_current_object() == app
|
assert flask.current_app._get_current_object() == app
|
||||||
assert flask._app_ctx_stack.top == None
|
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__)
|
app = flask.Flask(__name__)
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
assert flask.current_app._get_current_object() == app
|
assert flask.current_app._get_current_object() == app
|
||||||
assert flask._app_ctx_stack.top == None
|
assert flask._app_ctx_stack.top == None
|
||||||
|
|
||||||
def test_app_tearing_down(self):
|
def test_app_tearing_down():
|
||||||
cleanup_stuff = []
|
cleanup_stuff = []
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
@app.teardown_appcontext
|
@app.teardown_appcontext
|
||||||
|
|
@ -65,7 +63,7 @@ class TestAppContext(TestFlask):
|
||||||
|
|
||||||
assert cleanup_stuff == [None]
|
assert cleanup_stuff == [None]
|
||||||
|
|
||||||
def test_app_tearing_down_with_previous_exception(self):
|
def test_app_tearing_down_with_previous_exception():
|
||||||
cleanup_stuff = []
|
cleanup_stuff = []
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
@app.teardown_appcontext
|
@app.teardown_appcontext
|
||||||
|
|
@ -82,7 +80,7 @@ class TestAppContext(TestFlask):
|
||||||
|
|
||||||
assert cleanup_stuff == [None]
|
assert cleanup_stuff == [None]
|
||||||
|
|
||||||
def test_custom_app_ctx_globals_class(self):
|
def test_custom_app_ctx_globals_class():
|
||||||
class CustomRequestGlobals(object):
|
class CustomRequestGlobals(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.spam = 'eggs'
|
self.spam = 'eggs'
|
||||||
|
|
@ -91,7 +89,7 @@ class TestAppContext(TestFlask):
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
assert flask.render_template_string('{{ g.spam }}') == 'eggs'
|
assert flask.render_template_string('{{ g.spam }}') == 'eggs'
|
||||||
|
|
||||||
def test_context_refcounts(self):
|
def test_context_refcounts():
|
||||||
called = []
|
called = []
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
@app.teardown_request
|
@app.teardown_request
|
||||||
|
|
@ -113,9 +111,3 @@ class TestAppContext(TestFlask):
|
||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
assert res.data == b''
|
assert res.data == b''
|
||||||
assert called == ['request', 'app']
|
assert called == ['request', 'app']
|
||||||
|
|
||||||
|
|
||||||
def suite():
|
|
||||||
suite = unittest.TestSuite()
|
|
||||||
suite.addTest(unittest.makeSuite(TestAppContext))
|
|
||||||
return suite
|
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,9 @@ except ImportError:
|
||||||
from tests import TestFlask
|
from tests import TestFlask
|
||||||
from flask._compat import PY2
|
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
|
# we clear this out for various reasons. The most important one is
|
||||||
# that a real flaskext could be in there which would disable our
|
# that a real flaskext could be in there which would disable our
|
||||||
# fake package. Secondly we want to make sure that the flaskext
|
# fake package. Secondly we want to make sure that the flaskext
|
||||||
|
|
@ -32,7 +32,7 @@ class TestExtImportHook(TestFlask):
|
||||||
entry.startswith('flask_') or
|
entry.startswith('flask_') or
|
||||||
entry.startswith('flaskext.') or
|
entry.startswith('flaskext.') or
|
||||||
entry == 'flaskext') and value is not None:
|
entry == 'flaskext') and value is not None:
|
||||||
sys.modules.pop(entry, None)
|
monkeypatch.delitem(sys.modules, entry)
|
||||||
from flask import ext
|
from flask import ext
|
||||||
reload_module(ext)
|
reload_module(ext)
|
||||||
|
|
||||||
|
|
@ -45,71 +45,73 @@ class TestExtImportHook(TestFlask):
|
||||||
import_hooks += 1
|
import_hooks += 1
|
||||||
assert import_hooks == 1
|
assert import_hooks == 1
|
||||||
|
|
||||||
def teardown(self):
|
def teardown():
|
||||||
from flask import ext
|
from flask import ext
|
||||||
for key in ext.__dict__:
|
for key in ext.__dict__:
|
||||||
assert '.' not in key
|
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
|
from flask.ext.newext_simple import ext_id
|
||||||
assert ext_id == 'newext_simple'
|
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
|
from flask.ext import newext_simple
|
||||||
assert newext_simple.ext_id == 'newext_simple'
|
assert newext_simple.ext_id == 'newext_simple'
|
||||||
assert newext_simple.__name__ == 'flask_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
|
from flask.ext.newext_package import ext_id
|
||||||
assert ext_id == 'newext_package'
|
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
|
from flask.ext import newext_package
|
||||||
assert newext_package.ext_id == 'newext_package'
|
assert newext_package.ext_id == 'newext_package'
|
||||||
assert newext_package.__name__ == 'flask_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
|
from flask.ext.newext_package.submodule import test_function
|
||||||
assert test_function() == 42
|
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
|
from flask.ext.newext_package import submodule
|
||||||
assert submodule.__name__ == 'flask_newext_package.submodule'
|
assert submodule.__name__ == 'flask_newext_package.submodule'
|
||||||
assert submodule.test_function() == 42
|
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
|
from flask.ext.oldext_simple import ext_id
|
||||||
assert ext_id == 'oldext_simple'
|
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
|
from flask.ext import oldext_simple
|
||||||
assert oldext_simple.ext_id == 'oldext_simple'
|
assert oldext_simple.ext_id == 'oldext_simple'
|
||||||
assert oldext_simple.__name__ == 'flaskext.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
|
from flask.ext.oldext_package import ext_id
|
||||||
assert ext_id == 'oldext_package'
|
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
|
from flask.ext import oldext_package
|
||||||
assert oldext_package.ext_id == 'oldext_package'
|
assert oldext_package.ext_id == 'oldext_package'
|
||||||
assert oldext_package.__name__ == 'flaskext.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
|
from flask.ext.oldext_package import submodule
|
||||||
assert submodule.__name__ == 'flaskext.oldext_package.submodule'
|
assert submodule.__name__ == 'flaskext.oldext_package.submodule'
|
||||||
assert submodule.test_function() == 42
|
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
|
from flask.ext.oldext_package.submodule import test_function
|
||||||
assert test_function() == 42
|
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):
|
for x in range(2):
|
||||||
with pytest.raises(ImportError):
|
with pytest.raises(ImportError):
|
||||||
import flask.ext.broken
|
import flask.ext.broken
|
||||||
|
|
||||||
def test_no_error_swallowing(self):
|
def test_no_error_swallowing():
|
||||||
try:
|
try:
|
||||||
import flask.ext.broken
|
import flask.ext.broken
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|
@ -131,9 +133,5 @@ class TestExtImportHook(TestFlask):
|
||||||
import os.path
|
import os.path
|
||||||
assert os.path.join('flask_broken', '__init__.py') in \
|
assert os.path.join('flask_broken', '__init__.py') in \
|
||||||
next.tb_frame.f_code.co_filename
|
next.tb_frame.f_code.co_filename
|
||||||
|
else:
|
||||||
|
1/0 # XXX
|
||||||
def suite():
|
|
||||||
suite = unittest.TestSuite()
|
|
||||||
suite.addTest(unittest.makeSuite(TestExtImportHook))
|
|
||||||
return suite
|
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,7 @@ from tests import TestFlask
|
||||||
from flask._compat import StringIO
|
from flask._compat import StringIO
|
||||||
|
|
||||||
|
|
||||||
class TestFlaskSubclassing(TestFlask):
|
def test_suppressed_exception_logging():
|
||||||
|
|
||||||
def test_suppressed_exception_logging(self):
|
|
||||||
class SuppressedFlask(flask.Flask):
|
class SuppressedFlask(flask.Flask):
|
||||||
def log_exception(self, exc_info):
|
def log_exception(self, exc_info):
|
||||||
pass
|
pass
|
||||||
|
|
@ -38,9 +36,3 @@ class TestFlaskSubclassing(TestFlask):
|
||||||
|
|
||||||
err = out.getvalue()
|
err = out.getvalue()
|
||||||
assert err == ''
|
assert err == ''
|
||||||
|
|
||||||
|
|
||||||
def suite():
|
|
||||||
suite = unittest.TestSuite()
|
|
||||||
suite.addTest(unittest.makeSuite(TestFlaskSubclassing))
|
|
||||||
return suite
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@
|
||||||
:license: BSD, see LICENSE for more details.
|
:license: BSD, see LICENSE for more details.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
import flask
|
import flask
|
||||||
import unittest
|
import unittest
|
||||||
import logging
|
import logging
|
||||||
|
|
@ -17,9 +19,7 @@ from jinja2 import TemplateNotFound
|
||||||
from tests import TestFlask
|
from tests import TestFlask
|
||||||
|
|
||||||
|
|
||||||
class TestTemplating(TestFlask):
|
def test_context_processing():
|
||||||
|
|
||||||
def test_context_processing(self):
|
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
def context_processor():
|
def context_processor():
|
||||||
|
|
@ -30,7 +30,7 @@ class TestTemplating(TestFlask):
|
||||||
rv = app.test_client().get('/')
|
rv = app.test_client().get('/')
|
||||||
assert rv.data == b'<p>23|42'
|
assert rv.data == b'<p>23|42'
|
||||||
|
|
||||||
def test_original_win(self):
|
def test_original_win():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
|
|
@ -38,7 +38,7 @@ class TestTemplating(TestFlask):
|
||||||
rv = app.test_client().get('/')
|
rv = app.test_client().get('/')
|
||||||
assert rv.data == b'42'
|
assert rv.data == b'42'
|
||||||
|
|
||||||
def test_request_less_rendering(self):
|
def test_request_less_rendering():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
app.config['WORLD_NAME'] = 'Special World'
|
app.config['WORLD_NAME'] = 'Special World'
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
|
|
@ -50,7 +50,7 @@ class TestTemplating(TestFlask):
|
||||||
'{{ foo }}')
|
'{{ foo }}')
|
||||||
assert rv == 'Hello Special World 42'
|
assert rv == 'Hello Special World 42'
|
||||||
|
|
||||||
def test_standard_context(self):
|
def test_standard_context():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
app.secret_key = 'development key'
|
app.secret_key = 'development key'
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
|
|
@ -66,7 +66,7 @@ class TestTemplating(TestFlask):
|
||||||
rv = app.test_client().get('/?foo=42')
|
rv = app.test_client().get('/?foo=42')
|
||||||
assert rv.data.split() == [b'42', b'23', b'False', b'aha']
|
assert rv.data.split() == [b'42', b'23', b'False', b'aha']
|
||||||
|
|
||||||
def test_escaping(self):
|
def test_escaping():
|
||||||
text = '<p>Hello World!'
|
text = '<p>Hello World!'
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
|
|
@ -83,7 +83,7 @@ class TestTemplating(TestFlask):
|
||||||
b'<p>Hello World!'
|
b'<p>Hello World!'
|
||||||
]
|
]
|
||||||
|
|
||||||
def test_no_escaping(self):
|
def test_no_escaping():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
with app.test_request_context():
|
with app.test_request_context():
|
||||||
assert flask.render_template_string(
|
assert flask.render_template_string(
|
||||||
|
|
@ -91,13 +91,13 @@ class TestTemplating(TestFlask):
|
||||||
assert flask.render_template('mail.txt', foo='<test>') == \
|
assert flask.render_template('mail.txt', foo='<test>') == \
|
||||||
'<test> Mail'
|
'<test> Mail'
|
||||||
|
|
||||||
def test_macros(self):
|
def test_macros():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
with app.test_request_context():
|
with app.test_request_context():
|
||||||
macro = flask.get_template_attribute('_macro.html', 'hello')
|
macro = flask.get_template_attribute('_macro.html', 'hello')
|
||||||
assert macro('World') == 'Hello World!'
|
assert macro('World') == 'Hello World!'
|
||||||
|
|
||||||
def test_template_filter(self):
|
def test_template_filter():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
@app.template_filter()
|
@app.template_filter()
|
||||||
def my_reverse(s):
|
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'] == my_reverse
|
||||||
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba'
|
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba'
|
||||||
|
|
||||||
def test_add_template_filter(self):
|
def test_add_template_filter():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
def my_reverse(s):
|
def my_reverse(s):
|
||||||
return s[::-1]
|
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'] == my_reverse
|
||||||
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba'
|
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 = flask.Flask(__name__)
|
||||||
@app.template_filter('strrev')
|
@app.template_filter('strrev')
|
||||||
def my_reverse(s):
|
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'] == my_reverse
|
||||||
assert app.jinja_env.filters['strrev']('abcd') == 'dcba'
|
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__)
|
app = flask.Flask(__name__)
|
||||||
def my_reverse(s):
|
def my_reverse(s):
|
||||||
return s[::-1]
|
return s[::-1]
|
||||||
|
|
@ -133,7 +133,7 @@ class TestTemplating(TestFlask):
|
||||||
assert app.jinja_env.filters['strrev'] == my_reverse
|
assert app.jinja_env.filters['strrev'] == my_reverse
|
||||||
assert app.jinja_env.filters['strrev']('abcd') == 'dcba'
|
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 = flask.Flask(__name__)
|
||||||
@app.template_filter()
|
@app.template_filter()
|
||||||
def super_reverse(s):
|
def super_reverse(s):
|
||||||
|
|
@ -144,7 +144,7 @@ class TestTemplating(TestFlask):
|
||||||
rv = app.test_client().get('/')
|
rv = app.test_client().get('/')
|
||||||
assert rv.data == b'dcba'
|
assert rv.data == b'dcba'
|
||||||
|
|
||||||
def test_add_template_filter_with_template(self):
|
def test_add_template_filter_with_template():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
def super_reverse(s):
|
def super_reverse(s):
|
||||||
return s[::-1]
|
return s[::-1]
|
||||||
|
|
@ -155,7 +155,7 @@ class TestTemplating(TestFlask):
|
||||||
rv = app.test_client().get('/')
|
rv = app.test_client().get('/')
|
||||||
assert rv.data == b'dcba'
|
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 = flask.Flask(__name__)
|
||||||
@app.template_filter('super_reverse')
|
@app.template_filter('super_reverse')
|
||||||
def my_reverse(s):
|
def my_reverse(s):
|
||||||
|
|
@ -166,7 +166,7 @@ class TestTemplating(TestFlask):
|
||||||
rv = app.test_client().get('/')
|
rv = app.test_client().get('/')
|
||||||
assert rv.data == b'dcba'
|
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__)
|
app = flask.Flask(__name__)
|
||||||
def my_reverse(s):
|
def my_reverse(s):
|
||||||
return s[::-1]
|
return s[::-1]
|
||||||
|
|
@ -177,7 +177,7 @@ class TestTemplating(TestFlask):
|
||||||
rv = app.test_client().get('/')
|
rv = app.test_client().get('/')
|
||||||
assert rv.data == b'dcba'
|
assert rv.data == b'dcba'
|
||||||
|
|
||||||
def test_template_test(self):
|
def test_template_test():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
@app.template_test()
|
@app.template_test()
|
||||||
def boolean(value):
|
def boolean(value):
|
||||||
|
|
@ -186,7 +186,7 @@ class TestTemplating(TestFlask):
|
||||||
assert app.jinja_env.tests['boolean'] == boolean
|
assert app.jinja_env.tests['boolean'] == boolean
|
||||||
assert app.jinja_env.tests['boolean'](False)
|
assert app.jinja_env.tests['boolean'](False)
|
||||||
|
|
||||||
def test_add_template_test(self):
|
def test_add_template_test():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
def boolean(value):
|
def boolean(value):
|
||||||
return isinstance(value, bool)
|
return isinstance(value, bool)
|
||||||
|
|
@ -195,7 +195,7 @@ class TestTemplating(TestFlask):
|
||||||
assert app.jinja_env.tests['boolean'] == boolean
|
assert app.jinja_env.tests['boolean'] == boolean
|
||||||
assert app.jinja_env.tests['boolean'](False)
|
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 = flask.Flask(__name__)
|
||||||
@app.template_test('boolean')
|
@app.template_test('boolean')
|
||||||
def is_boolean(value):
|
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'] == is_boolean
|
||||||
assert app.jinja_env.tests['boolean'](False)
|
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__)
|
app = flask.Flask(__name__)
|
||||||
def is_boolean(value):
|
def is_boolean(value):
|
||||||
return isinstance(value, bool)
|
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'] == is_boolean
|
||||||
assert app.jinja_env.tests['boolean'](False)
|
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 = flask.Flask(__name__)
|
||||||
@app.template_test()
|
@app.template_test()
|
||||||
def boolean(value):
|
def boolean(value):
|
||||||
|
|
@ -224,7 +224,7 @@ class TestTemplating(TestFlask):
|
||||||
rv = app.test_client().get('/')
|
rv = app.test_client().get('/')
|
||||||
assert b'Success!' in rv.data
|
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__)
|
app = flask.Flask(__name__)
|
||||||
def boolean(value):
|
def boolean(value):
|
||||||
return isinstance(value, bool)
|
return isinstance(value, bool)
|
||||||
|
|
@ -235,7 +235,7 @@ class TestTemplating(TestFlask):
|
||||||
rv = app.test_client().get('/')
|
rv = app.test_client().get('/')
|
||||||
assert b'Success!' in rv.data
|
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 = flask.Flask(__name__)
|
||||||
@app.template_test('boolean')
|
@app.template_test('boolean')
|
||||||
def is_boolean(value):
|
def is_boolean(value):
|
||||||
|
|
@ -246,7 +246,7 @@ class TestTemplating(TestFlask):
|
||||||
rv = app.test_client().get('/')
|
rv = app.test_client().get('/')
|
||||||
assert b'Success!' in rv.data
|
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__)
|
app = flask.Flask(__name__)
|
||||||
def is_boolean(value):
|
def is_boolean(value):
|
||||||
return isinstance(value, bool)
|
return isinstance(value, bool)
|
||||||
|
|
@ -257,7 +257,7 @@ class TestTemplating(TestFlask):
|
||||||
rv = app.test_client().get('/')
|
rv = app.test_client().get('/')
|
||||||
assert b'Success!' in rv.data
|
assert b'Success!' in rv.data
|
||||||
|
|
||||||
def test_add_template_global(self):
|
def test_add_template_global():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
@app.template_global()
|
@app.template_global()
|
||||||
def get_stuff():
|
def get_stuff():
|
||||||
|
|
@ -269,7 +269,7 @@ class TestTemplating(TestFlask):
|
||||||
rv = flask.render_template_string('{{ get_stuff() }}')
|
rv = flask.render_template_string('{{ get_stuff() }}')
|
||||||
assert rv == '42'
|
assert rv == '42'
|
||||||
|
|
||||||
def test_custom_template_loader(self):
|
def test_custom_template_loader():
|
||||||
class MyFlask(flask.Flask):
|
class MyFlask(flask.Flask):
|
||||||
def create_global_jinja_loader(self):
|
def create_global_jinja_loader(self):
|
||||||
from jinja2 import DictLoader
|
from jinja2 import DictLoader
|
||||||
|
|
@ -282,7 +282,7 @@ class TestTemplating(TestFlask):
|
||||||
rv = c.get('/')
|
rv = c.get('/')
|
||||||
assert rv.data == b'Hello Custom World!'
|
assert rv.data == b'Hello Custom World!'
|
||||||
|
|
||||||
def test_iterable_loader(self):
|
def test_iterable_loader():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
def context_processor():
|
def context_processor():
|
||||||
|
|
@ -298,7 +298,7 @@ class TestTemplating(TestFlask):
|
||||||
rv = app.test_client().get('/')
|
rv = app.test_client().get('/')
|
||||||
assert rv.data == b'<h1>Jameson</h1>'
|
assert rv.data == b'<h1>Jameson</h1>'
|
||||||
|
|
||||||
def test_templates_auto_reload(self):
|
def test_templates_auto_reload():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
assert app.config['TEMPLATES_AUTO_RELOAD']
|
assert app.config['TEMPLATES_AUTO_RELOAD']
|
||||||
assert app.jinja_env.auto_reload
|
assert app.jinja_env.auto_reload
|
||||||
|
|
@ -306,7 +306,7 @@ class TestTemplating(TestFlask):
|
||||||
app.config['TEMPLATES_AUTO_RELOAD'] = False
|
app.config['TEMPLATES_AUTO_RELOAD'] = False
|
||||||
assert not app.jinja_env.auto_reload
|
assert not app.jinja_env.auto_reload
|
||||||
|
|
||||||
def test_template_loader_debugging(self):
|
def test_template_loader_debugging():
|
||||||
from blueprintapp import app
|
from blueprintapp import app
|
||||||
|
|
||||||
called = []
|
called = []
|
||||||
|
|
@ -331,20 +331,12 @@ class TestTemplating(TestFlask):
|
||||||
app.logger.handlers = [_TestHandler()]
|
app.logger.handlers = [_TestHandler()]
|
||||||
app.config['EXPLAIN_TEMPLATE_LOADING'] = True
|
app.config['EXPLAIN_TEMPLATE_LOADING'] = True
|
||||||
|
|
||||||
try:
|
with pytest.raises(TemplateNotFound) as excinfo:
|
||||||
c.get('/missing')
|
c.get('/missing')
|
||||||
except TemplateNotFound as e:
|
|
||||||
assert 'missing_template.html' in str(e)
|
assert 'missing_template.html' in str(excinfo.value)
|
||||||
else:
|
|
||||||
self.fail('Expected template not found exception.')
|
|
||||||
finally:
|
finally:
|
||||||
app.logger.handlers[:] = old_handlers
|
app.logger.handlers[:] = old_handlers
|
||||||
app.config['EXPLAIN_TEMPLATE_LOADING'] = old_load_setting
|
app.config['EXPLAIN_TEMPLATE_LOADING'] = old_load_setting
|
||||||
|
|
||||||
assert len(called) == 1
|
assert len(called) == 1
|
||||||
|
|
||||||
|
|
||||||
def suite():
|
|
||||||
suite = unittest.TestSuite()
|
|
||||||
suite.addTest(unittest.makeSuite(TestTemplating))
|
|
||||||
return suite
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue