-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.py
207 lines (186 loc) · 6.15 KB
/
tests.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from django.utils import unittest
from django.conf.urls import patterns
import hurl
class BasicPatternsTest(unittest.TestCase):
def test_simple_string(self):
h = hurl.Hurl()
result = h.urls({
'2003': '2003_view',
})
expected = (
(r'^2003/$', '2003_view', None, '2003_view'),
)
self.assertSequenceEqual(result, expected)
def test_with_callable(self):
def some_view(req):
return ''
h = hurl.Hurl()
result = h.urls({
'2003': some_view,
})
expected = (
(r'^2003/$', some_view, None, 'some_view'),
)
self.assertSequenceEqual(result, expected)
def test_simple_named_parameter(self):
h = hurl.Hurl()
result = h.urls({
'<id:int>': 'news.views.details',
})
expected = (
(r'^(?P<id>\d+)/$', 'news.views.details', None, 'details'),
)
self.assertSequenceEqual(result, expected)
def test_two_named_parameters(self):
h = hurl.Hurl()
result = h.urls({
'articles': {
'<id:int>/<id2:int>': 'news.views.details',
}
})
expected = (
(r'^articles/(?P<id>\d+)/(?P<id2>\d+)/$',
'news.views.details', None, 'details'),
)
self.assertSequenceEqual(result, expected)
def test_tree_urls(self):
self.maxDiff = None
h = hurl.Hurl()
result = h.urls({
'articles': [
('<id:int>/<id2:int>', 'news.views.details'),
('text/author', [
('<author_id:int>', 'news.views.author_details'),
('archive/<author_id:int>', 'news.views.archive'),
])
]
})
expected = (
(r'^articles/(?P<id>\d+)/(?P<id2>\d+)/$',
'news.views.details', None, 'details'),
(r'^articles/text/author/(?P<author_id>\d+)/$',
'news.views.author_details', None, 'author_details'),
(r'^articles/text/author/archive/(?P<author_id>\d+)/$',
'news.views.archive', None, 'archive'),
)
self.assertSequenceEqual(result, expected)
def test_slug_named_type(self):
h = hurl.Hurl()
result = h.urls({
'<id:slug>': 'news.views.details',
})
expected = (
(r'^(?P<id>[\w-]+)/$', 'news.views.details', None, 'details'),
)
self.assertSequenceEqual(result, expected)
def test_custom_named_type(self):
h = hurl.Hurl()
h.matchers['year'] = r'\d{4}'
result = h.urls({
'<year:year>': 'news.views.details',
})
expected = (
(r'^(?P<year>\d{4})/$', 'news.views.details', None, 'details'),
)
self.assertSequenceEqual(result, expected)
def test_custom_guessed_named_type(self):
h = hurl.Hurl()
h.matchers['year'] = r'\d{4}'
result = h.urls({
'<year>': 'news.views.details',
})
expected = (
(r'^(?P<year>\d{4})/$', 'news.views.details', None, 'details'),
)
self.assertSequenceEqual(result, expected)
def test_default_type_is_slug(self):
h = hurl.Hurl()
result = h.urls({
'<year>': 'news.views.details',
})
expected = (
(r'^(?P<year>[\w-]+)/$', 'news.views.details', None, 'details'),
)
self.assertSequenceEqual(result, expected)
def test_setting_custom_default_type(self):
h = hurl.Hurl()
h.default_matcher = 'int'
result = h.urls({
'<year>': 'news.views.details',
})
expected = (
(r'^(?P<year>\d+)/$', 'news.views.details', None, 'details'),
)
self.assertSequenceEqual(result, expected)
def test_empty_url(self):
h = hurl.Hurl()
result = h.urls({
'': 'details',
})
expected = (
(r'^$', 'details', None, 'details'),
)
self.assertSequenceEqual(result, expected)
def test_empty_nested_url(self):
h = hurl.Hurl()
result = h.urls({
'bla': {
'': 'details',
}
})
expected = (
(r'^bla/$', 'details', None, 'details'),
)
self.assertSequenceEqual(result, expected)
def test_no_name_only_type(self):
h = hurl.Hurl()
result = h.urls({
'<:int>': 'details',
})
expected = (
(r'^(\d+)/$', 'details', None, 'details'),
)
self.assertSequenceEqual(result, expected)
def test_name_prefix(self):
h = hurl.Hurl(name_prefix='news')
result = h.urls({
'<id:int>': 'news.views.details',
})
expected = (
(r'^(?P<id>\d+)/$', 'news.views.details', None, 'news_details'),
)
self.assertSequenceEqual(result, expected)
def test_include(self):
h = hurl.Hurl()
urlpatterns = h.patterns('', {
'<id:int>': 'news.views.details',
})
result = h.urls({
'<id:int>': {
'': 'news.views.details',
'comments': h.include(urlpatterns)
}
})
expected = (
(r'^(?P<id>\d+)/$', 'news.views.details', None, 'details'),
(r'^(?P<id>\d+)/comments/$', (urlpatterns, None, None), None, None),
)
self.assertEqual(result[0], expected[0])
self.assertEqual(result[1][0], expected[1][0])
self.assertEqual(result[1][2], expected[1][2])
self.assertEqual(result[1][3], expected[1][3])
self.assertEqual(
result[1][1][0][0].__dict__,
expected[1][1][0][0].__dict__)
def test_regexurlpatter_returned(self):
h = hurl.Hurl()
urlpatterns = h.patterns('', {
'<id:int>': 'news.views.details'
})
expected = patterns('',
(r'^(?P<id>[0-9]+)/', 'news.views.details', None,
'news_details')
)
self.assertTrue(urlpatterns, expected)
if __name__ == '__main__':
unittest.main()