|
|
|
@ -19,9 +19,7 @@ from werkzeug.http import parse_cache_control_header
|
|
|
|
|
from jinja2 import TemplateNotFound |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestBlueprint(TestFlask): |
|
|
|
|
|
|
|
|
|
def test_blueprint_specific_error_handling(self): |
|
|
|
|
def test_blueprint_specific_error_handling(): |
|
|
|
|
frontend = flask.Blueprint('frontend', __name__) |
|
|
|
|
backend = flask.Blueprint('backend', __name__) |
|
|
|
|
sideend = flask.Blueprint('sideend', __name__) |
|
|
|
@ -61,7 +59,7 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
assert c.get('/backend-no').data == b'backend says no' |
|
|
|
|
assert c.get('/what-is-a-sideend').data == b'application itself says no' |
|
|
|
|
|
|
|
|
|
def test_blueprint_specific_user_error_handling(self): |
|
|
|
|
def test_blueprint_specific_user_error_handling(): |
|
|
|
|
class MyDecoratorException(Exception): |
|
|
|
|
pass |
|
|
|
|
class MyFunctionException(Exception): |
|
|
|
@ -94,7 +92,7 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
assert c.get('/decorator').data == b'boom' |
|
|
|
|
assert c.get('/function').data == b'bam' |
|
|
|
|
|
|
|
|
|
def test_blueprint_url_definitions(self): |
|
|
|
|
def test_blueprint_url_definitions(): |
|
|
|
|
bp = flask.Blueprint('test', __name__) |
|
|
|
|
|
|
|
|
|
@bp.route('/foo', defaults={'baz': 42}) |
|
|
|
@ -115,7 +113,7 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
assert c.get('/1/bar').data == b'23' |
|
|
|
|
assert c.get('/2/bar').data == b'19' |
|
|
|
|
|
|
|
|
|
def test_blueprint_url_processors(self): |
|
|
|
|
def test_blueprint_url_processors(): |
|
|
|
|
bp = flask.Blueprint('frontend', __name__, url_prefix='/<lang_code>') |
|
|
|
|
|
|
|
|
|
@bp.url_defaults |
|
|
|
@ -142,7 +140,7 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
assert c.get('/de/').data == b'/de/about' |
|
|
|
|
assert c.get('/de/about').data == b'/de/' |
|
|
|
|
|
|
|
|
|
def test_templates_and_static(self): |
|
|
|
|
def test_templates_and_static(): |
|
|
|
|
from blueprintapp import app |
|
|
|
|
c = app.test_client() |
|
|
|
|
|
|
|
|
@ -187,7 +185,7 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
with flask.Flask(__name__).test_request_context(): |
|
|
|
|
assert flask.render_template('nested/nested.txt') == 'I\'m nested' |
|
|
|
|
|
|
|
|
|
def test_default_static_cache_timeout(self): |
|
|
|
|
def test_default_static_cache_timeout(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
class MyBlueprint(flask.Blueprint): |
|
|
|
|
def get_send_file_max_age(self, filename): |
|
|
|
@ -211,12 +209,12 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
finally: |
|
|
|
|
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = max_age_default |
|
|
|
|
|
|
|
|
|
def test_templates_list(self): |
|
|
|
|
def test_templates_list(): |
|
|
|
|
from blueprintapp import app |
|
|
|
|
templates = sorted(app.jinja_env.list_templates()) |
|
|
|
|
assert templates == ['admin/index.html', 'frontend/index.html'] |
|
|
|
|
|
|
|
|
|
def test_dotted_names(self): |
|
|
|
|
def test_dotted_names(): |
|
|
|
|
frontend = flask.Blueprint('myapp.frontend', __name__) |
|
|
|
|
backend = flask.Blueprint('myapp.backend', __name__) |
|
|
|
|
|
|
|
|
@ -241,7 +239,7 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
assert c.get('/fe2').data.strip() == b'/fe' |
|
|
|
|
assert c.get('/be').data.strip() == b'/fe' |
|
|
|
|
|
|
|
|
|
def test_dotted_names_from_app(self): |
|
|
|
|
def test_dotted_names_from_app(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
app.testing = True |
|
|
|
|
test = flask.Blueprint('test', __name__) |
|
|
|
@ -260,7 +258,7 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
rv = c.get('/') |
|
|
|
|
assert rv.data == b'/test/' |
|
|
|
|
|
|
|
|
|
def test_empty_url_defaults(self): |
|
|
|
|
def test_empty_url_defaults(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
|
|
|
|
|
@bp.route('/', defaults={'page': 1}) |
|
|
|
@ -275,7 +273,7 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
assert c.get('/').data == b'1' |
|
|
|
|
assert c.get('/page/2').data == b'2' |
|
|
|
|
|
|
|
|
|
def test_route_decorator_custom_endpoint(self): |
|
|
|
|
def test_route_decorator_custom_endpoint(): |
|
|
|
|
|
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
|
|
|
|
@ -309,7 +307,7 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
assert c.get('/py/bar/123').data == b'bp.123' |
|
|
|
|
assert c.get('/py/bar/foo').data == b'bp.bar_foo' |
|
|
|
|
|
|
|
|
|
def test_route_decorator_custom_endpoint_with_dots(self): |
|
|
|
|
def test_route_decorator_custom_endpoint_with_dots(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
|
|
|
|
|
@bp.route('/foo') |
|
|
|
@ -361,7 +359,7 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
rv = c.get('/py/bar/123') |
|
|
|
|
assert rv.status_code == 404 |
|
|
|
|
|
|
|
|
|
def test_template_filter(self): |
|
|
|
|
def test_template_filter(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
@bp.app_template_filter() |
|
|
|
|
def my_reverse(s): |
|
|
|
@ -372,7 +370,7 @@ class TestBlueprint(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(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
def my_reverse(s): |
|
|
|
|
return s[::-1] |
|
|
|
@ -383,7 +381,7 @@ class TestBlueprint(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(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
@bp.app_template_filter('strrev') |
|
|
|
|
def my_reverse(s): |
|
|
|
@ -394,7 +392,7 @@ class TestBlueprint(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(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
def my_reverse(s): |
|
|
|
|
return s[::-1] |
|
|
|
@ -405,7 +403,7 @@ class TestBlueprint(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(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
@bp.app_template_filter() |
|
|
|
|
def super_reverse(s): |
|
|
|
@ -418,7 +416,7 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
assert rv.data == b'dcba' |
|
|
|
|
|
|
|
|
|
def test_template_filter_after_route_with_template(self): |
|
|
|
|
def test_template_filter_after_route_with_template(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
@app.route('/') |
|
|
|
|
def index(): |
|
|
|
@ -431,7 +429,7 @@ class TestBlueprint(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(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
def super_reverse(s): |
|
|
|
|
return s[::-1] |
|
|
|
@ -444,7 +442,7 @@ class TestBlueprint(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(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
@bp.app_template_filter('super_reverse') |
|
|
|
|
def my_reverse(s): |
|
|
|
@ -457,7 +455,7 @@ class TestBlueprint(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(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
def my_reverse(s): |
|
|
|
|
return s[::-1] |
|
|
|
@ -470,7 +468,7 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
assert rv.data == b'dcba' |
|
|
|
|
|
|
|
|
|
def test_template_test(self): |
|
|
|
|
def test_template_test(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
@bp.app_template_test() |
|
|
|
|
def is_boolean(value): |
|
|
|
@ -481,7 +479,7 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
assert app.jinja_env.tests['is_boolean'] == is_boolean |
|
|
|
|
assert app.jinja_env.tests['is_boolean'](False) |
|
|
|
|
|
|
|
|
|
def test_add_template_test(self): |
|
|
|
|
def test_add_template_test(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
def is_boolean(value): |
|
|
|
|
return isinstance(value, bool) |
|
|
|
@ -492,7 +490,7 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
assert app.jinja_env.tests['is_boolean'] == is_boolean |
|
|
|
|
assert app.jinja_env.tests['is_boolean'](False) |
|
|
|
|
|
|
|
|
|
def test_template_test_with_name(self): |
|
|
|
|
def test_template_test_with_name(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
@bp.app_template_test('boolean') |
|
|
|
|
def is_boolean(value): |
|
|
|
@ -503,7 +501,7 @@ class TestBlueprint(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(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
def is_boolean(value): |
|
|
|
|
return isinstance(value, bool) |
|
|
|
@ -514,7 +512,7 @@ class TestBlueprint(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(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
@bp.app_template_test() |
|
|
|
|
def boolean(value): |
|
|
|
@ -527,7 +525,7 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
assert b'Success!' in rv.data |
|
|
|
|
|
|
|
|
|
def test_template_test_after_route_with_template(self): |
|
|
|
|
def test_template_test_after_route_with_template(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
@app.route('/') |
|
|
|
|
def index(): |
|
|
|
@ -540,7 +538,7 @@ class TestBlueprint(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(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
def boolean(value): |
|
|
|
|
return isinstance(value, bool) |
|
|
|
@ -553,7 +551,7 @@ class TestBlueprint(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(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
@bp.app_template_test('boolean') |
|
|
|
|
def is_boolean(value): |
|
|
|
@ -566,7 +564,7 @@ class TestBlueprint(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(): |
|
|
|
|
bp = flask.Blueprint('bp', __name__) |
|
|
|
|
def is_boolean(value): |
|
|
|
|
return isinstance(value, bool) |
|
|
|
@ -578,8 +576,3 @@ class TestBlueprint(TestFlask):
|
|
|
|
|
return flask.render_template('template_test.html', value=False) |
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
assert b'Success!' in rv.data |
|
|
|
|
|
|
|
|
|
def suite(): |
|
|
|
|
suite = unittest.TestSuite() |
|
|
|
|
suite.addTest(unittest.makeSuite(TestBlueprint)) |
|
|
|
|
return suite |
|
|
|
|