@ -18,7 +18,7 @@ from werkzeug.http import parse_cache_control_header
from jinja2 import TemplateNotFound
from jinja2 import TemplateNotFound
def test_blueprint_specific_error_handling ( ) :
def test_blueprint_specific_error_handling ( app , client ) :
frontend = flask . Blueprint ( ' frontend ' , __name__ )
frontend = flask . Blueprint ( ' frontend ' , __name__ )
backend = flask . Blueprint ( ' backend ' , __name__ )
backend = flask . Blueprint ( ' backend ' , __name__ )
sideend = flask . Blueprint ( ' sideend ' , __name__ )
sideend = flask . Blueprint ( ' sideend ' , __name__ )
@ -43,7 +43,6 @@ def test_blueprint_specific_error_handling():
def sideend_no ( ) :
def sideend_no ( ) :
flask . abort ( 403 )
flask . abort ( 403 )
app = flask . Flask ( __name__ )
app . register_blueprint ( frontend )
app . register_blueprint ( frontend )
app . register_blueprint ( backend )
app . register_blueprint ( backend )
app . register_blueprint ( sideend )
app . register_blueprint ( sideend )
@ -52,15 +51,15 @@ def test_blueprint_specific_error_handling():
def app_forbidden ( e ) :
def app_forbidden ( e ) :
return ' application itself says no ' , 403
return ' application itself says no ' , 403
c = app . test_client ( )
assert client . get ( ' /frontend-no ' ) . data == b ' frontend says no '
assert client . get ( ' /backend-no ' ) . data == b ' backend says no '
assert client . get ( ' /what-is-a-sideend ' ) . data == b ' application itself says no '
assert c . get ( ' /frontend-no ' ) . data == b ' frontend says no '
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 ( ) :
def test_blueprint_specific_user_error_handling ( app , client ) :
class MyDecoratorException ( Exception ) :
class MyDecoratorException ( Exception ) :
pass
pass
class MyFunctionException ( Exception ) :
class MyFunctionException ( Exception ) :
pass
pass
@ -74,32 +73,30 @@ def test_blueprint_specific_user_error_handling():
def my_function_exception_handler ( e ) :
def my_function_exception_handler ( e ) :
assert isinstance ( e , MyFunctionException )
assert isinstance ( e , MyFunctionException )
return ' bam '
return ' bam '
blue . register_error_handler ( MyFunctionException , my_function_exception_handler )
blue . register_error_handler ( MyFunctionException , my_function_exception_handler )
@blue . route ( ' /decorator ' )
@blue . route ( ' /decorator ' )
def blue_deco_test ( ) :
def blue_deco_test ( ) :
raise MyDecoratorException ( )
raise MyDecoratorException ( )
@blue . route ( ' /function ' )
@blue . route ( ' /function ' )
def blue_func_test ( ) :
def blue_func_test ( ) :
raise MyFunctionException ( )
raise MyFunctionException ( )
app = flask . Flask ( __name__ )
app . register_blueprint ( blue )
app . register_blueprint ( blue )
c = app . test_client ( )
assert client . get ( ' /decorator ' ) . data == b ' boom '
assert client . get ( ' /function ' ) . data == b ' bam '
assert c . get ( ' /decorator ' ) . data == b ' boom '
assert c . get ( ' /function ' ) . data == b ' bam '
def test_blueprint_app_error_handling ( ) :
def test_blueprint_app_error_handling ( app , client ) :
errors = flask . Blueprint ( ' errors ' , __name__ )
errors = flask . Blueprint ( ' errors ' , __name__ )
@errors . app_errorhandler ( 403 )
@errors . app_errorhandler ( 403 )
def forbidden_handler ( e ) :
def forbidden_handler ( e ) :
return ' you shall not pass ' , 403
return ' you shall not pass ' , 403
app = flask . Flask ( __name__ )
@app . route ( ' /forbidden ' )
@app . route ( ' /forbidden ' )
def app_forbidden ( ) :
def app_forbidden ( ) :
flask . abort ( 403 )
flask . abort ( 403 )
@ -113,12 +110,11 @@ def test_blueprint_app_error_handling():
app . register_blueprint ( errors )
app . register_blueprint ( errors )
app . register_blueprint ( forbidden_bp )
app . register_blueprint ( forbidden_bp )
c = app . test_client ( )
assert client . get ( ' /forbidden ' ) . data == b ' you shall not pass '
assert client . get ( ' /nope ' ) . data == b ' you shall not pass '
assert c . get ( ' /forbidden ' ) . data == b ' you shall not pass '
assert c . get ( ' /nope ' ) . data == b ' you shall not pass '
def test_blueprint_url_definitions ( ) :
def test_blueprint_url_definitions ( app , client ) :
bp = flask . Blueprint ( ' test ' , __name__ )
bp = flask . Blueprint ( ' test ' , __name__ )
@bp . route ( ' /foo ' , defaults = { ' baz ' : 42 } )
@bp . route ( ' /foo ' , defaults = { ' baz ' : 42 } )
@ -129,17 +125,16 @@ def test_blueprint_url_definitions():
def bar ( bar ) :
def bar ( bar ) :
return text_type ( bar )
return text_type ( bar )
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /1 ' , url_defaults = { ' bar ' : 23 } )
app . register_blueprint ( bp , url_prefix = ' /1 ' , url_defaults = { ' bar ' : 23 } )
app . register_blueprint ( bp , url_prefix = ' /2 ' , url_defaults = { ' bar ' : 19 } )
app . register_blueprint ( bp , url_prefix = ' /2 ' , url_defaults = { ' bar ' : 19 } )
c = app . test_client ( )
assert client . get ( ' /1/foo ' ) . data == b ' 23/42 '
assert c . get ( ' /1/foo ' ) . data == b ' 23/42 '
assert client . get ( ' /2/foo ' ) . data == b ' 19/42 '
assert c . get ( ' /2/foo ' ) . data == b ' 19/42 '
assert client . get ( ' /1/bar ' ) . data == b ' 23 '
assert c . get ( ' /1/bar ' ) . data == b ' 23 '
assert client . get ( ' /2/bar ' ) . data == b ' 19 '
assert c . get ( ' /2/bar ' ) . data == b ' 19 '
def test_blueprint_url_processors ( ) :
def test_blueprint_url_processors ( app , client ) :
bp = flask . Blueprint ( ' frontend ' , __name__ , url_prefix = ' /<lang_code> ' )
bp = flask . Blueprint ( ' frontend ' , __name__ , url_prefix = ' /<lang_code> ' )
@bp . url_defaults
@bp . url_defaults
@ -158,28 +153,26 @@ def test_blueprint_url_processors():
def about ( ) :
def about ( ) :
return flask . url_for ( ' .index ' )
return flask . url_for ( ' .index ' )
app = flask . Flask ( __name__ )
app . register_blueprint ( bp )
app . register_blueprint ( bp )
c = app . test_client ( )
assert client . get ( ' /de/ ' ) . data == b ' /de/about '
assert client . get ( ' /de/about ' ) . data == b ' /de/ '
assert c . get ( ' /de/ ' ) . data == b ' /de/about '
assert c . get ( ' /de/about ' ) . data == b ' /de/ '
def test_templates_and_static ( test_apps ) :
def test_templates_and_static ( test_apps ) :
from blueprintapp import app
from blueprintapp import app
c = app . test_client ( )
client = app . test_client ( )
rv = c . get ( ' / ' )
rv = client . get ( ' / ' )
assert rv . data == b ' Hello from the Frontend '
assert rv . data == b ' Hello from the Frontend '
rv = c . get ( ' /admin/ ' )
rv = client . get ( ' /admin/ ' )
assert rv . data == b ' Hello from the Admin '
assert rv . data == b ' Hello from the Admin '
rv = c . get ( ' /admin/index2 ' )
rv = client . get ( ' /admin/index2 ' )
assert rv . data == b ' Hello from the Admin '
assert rv . data == b ' Hello from the Admin '
rv = c . get ( ' /admin/static/test.txt ' )
rv = client . get ( ' /admin/static/test.txt ' )
assert rv . data . strip ( ) == b ' Admin File '
assert rv . data . strip ( ) == b ' Admin File '
rv . close ( )
rv . close ( )
rv = c . get ( ' /admin/static/css/test.css ' )
rv = client . get ( ' /admin/static/css/test.css ' )
assert rv . data . strip ( ) == b ' /* nested file */ '
assert rv . data . strip ( ) == b ' /* nested file */ '
rv . close ( )
rv . close ( )
@ -190,7 +183,7 @@ def test_templates_and_static(test_apps):
if app . config [ ' SEND_FILE_MAX_AGE_DEFAULT ' ] == expected_max_age :
if app . config [ ' SEND_FILE_MAX_AGE_DEFAULT ' ] == expected_max_age :
expected_max_age = 7200
expected_max_age = 7200
app . config [ ' SEND_FILE_MAX_AGE_DEFAULT ' ] = expected_max_age
app . config [ ' SEND_FILE_MAX_AGE_DEFAULT ' ] = expected_max_age
rv = c . get ( ' /admin/static/css/test.css ' )
rv = client . get ( ' /admin/static/css/test.css ' )
cc = parse_cache_control_header ( rv . headers [ ' Cache-Control ' ] )
cc = parse_cache_control_header ( rv . headers [ ' Cache-Control ' ] )
assert cc . max_age == expected_max_age
assert cc . max_age == expected_max_age
rv . close ( )
rv . close ( )
@ -208,8 +201,10 @@ def test_templates_and_static(test_apps):
with flask . Flask ( __name__ ) . test_request_context ( ) :
with flask . Flask ( __name__ ) . test_request_context ( ) :
assert flask . render_template ( ' nested/nested.txt ' ) == ' I \' m nested '
assert flask . render_template ( ' nested/nested.txt ' ) == ' I \' m nested '
def test_default_static_cache_timeout ( ) :
def test_default_static_cache_timeout ( ) :
app = flask . Flask ( __name__ )
app = flask . Flask ( __name__ )
class MyBlueprint ( flask . Blueprint ) :
class MyBlueprint ( flask . Blueprint ) :
def get_send_file_max_age ( self , filename ) :
def get_send_file_max_age ( self , filename ) :
return 100
return 100
@ -232,12 +227,14 @@ def test_default_static_cache_timeout():
finally :
finally :
app . config [ ' SEND_FILE_MAX_AGE_DEFAULT ' ] = max_age_default
app . config [ ' SEND_FILE_MAX_AGE_DEFAULT ' ] = max_age_default
def test_templates_list ( test_apps ) :
def test_templates_list ( test_apps ) :
from blueprintapp import app
from blueprintapp import app
templates = sorted ( app . jinja_env . list_templates ( ) )
templates = sorted ( app . jinja_env . list_templates ( ) )
assert templates == [ ' admin/index.html ' , ' frontend/index.html ' ]
assert templates == [ ' admin/index.html ' , ' frontend/index.html ' ]
def test_dotted_names ( ) :
def test_dotted_names ( app , client ) :
frontend = flask . Blueprint ( ' myapp.frontend ' , __name__ )
frontend = flask . Blueprint ( ' myapp.frontend ' , __name__ )
backend = flask . Blueprint ( ' myapp.backend ' , __name__ )
backend = flask . Blueprint ( ' myapp.backend ' , __name__ )
@ -253,18 +250,15 @@ def test_dotted_names():
def backend_index ( ) :
def backend_index ( ) :
return flask . url_for ( ' myapp.frontend.frontend_index ' )
return flask . url_for ( ' myapp.frontend.frontend_index ' )
app = flask . Flask ( __name__ )
app . register_blueprint ( frontend )
app . register_blueprint ( frontend )
app . register_blueprint ( backend )
app . register_blueprint ( backend )
c = app . test_client ( )
assert client . get ( ' /fe ' ) . data . strip ( ) == b ' /be '
assert c . get ( ' /fe ' ) . data . strip ( ) == b ' /be '
assert client . get ( ' /fe2 ' ) . data . strip ( ) == b ' /fe '
assert c . get ( ' /fe2 ' ) . data . strip ( ) == b ' /fe '
assert client . get ( ' /be ' ) . data . strip ( ) == b ' /fe '
assert c . get ( ' /be ' ) . data . strip ( ) == b ' /fe '
def test_dotted_names_from_app ( ) :
app = flask . Flask ( __name__ )
def test_dotted_names_from_app ( app , client ) :
app . testing = True
test = flask . Blueprint ( ' test ' , __name__ )
test = flask . Blueprint ( ' test ' , __name__ )
@app . route ( ' / ' )
@app . route ( ' / ' )
@ -277,11 +271,11 @@ def test_dotted_names_from_app():
app . register_blueprint ( test )
app . register_blueprint ( test )
with app . test_client ( ) as c :
rv = client . get ( ' / ' )
rv = c . get ( ' / ' )
assert rv . data == b ' /test/ '
assert rv . data == b ' /test/ '
def test_empty_url_defaults ( ) :
def test_empty_url_defaults ( app , client ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
@bp . route ( ' / ' , defaults = { ' page ' : 1 } )
@bp . route ( ' / ' , defaults = { ' page ' : 1 } )
@ -289,15 +283,13 @@ def test_empty_url_defaults():
def something ( page ) :
def something ( page ) :
return str ( page )
return str ( page )
app = flask . Flask ( __name__ )
app . register_blueprint ( bp )
app . register_blueprint ( bp )
c = app . test_client ( )
assert client . get ( ' / ' ) . data == b ' 1 '
assert c . get ( ' / ' ) . data == b ' 1 '
assert client . get ( ' /page/2 ' ) . data == b ' 2 '
assert c . get ( ' /page/2 ' ) . data == b ' 2 '
def test_route_decorator_custom_endpoint ( ) :
def test_route_decorator_custom_endpoint ( app , client ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
@bp . route ( ' /foo ' )
@bp . route ( ' /foo ' )
@ -316,21 +308,20 @@ def test_route_decorator_custom_endpoint():
def bar_foo ( ) :
def bar_foo ( ) :
return flask . request . endpoint
return flask . request . endpoint
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
@app . route ( ' / ' )
@app . route ( ' / ' )
def index ( ) :
def index ( ) :
return flask . request . endpoint
return flask . request . endpoint
c = app . test_client ( )
assert client . get ( ' / ' ) . data == b ' index '
assert c . get ( ' / ' ) . data == b ' index '
assert client . get ( ' /py/foo ' ) . data == b ' bp.foo '
assert c . get ( ' /py/foo ' ) . data == b ' bp.foo '
assert client . get ( ' /py/bar ' ) . data == b ' bp.bar '
assert c . get ( ' /py/bar ' ) . data == b ' bp.bar '
assert client . get ( ' /py/bar/123 ' ) . data == b ' bp.123 '
assert c . get ( ' /py/bar/123 ' ) . data == b ' bp.123 '
assert client . get ( ' /py/bar/foo ' ) . data == b ' bp.bar_foo '
assert c . get ( ' /py/bar/foo ' ) . data == b ' bp.bar_foo '
def test_route_decorator_custom_endpoint_with_dots ( ) :
def test_route_decorator_custom_endpoint_with_dots ( app , client ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
@bp . route ( ' /foo ' )
@bp . route ( ' /foo ' )
@ -371,21 +362,18 @@ def test_route_decorator_custom_endpoint_with_dots():
lambda : None
lambda : None
)
)
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
c = app . test_client ( )
assert client . get ( ' /py/foo ' ) . data == b ' bp.foo '
assert c . get ( ' /py/foo ' ) . data == b ' bp.foo '
# The rule's didn't actually made it through
# The rule's didn't actually made it through
rv = c . get ( ' /py/bar ' )
rv = client . get ( ' /py/bar ' )
assert rv . status_code == 404
assert rv . status_code == 404
rv = c . get ( ' /py/bar/123 ' )
rv = client . get ( ' /py/bar/123 ' )
assert rv . status_code == 404
assert rv . status_code == 404
def test_endpoint_decorator ( ) :
def test_endpoint_decorator ( app , client ) :
from werkzeug . routing import Rule
from werkzeug . routing import Rule
app = flask . Flask ( __name__ )
app . url_map . add ( Rule ( ' /foo ' , endpoint = ' bar ' ) )
app . url_map . add ( Rule ( ' /foo ' , endpoint = ' bar ' ) )
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
@ -396,229 +384,282 @@ def test_endpoint_decorator():
app . register_blueprint ( bp , url_prefix = ' /bp_prefix ' )
app . register_blueprint ( bp , url_prefix = ' /bp_prefix ' )
c = app . test_client ( )
assert client . get ( ' /foo ' ) . data == b ' bar '
assert c . get ( ' /foo ' ) . data == b ' bar '
assert client . get ( ' /bp_prefix/bar ' ) . status_code == 404
assert c . get ( ' /bp_prefix/bar ' ) . status_code == 404
def test_template_filter ( ) :
def test_template_filter ( app ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
@bp . app_template_filter ( )
@bp . app_template_filter ( )
def my_reverse ( s ) :
def my_reverse ( s ) :
return s [ : : - 1 ]
return s [ : : - 1 ]
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
assert ' my_reverse ' in app . jinja_env . filters . keys ( )
assert ' my_reverse ' in app . jinja_env . filters . keys ( )
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 ( ) :
def test_add_template_filter ( app ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
def my_reverse ( s ) :
def my_reverse ( s ) :
return s [ : : - 1 ]
return s [ : : - 1 ]
bp . add_app_template_filter ( my_reverse )
bp . add_app_template_filter ( my_reverse )
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
assert ' my_reverse ' in app . jinja_env . filters . keys ( )
assert ' my_reverse ' in app . jinja_env . filters . keys ( )
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 ( ) :
def test_template_filter_with_name ( app ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
@bp . app_template_filter ( ' strrev ' )
@bp . app_template_filter ( ' strrev ' )
def my_reverse ( s ) :
def my_reverse ( s ) :
return s [ : : - 1 ]
return s [ : : - 1 ]
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
assert ' strrev ' in app . jinja_env . filters . keys ( )
assert ' strrev ' in app . jinja_env . filters . keys ( )
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 ( ) :
def test_add_template_filter_with_name ( app ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
def my_reverse ( s ) :
def my_reverse ( s ) :
return s [ : : - 1 ]
return s [ : : - 1 ]
bp . add_app_template_filter ( my_reverse , ' strrev ' )
bp . add_app_template_filter ( my_reverse , ' strrev ' )
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
assert ' strrev ' in app . jinja_env . filters . keys ( )
assert ' strrev ' in app . jinja_env . filters . keys ( )
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 ( ) :
def test_template_filter_with_template ( app , client ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
@bp . app_template_filter ( )
@bp . app_template_filter ( )
def super_reverse ( s ) :
def super_reverse ( s ) :
return s [ : : - 1 ]
return s [ : : - 1 ]
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
@app . route ( ' / ' )
@app . route ( ' / ' )
def index ( ) :
def index ( ) :
return flask . render_template ( ' template_filter.html ' , value = ' abcd ' )
return flask . render_template ( ' template_filter.html ' , value = ' abcd ' )
rv = app . test_client ( ) . get ( ' / ' )
rv = client . get ( ' / ' )
assert rv . data == b ' dcba '
assert rv . data == b ' dcba '
def test_template_filter_after_route_with_template ( ) :
app = flask . Flask ( __name__ )
def test_template_filter_after_route_with_template ( app , client ) :
@app . route ( ' / ' )
@app . route ( ' / ' )
def index ( ) :
def index ( ) :
return flask . render_template ( ' template_filter.html ' , value = ' abcd ' )
return flask . render_template ( ' template_filter.html ' , value = ' abcd ' )
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
@bp . app_template_filter ( )
@bp . app_template_filter ( )
def super_reverse ( s ) :
def super_reverse ( s ) :
return s [ : : - 1 ]
return s [ : : - 1 ]
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
rv = app . test_ client( ) . get ( ' / ' )
rv = client . get ( ' / ' )
assert rv . data == b ' dcba '
assert rv . data == b ' dcba '
def test_add_template_filter_with_template ( ) :
def test_add_template_filter_with_template ( app , client ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
def super_reverse ( s ) :
def super_reverse ( s ) :
return s [ : : - 1 ]
return s [ : : - 1 ]
bp . add_app_template_filter ( super_reverse )
bp . add_app_template_filter ( super_reverse )
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
@app . route ( ' / ' )
@app . route ( ' / ' )
def index ( ) :
def index ( ) :
return flask . render_template ( ' template_filter.html ' , value = ' abcd ' )
return flask . render_template ( ' template_filter.html ' , value = ' abcd ' )
rv = app . test_client ( ) . get ( ' / ' )
rv = client . get ( ' / ' )
assert rv . data == b ' dcba '
assert rv . data == b ' dcba '
def test_template_filter_with_name_and_template ( ) :
def test_template_filter_with_name_and_template ( app , client ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
@bp . app_template_filter ( ' super_reverse ' )
@bp . app_template_filter ( ' super_reverse ' )
def my_reverse ( s ) :
def my_reverse ( s ) :
return s [ : : - 1 ]
return s [ : : - 1 ]
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
@app . route ( ' / ' )
@app . route ( ' / ' )
def index ( ) :
def index ( ) :
return flask . render_template ( ' template_filter.html ' , value = ' abcd ' )
return flask . render_template ( ' template_filter.html ' , value = ' abcd ' )
rv = app . test_client ( ) . get ( ' / ' )
rv = client . get ( ' / ' )
assert rv . data == b ' dcba '
assert rv . data == b ' dcba '
def test_add_template_filter_with_name_and_template ( ) :
def test_add_template_filter_with_name_and_template ( app , client ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
def my_reverse ( s ) :
def my_reverse ( s ) :
return s [ : : - 1 ]
return s [ : : - 1 ]
bp . add_app_template_filter ( my_reverse , ' super_reverse ' )
bp . add_app_template_filter ( my_reverse , ' super_reverse ' )
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
@app . route ( ' / ' )
@app . route ( ' / ' )
def index ( ) :
def index ( ) :
return flask . render_template ( ' template_filter.html ' , value = ' abcd ' )
return flask . render_template ( ' template_filter.html ' , value = ' abcd ' )
rv = app . test_client ( ) . get ( ' / ' )
rv = client . get ( ' / ' )
assert rv . data == b ' dcba '
assert rv . data == b ' dcba '
def test_template_test ( ) :
def test_template_test ( app ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
@bp . app_template_test ( )
@bp . app_template_test ( )
def is_boolean ( value ) :
def is_boolean ( value ) :
return isinstance ( value , bool )
return isinstance ( value , bool )
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
assert ' is_boolean ' in app . jinja_env . tests . keys ( )
assert ' is_boolean ' in app . jinja_env . tests . keys ( )
assert app . jinja_env . tests [ ' is_boolean ' ] == is_boolean
assert app . jinja_env . tests [ ' is_boolean ' ] == is_boolean
assert app . jinja_env . tests [ ' is_boolean ' ] ( False )
assert app . jinja_env . tests [ ' is_boolean ' ] ( False )
def test_add_template_test ( ) :
def test_add_template_test ( app ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
def is_boolean ( value ) :
def is_boolean ( value ) :
return isinstance ( value , bool )
return isinstance ( value , bool )
bp . add_app_template_test ( is_boolean )
bp . add_app_template_test ( is_boolean )
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
assert ' is_boolean ' in app . jinja_env . tests . keys ( )
assert ' is_boolean ' in app . jinja_env . tests . keys ( )
assert app . jinja_env . tests [ ' is_boolean ' ] == is_boolean
assert app . jinja_env . tests [ ' is_boolean ' ] == is_boolean
assert app . jinja_env . tests [ ' is_boolean ' ] ( False )
assert app . jinja_env . tests [ ' is_boolean ' ] ( False )
def test_template_test_with_name ( ) :
def test_template_test_with_name ( app ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
@bp . app_template_test ( ' boolean ' )
@bp . app_template_test ( ' boolean ' )
def is_boolean ( value ) :
def is_boolean ( value ) :
return isinstance ( value , bool )
return isinstance ( value , bool )
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
assert ' boolean ' in app . jinja_env . tests . keys ( )
assert ' boolean ' in app . jinja_env . tests . keys ( )
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 ( ) :
def test_add_template_test_with_name ( app ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
def is_boolean ( value ) :
def is_boolean ( value ) :
return isinstance ( value , bool )
return isinstance ( value , bool )
bp . add_app_template_test ( is_boolean , ' boolean ' )
bp . add_app_template_test ( is_boolean , ' boolean ' )
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
assert ' boolean ' in app . jinja_env . tests . keys ( )
assert ' boolean ' in app . jinja_env . tests . keys ( )
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 ( ) :
def test_template_test_with_template ( app , client ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
@bp . app_template_test ( )
@bp . app_template_test ( )
def boolean ( value ) :
def boolean ( value ) :
return isinstance ( value , bool )
return isinstance ( value , bool )
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
@app . route ( ' / ' )
@app . route ( ' / ' )
def index ( ) :
def index ( ) :
return flask . render_template ( ' template_test.html ' , value = False )
return flask . render_template ( ' template_test.html ' , value = False )
rv = app . test_client ( ) . get ( ' / ' )
rv = client . get ( ' / ' )
assert b ' Success! ' in rv . data
assert b ' Success! ' in rv . data
def test_template_test_after_route_with_template ( ) :
app = flask . Flask ( __name__ )
def test_template_test_after_route_with_template ( app , client ) :
@app . route ( ' / ' )
@app . route ( ' / ' )
def index ( ) :
def index ( ) :
return flask . render_template ( ' template_test.html ' , value = False )
return flask . render_template ( ' template_test.html ' , value = False )
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
@bp . app_template_test ( )
@bp . app_template_test ( )
def boolean ( value ) :
def boolean ( value ) :
return isinstance ( value , bool )
return isinstance ( value , bool )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
rv = app . test_ client( ) . get ( ' / ' )
rv = client . get ( ' / ' )
assert b ' Success! ' in rv . data
assert b ' Success! ' in rv . data
def test_add_template_test_with_template ( ) :
def test_add_template_test_with_template ( app , client ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
def boolean ( value ) :
def boolean ( value ) :
return isinstance ( value , bool )
return isinstance ( value , bool )
bp . add_app_template_test ( boolean )
bp . add_app_template_test ( boolean )
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
@app . route ( ' / ' )
@app . route ( ' / ' )
def index ( ) :
def index ( ) :
return flask . render_template ( ' template_test.html ' , value = False )
return flask . render_template ( ' template_test.html ' , value = False )
rv = app . test_client ( ) . get ( ' / ' )
rv = client . get ( ' / ' )
assert b ' Success! ' in rv . data
assert b ' Success! ' in rv . data
def test_template_test_with_name_and_template ( ) :
def test_template_test_with_name_and_template ( app , client ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
@bp . app_template_test ( ' boolean ' )
@bp . app_template_test ( ' boolean ' )
def is_boolean ( value ) :
def is_boolean ( value ) :
return isinstance ( value , bool )
return isinstance ( value , bool )
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
@app . route ( ' / ' )
@app . route ( ' / ' )
def index ( ) :
def index ( ) :
return flask . render_template ( ' template_test.html ' , value = False )
return flask . render_template ( ' template_test.html ' , value = False )
rv = app . test_client ( ) . get ( ' / ' )
rv = client . get ( ' / ' )
assert b ' Success! ' in rv . data
assert b ' Success! ' in rv . data
def test_add_template_test_with_name_and_template ( ) :
def test_add_template_test_with_name_and_template ( app , client ) :
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
def is_boolean ( value ) :
def is_boolean ( value ) :
return isinstance ( value , bool )
return isinstance ( value , bool )
bp . add_app_template_test ( is_boolean , ' boolean ' )
bp . add_app_template_test ( is_boolean , ' boolean ' )
app = flask . Flask ( __name__ )
app . register_blueprint ( bp , url_prefix = ' /py ' )
app . register_blueprint ( bp , url_prefix = ' /py ' )
@app . route ( ' / ' )
@app . route ( ' / ' )
def index ( ) :
def index ( ) :
return flask . render_template ( ' template_test.html ' , value = False )
return flask . render_template ( ' template_test.html ' , value = False )
rv = app . test_client ( ) . get ( ' / ' )
rv = client . get ( ' / ' )
assert b ' Success! ' in rv . data
assert b ' Success! ' in rv . data
def test_context_processing ( ) :
def test_context_processing ( ) :
app = flask . Flask ( __name__ )
app = flask . Flask ( __name__ )
answer_bp = flask . Blueprint ( ' answer_bp ' , __name__ )
answer_bp = flask . Blueprint ( ' answer_bp ' , __name__ )
@ -661,12 +702,15 @@ def test_context_processing():
assert b ' 42 ' in answer_page_bytes
assert b ' 42 ' in answer_page_bytes
assert b ' 43 ' in answer_page_bytes
assert b ' 43 ' in answer_page_bytes
def test_template_global ( ) :
def test_template_global ( ) :
app = flask . Flask ( __name__ )
app = flask . Flask ( __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
bp = flask . Blueprint ( ' bp ' , __name__ )
@bp . app_template_global ( )
@bp . app_template_global ( )
def get_answer ( ) :
def get_answer ( ) :
return 42
return 42
# Make sure the function is not in the jinja_env already
# Make sure the function is not in the jinja_env already
assert ' get_answer ' not in app . jinja_env . globals . keys ( )
assert ' get_answer ' not in app . jinja_env . globals . keys ( )
app . register_blueprint ( bp )
app . register_blueprint ( bp )