def test_nested_cases(self): 'Locations of nested web.cases' chain = web.cases( web.match('/', 'index'), web.cases( web.match('/docs', 'docs'))) self.assert_(web.locations(chain).keys(), ['index', 'docs'])
def test_nested_cases(self): 'Reverse with nested web.cases' chain = web.cases( web.cases(web.match('/', 'index'), web.match('/docs', 'docs'), web.cases(web.match('/news', 'news')))) r = web.Reverse.from_handler(chain) self.assertEqual(r('index'), '/') self.assertEqual(r('docs'), '/docs') self.assertEqual(r('news'), '/news')
def test_nested_cases(self): 'Reverse with nested web.cases' chain = web.cases( web.cases( web.match('/', 'index'), web.match('/docs', 'docs'), web.cases( web.match('/news', 'news')))) r = web.Reverse.from_handler(chain) self.assertEqual(r('index'), '/') self.assertEqual(r('docs'), '/docs') self.assertEqual(r('news'), '/news')
def test_chain_of_lists(self): 'Chain of lists, data check' def h(env, data, nx): data.count = 1 return nx(env, data) def h1(env, data, nx): self.assert_('count' in data) self.assertEqual(data.count, 1) return nx(env, data) chain = web.cases(h) | web.cases(h1, h1) chain(VersionedStorage(), VersionedStorage())
def test_chain_of_lists(self): "Chain of lists, data check" def h(env, data, nx): data.count = 1 return nx(env, data) def h1(env, data, nx): self.assert_("count" in data) self.assertEqual(data.count, 1) return nx(env, data) chain = web.cases(h) | web.cases(h1, h1) chain(VersionedStorage(), VersionedStorage())
def test_nested_cases_with_prefixes(self): 'Reverse with nested web.cases with web.prefixes' chain = web.cases( web.match('/', 'index'), web.prefix('/docs') | web.cases(web.match('/<int:id>', 'doc'), web.match('/list', 'docs')), web.prefix('/news') | web.cases(web.match('/<int:id>', 'news'), web.match('/list', 'newslist'))) r = web.Reverse.from_handler(chain) self.assertEqual(r('index'), '/') self.assertEqual(r('docs'), '/docs/list') self.assertEqual(r('newslist'), '/news/list') self.assertEqual(r('doc', id=1), '/docs/1') self.assertEqual(r('news', id=1), '/news/1')
def test_prefix_and_cases(self): 'Locations of web.cases with prefix' chain = web.prefix('/news') | web.cases(web.match('/', 'index'), web.match('/docs', 'docs')) for k in ('index', 'docs'): self.assert_(web.locations(chain).keys(), [k]) self.assert_('builders' in web.locations(chain)[k]) self.assertEqual(len(web.locations(chain)[k]['builders']), 2)
def test_subdomains_and_cases(self): 'Locations of web.cases with subdomains' chain = web.subdomain('news') | web.cases(web.match('/', 'index'), web.match('/docs', 'docs')) for k in ('index', 'docs'): self.assert_(web.locations(chain).keys(), [k]) self.assert_('subdomains' in web.locations(chain)[k]) self.assertEqual(web.locations(chain)[k]['subdomains'], ['news'])
def test_prefix_leaf(self): '''Simple prefix''' def handler(env, data, nx): self.assertEqual(env.request.prefixed_path, '/item') return Response() app = web.cases( web.match('/', 'index'), web.prefix('/docs') | web.cases( web.match('/', 'docs') | handler, web.match('/item', 'doc') | handler, web.prefix('/tags') | web.cases( web.match('/', 'tags') | handler, web.match('/tag', 'tag') | handler))) self.assertEqual(web.ask(app, '/docs/item').status_int, 200)
def test_nested_cases_with_prefixes(self): 'Reverse with nested web.cases with web.prefixes' chain = web.cases( web.match('/', 'index'), web.prefix('/docs') | web.cases( web.match('/<int:id>', 'doc'), web.match('/list', 'docs')), web.prefix('/news') | web.cases( web.match('/<int:id>', 'news'), web.match('/list', 'newslist'))) r = web.Reverse.from_handler(chain) self.assertEqual(r('index'), '/') self.assertEqual(r('docs'), '/docs/list') self.assertEqual(r('newslist'), '/news/list') self.assertEqual(r('doc', id=1), '/docs/1') self.assertEqual(r('news', id=1), '/news/1')
def test_mix(self): 'Loactions mix' chain = web.prefix('/items') | web.cases( web.match('/', 'index'), web.prefix('/news') | web.namespace('news') | web.cases( web.match('/', 'index'), web.match('/<int:id>', 'item')), web.prefix('/docs') | web.namespace('docs') | web.cases( web.match('/', 'index'), web.match('/<int:id>', 'item'))) locations = web.locations(chain) self.assertEqual( locations.keys(), ['index', 'docs.index', 'docs.item', 'news.item', 'news.index']) self.assertEqual(len(locations['index']['builders']), 2) self.assertEqual(len(locations['news.index']['builders']), 3) self.assertEqual(len(locations['news.item']['builders']), 3) self.assertEqual(len(locations['docs.index']['builders']), 3) self.assertEqual(len(locations['docs.item']['builders']), 3)
def test_unicode(self): '''Routing rules with unicode''' # XXX move to urltemplate and reverse tests? app = web.cases( web.prefix(u'/հայերեն') | web.cases( web.match(u'/%', 'percent') | (lambda e,d,n: Response()) ) ) encoded = '/%D5%B0%D5%A1%D5%B5%D5%A5%D6%80%D5%A5%D5%B6/%25' self.assertEqual(web.Reverse.from_handler(app)('percent'), encoded) self.assertEqual(web.Reverse.from_handler(app)('percent').get_readable(), u'/հայերեն/%') self.assertNotEqual(web.ask(app, encoded), None) # ??? # rctx have prefixes, so we need new one self.assertEqual(web.ask(app, encoded).status_int, 200)
def test_prefix_and_cases(self): 'Locations of web.cases with prefix' chain = web.prefix('/news') | web.cases( web.match('/', 'index'), web.match('/docs', 'docs')) for k in ('index', 'docs'): self.assert_(web.locations(chain).keys(), [k]) self.assert_('builders' in web.locations(chain)[k]) self.assertEqual(len(web.locations(chain)[k]['builders']), 2)
def test_subdomains_and_cases(self): 'Locations of web.cases with subdomains' chain = web.subdomain('news') | web.cases( web.match('/', 'index'), web.match('/docs', 'docs')) for k in ('index', 'docs'): self.assert_(web.locations(chain).keys(), [k]) self.assert_('subdomains' in web.locations(chain)[k]) self.assertEqual(web.locations(chain)[k]['subdomains'], ['news'])
def test_mix(self): 'Loactions mix' chain = web.prefix('/items') | web.cases( web.match('/', 'index'), web.prefix('/news') | web.namespace('news') | web.cases( web.match('/', 'index'), web.match('/<int:id>', 'item')), web.prefix('/docs') | web.namespace('docs') | web.cases( web.match('/', 'index'), web.match('/<int:id>', 'item'))) locations = web.locations(chain) self.assertEqual(locations.keys(), ['index', 'docs.index', 'docs.item', 'news.item', 'news.index']) self.assertEqual(len(locations['index']['builders']), 2) self.assertEqual(len(locations['news.index']['builders']), 3) self.assertEqual(len(locations['news.item']['builders']), 3) self.assertEqual(len(locations['docs.index']['builders']), 3) self.assertEqual(len(locations['docs.item']['builders']), 3)
def test_host10(self): app = web.cases ( web.subdomain(u'bla') | web.match('/', 'bla') | (lambda e,d,n: Response(body='bla') ), web.subdomain(u'localhost') | web.match('/', 'localhost') | (lambda e,d,n: Response(body='localhost')) ) self.assertEqual(web.ask(app, 'http://bla/').body, 'bla') self.assertEqual(web.ask(app, '/').body, 'localhost')
def test_few_handlers(self): 'Reverse a few handlers' chain = web.cases( web.match('/', 'index'), web.match('/docs', 'docs'), web.match('/news', 'news'), ) r = web.Reverse.from_handler(chain) self.assertEqual(r('index'), '/') self.assertEqual(r('docs'), '/docs') self.assertEqual(r('news'), '/news')
def test_prefix_root(self): '''Prefix root''' def handler(env, data, nx): self.assertEqual(env.request.prefixed_path, '/') return Response() app = web.cases( web.match('/', 'index') | handler, web.prefix('/docs') | web.cases( web.match('/', 'docs') | handler, web.match('/item', 'doc') | handler, web.prefix('/tags') | web.cases( web.match('/', 'tags') | handler, web.match('/tag', 'tag') | handler))) self.assertEqual(web.ask(app, '/docs'), None) self.assertEqual(web.ask(app, '/docs/').status_int, 200) self.assertEqual(web.ask(app, '/docs/tags'), None) self.assertEqual(web.ask(app, '/docs/tags/').status_int, 200) self.assertEqual(web.ask(app, '/docs/tags/asdasd'), None)
def test_int_converter(self): '''Check int converter''' def handler(env, data, nx): self.assertEqual(data.id, 42) return Response() app = web.cases( web.match('/first', 'first') | handler, web.match('/second/<int:id>', 'second') | handler) web.ask(app, '/second/42')
def test_not_found(self): '''Check int converter with handler which accepts params''' def handler(env, data, nx): return Response() app = web.cases( web.match('/first', 'first') | handler, web.match('/second/<int:id>', 'second') | handler) self.assert_(web.ask(app, '/second/42/') is None) self.assert_(web.ask(app, '/second/42s') is None)
def test_multiple_int_convs(self): '''Check multiple int converters''' def handler(env, data, nx): self.assertEqual(data.id, 42) self.assertEqual(data.param, 23) return Response() app = web.cases( web.match('/first', 'first') | handler, web.match('/second/<int:id>/<int:param>', 'second') | handler) web.ask(app, '/second/42/23')
def test_subdomain(self): '''Subdomain filter''' def handler(env, data, nx): self.assertEqual(env.request.path, '/') return Response() app = web.subdomain('host') | web.cases( web.subdomain('') | web.match('/', 'index') | handler, web.subdomain('k') | web.cases( web.subdomain('l') | web.cases( web.match('/', 'l') | handler, ), web.subdomain('') | web.match('/', 'k') | handler)) self.assertEqual(web.ask(app, 'http://host/').status_int, 200) self.assertEqual(web.ask(app, 'http://k.host/').status_int, 200) self.assertEqual(web.ask(app, 'http://l.k.host/').status_int, 200) self.assertEqual(web.ask(app, 'http://x.l.k.host/').status_int, 200) self.assert_(web.ask(app, 'http://x.k.host/') is None) self.assert_(web.ask(app, 'http://lk.host/') is None) self.assert_(web.ask(app, 'http://mhost/') is None)
def test_unicode(self): 'Reverse with unicode' # various combinations of url parts containing unicode chain = web.subdomain(u'п') | web.cases( web.prefix(u'/з') | web.match('/', 'unicode1'), web.prefix(u'/з') | web.match('/<string:slug>', 'unicode2'), web.match(u'/д/<string:slug>', 'unicode3'), #regression web.match(u'/<string:slug1>/<string:slug2>', 'unicode4'), #regression ) r = web.Reverse.from_handler(chain) self.assertEqual(r('unicode1'), 'http://xn--o1a/%D0%B7/') self.assertEqual(r('unicode2', slug=u'ю'), 'http://xn--o1a/%D0%B7/%D1%8E') self.assertEqual(r('unicode3', slug=u'ю'), 'http://xn--o1a/%D0%B4/%D1%8E') self.assertEqual(r('unicode4', slug1=u'д', slug2=u'ю'), 'http://xn--o1a/%D0%B4/%D1%8E')
def test_chain_with_list_and_postfix(self): 'Chain with cases and postfix' def h(env, data, nh): self.assertEqual(env.count, 0) env['count'] = env['count'] + 1 return nh(env, data) def h1(env, data, nh): self.assertEqual(env.count, 1) env['count'] = env['count'] + 1 return nh(env, data) def h2(env, data, nh): self.assertEqual(env.count, 2) env['count'] = env['count'] + 1 chain = web.handler(h) | web.cases(h1, web.handler(h1) | h2) | h2 count = VersionedStorage(count=0) self.assert_(chain(count, VersionedStorage()) is None) self.assertEqual(count['count'], 0)
def test_chain_with_list_and_postfix(self): "Chain with cases and postfix" def h(env, data, nh): self.assertEqual(env.count, 0) env["count"] = env["count"] + 1 return nh(env, data) def h1(env, data, nh): self.assertEqual(env.count, 1) env["count"] = env["count"] + 1 return nh(env, data) def h2(env, data, nh): self.assertEqual(env.count, 2) env["count"] = env["count"] + 1 chain = web.handler(h) | web.cases(h1, web.handler(h1) | h2) | h2 count = VersionedStorage(count=0) self.assert_(chain(count, VersionedStorage()) is None) self.assertEqual(count["count"], 0)
def test_List(self): 'cases handle' def h1(env, data, nh): self.assertEqual(env.count, 0) env['count'] = env['count'] + 1 return nh(env, data) def h2(env, data, nh): self.assertEqual(env.count, 0) env['count'] = env['count'] + 1 return nh(env, data) def h3(env, data, nh): self.assertEqual(env.count, 0) env['count'] = env['count'] + 1 return nh(env, data) chain = web.cases(h1, h2, h3) count = VersionedStorage(count=0) self.assert_(chain(count, VersionedStorage()) is None) self.assertEqual(count['count'], 0)
def test_list_of_chains(self): 'cases of chains' def h1(env, data, nh): self.assertEqual(env.count, 0) env['count'] = env['count'] + 1 return nh(env, data) def h2(env, data, nh): self.assertEqual(env.count, 0) env['count'] = env['count'] + 1 return nh(env, data) def h3(env, data, nh): self.assertEqual(env.count, 1) env['count'] = env['count'] + 1 return nh(env, data) chain = web.cases(h1, web.handler(h2) | h3) count = VersionedStorage(count=0) self.assert_(chain(count, VersionedStorage()) is None) self.assertEqual(count['count'], 0)
def test_list_of_chains(self): "cases of chains" def h1(env, data, nh): self.assertEqual(env.count, 0) env["count"] = env["count"] + 1 return nh(env, data) def h2(env, data, nh): self.assertEqual(env.count, 0) env["count"] = env["count"] + 1 return nh(env, data) def h3(env, data, nh): self.assertEqual(env.count, 1) env["count"] = env["count"] + 1 return nh(env, data) chain = web.cases(h1, web.handler(h2) | h3) count = VersionedStorage(count=0) self.assert_(chain(count, VersionedStorage()) is None) self.assertEqual(count["count"], 0)
def test_List(self): "cases handle" def h1(env, data, nh): self.assertEqual(env.count, 0) env["count"] = env["count"] + 1 return nh(env, data) def h2(env, data, nh): self.assertEqual(env.count, 0) env["count"] = env["count"] + 1 return nh(env, data) def h3(env, data, nh): self.assertEqual(env.count, 0) env["count"] = env["count"] + 1 return nh(env, data) chain = web.cases(h1, h2, h3) count = VersionedStorage(count=0) self.assert_(chain(count, VersionedStorage()) is None) self.assertEqual(count["count"], 0)
def test_namespace_and_cases(self): 'Locations namespace with web.cases' chain = web.namespace('news') | web.cases( web.match('/', 'index'), web.match('/docs', 'docs')) self.assertEqual(web.locations(chain).keys(), ['news.index', 'news.docs'])
def test_nested_cases(self): 'Locations of nested web.cases' chain = web.cases(web.match('/', 'index'), web.cases(web.match('/docs', 'docs'))) self.assert_(web.locations(chain).keys(), ['index', 'docs'])
def test_namespace_and_cases(self): 'Locations namespace with web.cases' chain = web.namespace('news') | web.cases(web.match('/', 'index'), web.match('/docs', 'docs')) self.assertEqual( web.locations(chain).keys(), ['news.index', 'news.docs'])
env.url_for_static = static.construct_reverse() env.template = template env.db = db_maker() env.cache = memcache_client try: return next_handler(env, data) finally: env.db.close() app = web.handler(environment) | web.cases( auth.login_handler | h.render_to('login.html'), auth.logout_handler, # API match('/api/posts', 'api-posts') | web.cases( ctype(ctype.xml) | h.posts_paginator | h.to_xml, ctype(ctype.json) | h.posts_paginator | h.to_json, ), auth | web.cases( match('/', 'posts') | h.posts_paginator | h.render_to('posts.html'), match('/<int:id>', 'post') | h.post_by_id | h.render_to('post.html'), prefix('/posts') | auth.login_required | web.cases( match('/add', 'add-post') | h.post_form | h.render_to('add_post.html'), match('/edit/<int:id>', 'edit-post') | h.edit_post | h.render_to('add_post.html'), match('/delete/<int:id>', 'del-post') | h.del_post | h.render_to('del_post.html') ) ), ) url_for = web.Reverse(web.locations(app))
app = web.handler(config) | web.cases( static, auth.login_handler | template.render_to('login'), auth.logout_handler, auth | web.cases( web.match('/', 'dashboard') | views.dashboard, web.prefix('/issue') | web.cases( web.match('/<int:issue>', 'issue') | issue.get | web.cases( web.method('get'), web.method('post') | auth.login_required | issue.update, ) | template.render_to('issue'), ), web.prefix('/proj') | web.cases( web.match('', 'create-project') | auth.login_required | project.create, web.prefix('/<int:proj>') | project.get | web.cases( web.match('', 'project') | template.render_to('proj'), web.match('/update', 'update-project') | project.update, web.match('/issue', 'create-issue') | issue.create, ) ), web.prefix('/user') | web.cases( web.match('', 'create-user') | auth.login_required | user.create, web.match('/<int:user_id>', 'user') | user.get | web.cases( web.method('get'), web.method('post') | auth.login_required | user.update, ) | template.render_to('user'), ) ) )
def test_match_dublication(self): 'Raise error on same url names' self.assertRaises(ValueError, lambda: web.locations( web.cases( web.match('/', 'index'), web.match('/index', 'index'))))
def test_match_dublication(self): 'Raise error on same url names' self.assertRaises( ValueError, lambda: web.locations( web.cases(web.match('/', 'index'), web.match( '/index', 'index'))))