<s> import unittest <EOL> from flask import Flask , Blueprint , request <EOL> try : <EOL> from mock import Mock <EOL> except : <EOL> from unittest . mock import Mock <EOL> import flask <EOL> import flask_restful <EOL> import flask_restful . fields <EOL> from nose . tools import assert_true , assert_false <EOL> class HelloWorld ( flask_restful . Resource ) : <EOL> def get ( self ) : <EOL> return { } <EOL> class GoodbyeWorld ( flask_restful . Resource ) : <EOL> def __init__ ( self , err ) : <EOL> self . err = err <EOL> def get ( self ) : <EOL> flask . abort ( self . err ) <EOL> class APIWithBlueprintTestCase ( unittest . TestCase ) : <EOL> def test_api_base ( self ) : <EOL> blueprint = Blueprint ( '<STR_LIT:test>' , __name__ ) <EOL> api = flask_restful . Api ( blueprint ) <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( blueprint ) <EOL> self . assertEquals ( api . urls , { } ) <EOL> self . assertEquals ( api . prefix , '<STR_LIT>' ) <EOL> self . assertEquals ( api . default_mediatype , '<STR_LIT:application/json>' ) <EOL> def test_api_delayed_initialization ( self ) : <EOL> blueprint = Blueprint ( '<STR_LIT:test>' , __name__ ) <EOL> api = flask_restful . Api ( ) <EOL> api . init_app ( blueprint ) <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( blueprint ) <EOL> api . add_resource ( HelloWorld , '<STR_LIT:/>' , endpoint = "<STR_LIT:hello>" ) <EOL> def test_add_resource_endpoint ( self ) : <EOL> blueprint = Blueprint ( '<STR_LIT:test>' , __name__ ) <EOL> api = flask_restful . Api ( blueprint ) <EOL> view = Mock ( ** { '<STR_LIT>' : Mock ( __name__ = '<STR_LIT>' ) } ) <EOL> api . add_resource ( view , '<STR_LIT>' , endpoint = '<STR_LIT:bar>' ) <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( blueprint ) <EOL> view . as_view . assert_called_with ( '<STR_LIT:bar>' ) <EOL> def test_add_resource_endpoint_after_registration ( self ) : <EOL> blueprint = Blueprint ( '<STR_LIT:test>' , __name__ ) <EOL> api = flask_restful . Api ( blueprint ) <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( blueprint ) <EOL> view = Mock ( ** { '<STR_LIT>' : Mock ( __name__ = '<STR_LIT>' ) } ) <EOL> api . add_resource ( view , '<STR_LIT>' , endpoint = '<STR_LIT:bar>' ) <EOL> view . as_view . assert_called_with ( '<STR_LIT:bar>' ) <EOL> def test_url_with_api_prefix ( self ) : <EOL> blueprint = Blueprint ( '<STR_LIT:test>' , __name__ ) <EOL> api = flask_restful . Api ( blueprint , prefix = '<STR_LIT>' ) <EOL> api . add_resource ( HelloWorld , '<STR_LIT>' , endpoint = '<STR_LIT:hello>' ) <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( blueprint ) <EOL> with app . test_request_context ( '<STR_LIT>' ) : <EOL> self . assertEquals ( request . endpoint , '<STR_LIT>' ) <EOL> def test_url_with_blueprint_prefix ( self ) : <EOL> blueprint = Blueprint ( '<STR_LIT:test>' , __name__ , url_prefix = '<STR_LIT>' ) <EOL> api = flask_restful . Api ( blueprint ) <EOL> api . add_resource ( HelloWorld , '<STR_LIT>' , endpoint = '<STR_LIT:hello>' ) <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( blueprint ) <EOL> with app . test_request_context ( '<STR_LIT>' ) : <EOL> self . assertEquals ( request . endpoint , '<STR_LIT>' ) <EOL> def test_url_with_registration_prefix ( self ) : <EOL> blueprint = Blueprint ( '<STR_LIT:test>' , __name__ ) <EOL> api = flask_restful . Api ( blueprint ) <EOL> api . add_resource ( HelloWorld , '<STR_LIT>' , endpoint = '<STR_LIT:hello>' ) <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( blueprint , url_prefix = '<STR_LIT>' ) <EOL> with app . test_request_context ( '<STR_LIT>' ) : <EOL> self . assertEquals ( request . endpoint , '<STR_LIT>' ) <EOL> def test_registration_prefix_overrides_blueprint_prefix ( self ) : <EOL> blueprint = Blueprint ( '<STR_LIT:test>' , __name__ , url_prefix = '<STR_LIT>' ) <EOL> api = flask_restful . Api ( blueprint ) <EOL> api . add_resource ( HelloWorld , '<STR_LIT>' , endpoint = '<STR_LIT:hello>' ) <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( blueprint , url_prefix = '<STR_LIT>' ) <EOL> with app . test_request_context ( '<STR_LIT>' ) : <EOL> self . assertEquals ( request . endpoint , '<STR_LIT>' ) <EOL> def test_url_with_api_and_blueprint_prefix ( self ) : <EOL> blueprint = Blueprint ( '<STR_LIT:test>' , __name__ , url_prefix = '<STR_LIT>' ) <EOL> api = flask_restful . Api ( blueprint , prefix = '<STR_LIT>' ) <EOL> api . add_resource ( HelloWorld , '<STR_LIT>' , endpoint = '<STR_LIT:hello>' ) <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( blueprint ) <EOL> with app . test_request_context ( '<STR_LIT>' ) : <EOL> self . assertEquals ( request . endpoint , '<STR_LIT>' ) <EOL> def test_url_part_order_aeb ( self ) : <EOL> blueprint = Blueprint ( '<STR_LIT:test>' , __name__ , url_prefix = '<STR_LIT>' ) <EOL> api = flask_restful . Api ( blueprint , prefix = '<STR_LIT>' , url_part_order = '<STR_LIT>' ) <EOL> api . add_resource ( HelloWorld , '<STR_LIT>' , endpoint = '<STR_LIT:hello>' ) <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( blueprint ) <EOL> with app . test_request_context ( '<STR_LIT>' ) : <EOL> self . assertEquals ( request . endpoint , '<STR_LIT>' ) <EOL> def test_error_routing ( self ) : <EOL> blueprint = Blueprint ( '<STR_LIT:test>' , __name__ ) <EOL> api = flask_restful . Api ( blueprint ) <EOL> api . add_resource ( HelloWorld ( ) , '<STR_LIT>' , endpoint = "<STR_LIT:hello>" ) <EOL> api . add_resource ( GoodbyeWorld ( <NUM_LIT> ) , '<STR_LIT>' , endpoint = "<STR_LIT>" ) <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( blueprint ) <EOL> with app . test_request_context ( '<STR_LIT>' , method = '<STR_LIT:POST>' ) : <EOL> assert_true ( api . _should_use_fr_error_handler ( ) ) <EOL> assert_true ( api . _has_fr_route ( ) ) <EOL> with app . test_request_context ( '<STR_LIT>' ) : <EOL> api . _should_use_fr_error_handler = Mock ( return_value = False ) <EOL> assert_true ( api . _has_fr_route ( ) ) <EOL> def test_non_blueprint_rest_error_routing ( self ) : <EOL> blueprint = Blueprint ( '<STR_LIT:test>' , __name__ ) <EOL> api = flask_restful . Api ( blueprint ) <EOL> api . add_resource ( HelloWorld ( ) , '<STR_LIT>' , endpoint = "<STR_LIT:hello>" ) <EOL> api . add_resource ( GoodbyeWorld ( <NUM_LIT> ) , '<STR_LIT>' , endpoint = "<STR_LIT>" ) <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( blueprint , url_prefix = '<STR_LIT>' ) <EOL> api2 = flask_restful . Api ( app ) <EOL> api2 . add_resource ( HelloWorld ( ) , '<STR_LIT>' , endpoint = "<STR_LIT:hello>" ) <EOL> api2 . add_resource ( GoodbyeWorld ( <NUM_LIT> ) , '<STR_LIT>' , endpoint = "<STR_LIT>" ) <EOL> with app . test_request_context ( '<STR_LIT>' , method = '<STR_LIT:POST>' ) : <EOL> assert_false ( api . _should_use_fr_error_handler ( ) ) <EOL> assert_true ( api2 . _should_use_fr_error_handler ( ) ) <EOL> assert_false ( api . _has_fr_route ( ) ) <EOL> assert_true ( api2 . _has_fr_route ( ) ) <EOL> with app . test_request_context ( '<STR_LIT>' , method = '<STR_LIT:POST>' ) : <EOL> assert_true ( api . _should_use_fr_error_handler ( ) ) <EOL> assert_false ( api2 . _should_use_fr_error_handler ( ) ) <EOL> assert_true ( api . _has_fr_route ( ) ) <EOL> assert_false ( api2 . _has_fr_route ( ) ) <EOL> api . _should_use_fr_error_handler = Mock ( return_value = False ) <EOL> api2 . _should_use_fr_error_handler = Mock ( return_value = False ) <EOL> with app . test_request_context ( '<STR_LIT>' ) : <EOL> assert_false ( api . _has_fr_route ( ) ) <EOL> assert_true ( api2 . _has_fr_route ( ) ) <EOL> with app . test_request_context ( '<STR_LIT>' ) : <EOL> assert_true ( api . _has_fr_route ( ) ) <EOL> assert_false ( api2 . _has_fr_route ( ) ) <EOL> def test_non_blueprint_non_rest_error_routing ( self ) : <EOL> blueprint = Blueprint ( '<STR_LIT:test>' , __name__ ) <EOL> api = flask_restful . Api ( blueprint ) <EOL> api . add_resource ( HelloWorld ( ) , '<STR_LIT>' , endpoint = "<STR_LIT:hello>" ) <EOL> api . add_resource ( GoodbyeWorld ( <NUM_LIT> ) , '<STR_LIT>' , endpoint = "<STR_LIT>" ) <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( blueprint , url_prefix = '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def hi ( ) : <EOL> return '<STR_LIT>' <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def bye ( ) : <EOL> flask . abort ( <NUM_LIT> ) <EOL> with app . test_request_context ( '<STR_LIT>' , method = '<STR_LIT:POST>' ) : <EOL> assert_false ( api . _should_use_fr_error_handler ( ) ) <EOL> assert_false ( api . _has_fr_route ( ) ) <EOL> with app . test_request_context ( '<STR_LIT>' , method = '<STR_LIT:POST>' ) : <EOL> assert_true ( api . _should_use_fr_error_handler ( ) ) <EOL> assert_true ( api . _has_fr_route ( ) ) <EOL> api . _should_use_fr_error_handler = Mock ( return_value = False ) <EOL> with app . test_request_context ( '<STR_LIT>' ) : <EOL> assert_false ( api . _has_fr_route ( ) ) <EOL> with app . test_request_context ( '<STR_LIT>' ) : <EOL> assert_true ( api . _has_fr_route ( ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
