當前位置: 首頁>>代碼示例>>Python>>正文


Python Router.url_for方法代碼示例

本文整理匯總了Python中router.Router.url_for方法的典型用法代碼示例。如果您正苦於以下問題:Python Router.url_for方法的具體用法?Python Router.url_for怎麽用?Python Router.url_for使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在router.Router的用法示例。


在下文中一共展示了Router.url_for方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: RouterTest

# 需要導入模塊: from router import Router [as 別名]
# 或者: from router.Router import url_for [as 別名]
class RouterTest(unittest.TestCase):

    def setUp(self):
        self.router = Router()

    def tearDown(self):
        self.router = None

    def test_register_with_not_callable(self):
        self.assertRaises(
            RouterException, self.router.register, '/', '10', ['GET'])

    def test_register_with_callable_and_legal_path(self):
        self.router.register('/', just_a_callable, ['GET'])
        self.assertIn(just_a_callable, self.router.methods['GET'])

    def test_call_magic_method(self):
        self.router.register('/', just_a_callable, ['GET'])
        r = self.router('/')
        self.assertEqual(r, (just_a_callable, None))

    def test_register_with_callable_and_illegal_path(self):
        self.assertRaises(
            RouterException, self.router.register, '$$', just_a_callable, ['GET'])

    def test_get_with_illegal_methods(self):
        self.router.register('/', just_a_callable, ['GET'])
        self.assertRaises(RouterException, self.router.get, '/', 'REMOVE')

    def test_get_with_legal_methods_one_slash(self):
        self.router.register('/', just_a_callable, ['GET'])
        self.assertEqual((just_a_callable, None), self.router.get('/'))

    def test_get_with_legal_methods_two_slash(self):
        self.router.register('/post/about', just_a_callable, ['GET'])
        self.assertEqual(
            (just_a_callable, None), self.router.get('/post/about'))

    def test_get_with_int_args(self):
        self.router.register('/show/<int:id>', just_a_callable, ['GET'])
        self.assertEqual(
            (just_a_callable, {'id': '1'}), self.router.get('/show/1'))

    def test_url_for_with_not_callable(self):
        self.assertRaises(RouterException, self.router.url_for, '10')

    def test_url_for_with_no_args_rule(self):
        self.router.register('/post', just_a_callable, ['GET'])
        self.assertEqual('/post', self.router.url_for(just_a_callable))

    def test_url_for_with_args_rule_but_no_args_provided(self):
        self.router.register('/post/<int:id>', just_a_callable, ['GET'])
        self.assertRaises(
            RouterException, self.router.url_for, just_a_callable)

    def test_url_for_with_args_rule_and_args_provided(self):
        self.router.register('/post/<int:id>', just_a_callable, ['GET'])
        self.assertEqual(
            '/post/1', self.router.url_for(just_a_callable, id='1'))

    def test_url_for_with_no_matched_rule(self):
        self.router.register('/post', just_a_callable, ['GET'])
        self.assertRaises(
            RouterException, self.router.url_for, another_callable)

    def test_all_callable(self):
        self.router.register('/', just_a_callable, ['GET'])
        r = self.router.all_callables()
        self.assertEqual(set(r), set([just_a_callable]))
        self.router.register('/post', another_callable, ['GET'])
        r = self.router.all_callables()
        self.assertEqual(set(r), set([another_callable, just_a_callable]))
開發者ID:hziling,項目名稱:router,代碼行數:74,代碼來源:test.py


注:本文中的router.Router.url_for方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。