<s> import reg <EOL> import dectate <EOL> import morepath <EOL> from morepath import core <EOL> from morepath import compat <EOL> from . fixtures import identity_policy <EOL> def objects ( actions ) : <EOL> result = [ ] <EOL> for action , obj in actions : <EOL> result . append ( obj ) <EOL> return result <EOL> def builtin_ref ( s ) : <EOL> if compat . PY3 : <EOL> return '<STR_LIT>' % s <EOL> else : <EOL> return '<STR_LIT>' % s <EOL> def test_setting ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ App . setting ( section = '<STR_LIT:foo>' , name = '<STR_LIT:bar>' ) <EOL> def f ( ) : <EOL> return "<STR_LIT>" <EOL> @ App . setting ( section = '<STR_LIT:foo>' , name = '<STR_LIT>' ) <EOL> def g ( ) : <EOL> return "<STR_LIT>" <EOL> dectate . commit ( App ) <EOL> assert objects ( dectate . query_app ( App , '<STR_LIT>' ) ) == [ f , g ] <EOL> assert objects ( dectate . query_app ( App , '<STR_LIT>' , name = '<STR_LIT:bar>' ) ) == [ f ] <EOL> assert objects ( dectate . query_app ( App , '<STR_LIT>' ) ) == [ f , g ] <EOL> def test_predicate_fallback ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' ) ) <EOL> assert r == [ <EOL> core . model_not_found , <EOL> core . name_not_found , <EOL> core . method_not_allowed , <EOL> core . body_model_unprocessable <EOL> ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' , <EOL> dispatch = '<STR_LIT>' ) ) <EOL> assert r == [ <EOL> core . model_not_found , <EOL> core . name_not_found , <EOL> core . method_not_allowed , <EOL> core . body_model_unprocessable <EOL> ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' , <EOL> dispatch = '<STR_LIT>' ) ) <EOL> assert r == [ ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' , <EOL> func = '<STR_LIT>' ) ) <EOL> assert r == [ <EOL> core . model_not_found , <EOL> ] <EOL> def test_predicate ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' ) ) <EOL> assert r == [ <EOL> core . model_predicate , <EOL> core . name_predicate , <EOL> core . request_method_predicate , <EOL> core . body_model_predicate <EOL> ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' , <EOL> dispatch = '<STR_LIT>' ) ) <EOL> assert r == [ <EOL> core . model_predicate , <EOL> core . name_predicate , <EOL> core . request_method_predicate , <EOL> core . body_model_predicate <EOL> ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' , <EOL> dispatch = '<STR_LIT>' ) ) <EOL> assert r == [ ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' , <EOL> name = '<STR_LIT:name>' ) ) <EOL> assert r == [ <EOL> core . name_predicate , <EOL> ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' , <EOL> index = '<STR_LIT>' ) ) <EOL> assert r == [ <EOL> core . model_predicate , <EOL> core . body_model_predicate <EOL> ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' , <EOL> after = '<STR_LIT>' ) ) <EOL> assert r == [ <EOL> core . name_predicate <EOL> ] <EOL> @ reg . dispatch_external_predicates ( ) <EOL> def generic ( v ) : <EOL> pass <EOL> def test_function ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ App . predicate ( generic , name = '<STR_LIT:v>' , default = '<STR_LIT>' , index = reg . KeyIndex ) <EOL> def get ( v ) : <EOL> return v <EOL> @ App . function ( generic , v = '<STR_LIT:A>' ) <EOL> def a ( v ) : <EOL> return v <EOL> @ App . function ( generic , v = '<STR_LIT:B>' ) <EOL> def b ( v ) : <EOL> return v <EOL> dectate . commit ( App ) <EOL> app = App ( ) <EOL> assert generic ( '<STR_LIT:A>' , lookup = app . lookup ) == '<STR_LIT:A>' <EOL> assert generic ( '<STR_LIT:B>' , lookup = app . lookup ) == '<STR_LIT:B>' <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' ) ) <EOL> assert r == [ a , b ] <EOL> r = objects ( <EOL> dectate . query_app ( App , '<STR_LIT>' , <EOL> func = '<STR_LIT>' ) ) <EOL> assert r == [ a , b ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' , v = '<STR_LIT:A>' ) ) <EOL> assert r == [ a ] <EOL> def test_converter ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' ) ) <EOL> expected = [ <EOL> core . int_converter , <EOL> core . unicode_converter <EOL> ] <EOL> if not compat . PY3 : <EOL> expected . append ( core . str_converter ) <EOL> expected . extend ( [ <EOL> core . date_converter , <EOL> core . datetime_converter <EOL> ] ) <EOL> assert r == expected <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' , type = builtin_ref ( '<STR_LIT:int>' ) ) ) <EOL> assert r == [ <EOL> core . int_converter <EOL> ] <EOL> class Foos ( object ) : <EOL> pass <EOL> class Base ( object ) : <EOL> pass <EOL> class Foo ( Base ) : <EOL> pass <EOL> class SubFoo ( Foo ) : <EOL> pass <EOL> class Bar ( Base ) : <EOL> pass <EOL> class SubBar ( Bar ) : <EOL> pass <EOL> def get_variables ( obj ) : <EOL> pass <EOL> def get_converters ( ) : <EOL> return { } <EOL> def test_path ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ App . path ( '<STR_LIT>' , model = Base ) <EOL> def get_base ( ) : <EOL> pass <EOL> @ App . path ( '<STR_LIT>' , model = Foos , variables = get_variables ) <EOL> def get_foos ( ) : <EOL> pass <EOL> @ App . path ( '<STR_LIT>' , model = Foo , get_converters = get_converters , <EOL> absorb = True ) <EOL> def get_foo ( id ) : <EOL> pass <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT:path>' ) ) <EOL> assert r == [ get_base , get_foos , get_foo ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT:path>' , <EOL> model = '<STR_LIT>' ) ) <EOL> assert r == [ get_base , get_foo ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT:path>' , <EOL> model = '<STR_LIT>' ) ) <EOL> assert r == [ get_base , get_foo ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT:path>' , <EOL> model = '<STR_LIT>' ) ) <EOL> assert r == [ get_base ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT:path>' , <EOL> model = '<STR_LIT>' ) ) <EOL> assert r == [ get_base ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT:path>' , path = '<STR_LIT>' ) ) <EOL> assert r == [ get_foos ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT:path>' , path = '<STR_LIT>' ) ) <EOL> assert r == [ get_foos ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT:path>' , path = '<STR_LIT>' ) ) <EOL> assert r == [ get_foo ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT:path>' , path = '<STR_LIT>' ) ) <EOL> assert r == [ get_foo ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT:path>' , <EOL> variables = '<STR_LIT>' ) ) <EOL> assert r == [ get_foos ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT:path>' , <EOL> get_converters = '<STR_LIT>' ) ) <EOL> assert r == [ get_foo ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT:path>' , absorb = '<STR_LIT:True>' ) ) <EOL> assert r == [ get_foo ] <EOL> class BasePermission ( object ) : <EOL> pass <EOL> class Permission ( BasePermission ) : <EOL> pass <EOL> class SubPermission ( Permission ) : <EOL> pass <EOL> class SubIdentity ( morepath . Identity ) : <EOL> pass <EOL> def test_permission_rule ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ App . permission_rule ( model = Base , permission = BasePermission ) <EOL> def rule_base ( obj , permission , identity ) : <EOL> return True <EOL> @ App . permission_rule ( model = Foo , permission = Permission ) <EOL> def rule_foo ( obj , permission , identity ) : <EOL> return True <EOL> @ App . permission_rule ( model = Foos , permission = SubPermission , <EOL> identity = SubIdentity ) <EOL> def rule_foos ( obj , permission , identity ) : <EOL> return True <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' ) ) <EOL> assert r == [ rule_base , rule_foo , rule_foos ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> model = '<STR_LIT>' ) ) <EOL> assert r == [ rule_base , rule_foo ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> model = '<STR_LIT>' ) ) <EOL> assert r == [ rule_base ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> model = '<STR_LIT>' ) ) <EOL> assert r == [ rule_base ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> permission = '<STR_LIT>' ) ) <EOL> assert r == [ rule_foo , rule_foos ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> permission = '<STR_LIT>' ) ) <EOL> assert r == [ rule_base , rule_foo , rule_foos ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> permission = '<STR_LIT>' ) ) <EOL> assert r == [ rule_foos ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> model = '<STR_LIT>' , <EOL> permission = '<STR_LIT>' ) ) <EOL> assert r == [ rule_base , rule_foo ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> identity = '<STR_LIT>' ) ) <EOL> assert r == [ rule_base , rule_foo , rule_foos ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> identity = '<STR_LIT>' ) ) <EOL> assert r == [ rule_foos ] <EOL> def template_directory_a ( ) : <EOL> return '<STR_LIT:/>' <EOL> def test_template_directory ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> App . template_directory ( ) ( template_directory_a ) <EOL> @ App . template_directory ( after = template_directory_a , name = '<STR_LIT>' ) <EOL> def b ( ) : <EOL> return '<STR_LIT:/>' <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' ) ) <EOL> assert r == [ template_directory_a , b ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' , <EOL> name = '<STR_LIT>' ) ) <EOL> assert r == [ b ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> after = '<STR_LIT>' ) ) <EOL> assert r == [ b ] <EOL> def test_template_render ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ App . template_render ( '<STR_LIT>' ) <EOL> def render ( loader , name , original_render ) : <EOL> pass <EOL> @ App . template_render ( '<STR_LIT>' ) <EOL> def render2 ( loader , name , original_render ) : <EOL> pass <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' ) ) <EOL> assert r == [ render , render2 ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' , extension = '<STR_LIT>' ) ) <EOL> assert r == [ render2 ] <EOL> def test_view ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ App . json ( model = Foo ) <EOL> def foo_default ( self , request ) : <EOL> pass <EOL> @ App . view ( model = Base ) <EOL> def base_default ( self , request ) : <EOL> pass <EOL> @ App . view ( model = Foo , name = '<STR_LIT>' ) <EOL> def foo_edit ( self , request ) : <EOL> pass <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' ) ) <EOL> assert r == [ core . standard_exception_view , <EOL> foo_default , base_default , foo_edit ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' ) ) <EOL> assert r == [ core . standard_exception_view , <EOL> foo_default , base_default , foo_edit ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> model = '<STR_LIT>' ) ) <EOL> assert r == [ base_default ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> model = '<STR_LIT>' ) ) <EOL> assert r == [ foo_default , base_default , foo_edit ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> model = '<STR_LIT>' ) ) <EOL> assert r == [ base_default ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> name = '<STR_LIT>' ) ) <EOL> assert r == [ foo_edit ] <EOL> def test_view_permission ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ App . view ( model = Foo , name = '<STR_LIT:n>' ) <EOL> def foo_n ( self , request ) : <EOL> pass <EOL> @ App . view ( model = Foo , name = '<STR_LIT:b>' , permission = BasePermission ) <EOL> def foo_b ( self , request ) : <EOL> pass <EOL> @ App . view ( model = Foo , name = '<STR_LIT:p>' , permission = Permission ) <EOL> def foo_p ( self , request ) : <EOL> pass <EOL> @ App . view ( model = Foo , name = '<STR_LIT:s>' , permission = SubPermission ) <EOL> def foo_s ( self , request ) : <EOL> pass <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> permission = '<STR_LIT>' ) ) <EOL> assert r == [ foo_b , foo_p , foo_s ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> permission = '<STR_LIT>' ) ) <EOL> assert r == [ foo_p , foo_s ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> permission = '<STR_LIT>' ) ) <EOL> assert r == [ foo_s ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' , <EOL> model = '<STR_LIT>' , <EOL> permission = builtin_ref ( '<STR_LIT:None>' ) ) ) <EOL> assert r == [ foo_n ] <EOL> def test_view_body_model ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ App . view ( model = Foo , request_method = '<STR_LIT:POST>' , body_model = Base ) <EOL> def foo_base ( self , request ) : <EOL> pass <EOL> @ App . view ( model = Foo , request_method = '<STR_LIT:POST>' , body_model = Foo ) <EOL> def foo_foo ( self , request ) : <EOL> pass <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> model = '<STR_LIT>' , <EOL> body_model = '<STR_LIT>' ) ) <EOL> assert r == [ foo_base ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> model = '<STR_LIT>' , <EOL> body_model = '<STR_LIT>' ) ) <EOL> assert r == [ foo_base , foo_foo ] <EOL> class Mounted ( morepath . App ) : <EOL> pass <EOL> def test_mount ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ App . path ( path = '<STR_LIT:foo>' , model = Foo ) <EOL> def get_foo ( ) : <EOL> pass <EOL> @ App . mount ( path = '<STR_LIT>' , app = Mounted ) <EOL> def mount_app ( ) : <EOL> return Mounted ( ) <EOL> dectate . commit ( App , Mounted ) <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' ) ) <EOL> assert r == [ get_foo , mount_app ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , app = '<STR_LIT>' ) ) <EOL> assert r == [ mount_app ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , model = '<STR_LIT>' ) ) <EOL> assert r == [ get_foo ] <EOL> def test_defer_links ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> class Mounted2 ( morepath . App ) : <EOL> pass <EOL> @ App . path ( path = '<STR_LIT:foo>' , model = Foo ) <EOL> def get_foo ( ) : <EOL> pass <EOL> @ App . defer_links ( model = Bar ) <EOL> def defer_bar ( app , obj ) : <EOL> return app . child ( '<STR_LIT>' ) <EOL> @ App . mount ( path = '<STR_LIT>' , app = Mounted2 ) <EOL> def mount_app ( ) : <EOL> return Mounted2 ( ) <EOL> dectate . commit ( App , Mounted2 ) <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' ) ) <EOL> assert r == [ get_foo , defer_bar , mount_app ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> model = '<STR_LIT>' ) ) <EOL> assert r == [ defer_bar ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> model = '<STR_LIT>' ) ) <EOL> assert r == [ defer_bar ] <EOL> r = objects ( dectate . query_app ( App , '<STR_LIT>' , <EOL> model = '<STR_LIT>' ) ) <EOL> assert r == [ get_foo ] <EOL> def tween_a_factory ( ) : <EOL> pass <EOL> def tween_b_factory ( ) : <EOL> pass <EOL> def test_tween_factory ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> App . tween_factory ( ) ( tween_a_factory ) <EOL> App . tween_factory ( over = tween_a_factory ) ( tween_b_factory ) <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' ) ) <EOL> assert r == [ <EOL> core . excview_tween_factory , <EOL> core . poisoned_host_header_protection_tween_factory , <EOL> tween_a_factory , <EOL> tween_b_factory <EOL> ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> over = '<STR_LIT>' ) ) <EOL> assert r == [ tween_b_factory ] <EOL> def test_identity_policy ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ App . identity_policy ( ) <EOL> def get_identity_policy ( ) : <EOL> return identity_policy . IdentityPolicy ( ) <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' ) ) <EOL> assert len ( r ) == <NUM_LIT:3> <EOL> def test_verify_identity ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ App . verify_identity ( ) <EOL> def verify ( identity ) : <EOL> return True <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' ) ) <EOL> assert r == [ verify ] <EOL> def test_dump_json ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ App . dump_json ( model = Foo ) <EOL> def dump_foo ( ) : <EOL> pass <EOL> @ App . dump_json ( model = Bar ) <EOL> def dump_bar ( ) : <EOL> pass <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' ) ) <EOL> assert r == [ dump_foo , dump_bar ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> model = '<STR_LIT>' ) ) <EOL> assert r == [ dump_foo ] <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' , <EOL> model = '<STR_LIT>' ) ) <EOL> assert r == [ dump_foo ] <EOL> def test_load_json ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ App . load_json ( ) <EOL> def load ( ) : <EOL> pass <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' ) ) <EOL> assert r == [ load ] <EOL> def test_link_prefix ( ) : <EOL> class App ( morepath . App ) : <EOL> pass <EOL> @ App . link_prefix ( ) <EOL> def prefix ( s ) : <EOL> pass <EOL> dectate . commit ( App ) <EOL> r = objects ( dectate . query_app ( <EOL> App , '<STR_LIT>' ) ) <EOL> assert r == [ prefix ] </s>
