-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
85 lines (62 loc) · 3.02 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import unittest
from router import Router, RouterException
def just_a_callable():
pass
def another_callable():
pass
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]))