2006-07-28 10:08:36 +08:00
|
|
|
"Unit tests for reverse URL lookup"
|
|
|
|
|
|
|
|
from django.core.urlresolvers import reverse_helper, NoReverseMatch
|
2006-08-27 21:59:47 +08:00
|
|
|
import re, unittest
|
2006-07-28 10:08:36 +08:00
|
|
|
|
|
|
|
test_data = (
|
|
|
|
('^places/(\d+)/$', 'places/3/', [3], {}),
|
|
|
|
('^places/(\d+)/$', 'places/3/', ['3'], {}),
|
|
|
|
('^places/(\d+)/$', NoReverseMatch, ['a'], {}),
|
|
|
|
('^places/(\d+)/$', NoReverseMatch, [], {}),
|
|
|
|
('^places/(?P<id>\d+)/$', 'places/3/', [], {'id': 3}),
|
|
|
|
('^people/(?P<name>\w+)/$', 'people/adrian/', ['adrian'], {}),
|
|
|
|
('^people/(?P<name>\w+)/$', 'people/adrian/', [], {'name': 'adrian'}),
|
|
|
|
('^people/(?P<name>\w+)/$', NoReverseMatch, ['name with spaces'], {}),
|
|
|
|
('^people/(?P<name>\w+)/$', NoReverseMatch, [], {'name': 'name with spaces'}),
|
|
|
|
('^people/(?P<name>\w+)/$', NoReverseMatch, [], {}),
|
|
|
|
('^hardcoded/$', 'hardcoded/', [], {}),
|
|
|
|
('^hardcoded/$', 'hardcoded/', ['any arg'], {}),
|
|
|
|
('^hardcoded/$', 'hardcoded/', [], {'kwarg': 'foo'}),
|
2008-06-16 12:16:21 +08:00
|
|
|
('^hardcoded/doc\\.pdf$', 'hardcoded/doc.pdf', [], {}),
|
2006-07-28 10:08:36 +08:00
|
|
|
('^people/(?P<state>\w\w)/(?P<name>\w+)/$', 'people/il/adrian/', [], {'state': 'il', 'name': 'adrian'}),
|
|
|
|
('^people/(?P<state>\w\w)/(?P<name>\d)/$', NoReverseMatch, [], {'state': 'il', 'name': 'adrian'}),
|
|
|
|
('^people/(?P<state>\w\w)/(?P<name>\w+)/$', NoReverseMatch, [], {'state': 'il'}),
|
|
|
|
('^people/(?P<state>\w\w)/(?P<name>\w+)/$', NoReverseMatch, [], {'name': 'adrian'}),
|
|
|
|
('^people/(?P<state>\w\w)/(\w+)/$', NoReverseMatch, ['il'], {'name': 'adrian'}),
|
|
|
|
('^people/(?P<state>\w\w)/(\w+)/$', 'people/il/adrian/', ['adrian'], {'state': 'il'}),
|
|
|
|
)
|
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
class URLPatternReverse(unittest.TestCase):
|
|
|
|
def test_urlpattern_reverse(self):
|
|
|
|
for regex, expected, args, kwargs in test_data:
|
|
|
|
try:
|
|
|
|
got = reverse_helper(re.compile(regex), *args, **kwargs)
|
|
|
|
except NoReverseMatch, e:
|
|
|
|
self.assertEqual(expected, NoReverseMatch)
|
|
|
|
else:
|
|
|
|
self.assertEquals(got, expected)
|
2006-07-28 10:08:36 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
run_tests(1)
|