diff --git a/tests/regressiontests/urlpatterns_reverse/tests.py b/tests/regressiontests/urlpatterns_reverse/tests.py index 610c3f7bc7ec6..de0441ea40076 100644 --- a/tests/regressiontests/urlpatterns_reverse/tests.py +++ b/tests/regressiontests/urlpatterns_reverse/tests.py @@ -1,19 +1,32 @@ -"Unit tests for reverse URL lookup" +""" +Unit tests for reverse URL lookups. +""" + +import re +import unittest from django.core.urlresolvers import reverse_helper, NoReverseMatch -import re, unittest test_data = ( ('^places/(\d+)/$', 'places/3/', [3], {}), ('^places/(\d+)/$', 'places/3/', ['3'], {}), ('^places/(\d+)/$', NoReverseMatch, ['a'], {}), ('^places/(\d+)/$', NoReverseMatch, [], {}), + ('^places?/$', '/', [], {}), + ('^places+/$', 'places/', [], {}), + ('^places*/$', '/', [], {}), + (r'^places/(\d+|[a-z_]+)/', 'places/4/', [4], {}), + (r'^places/(\d+|[a-z_]+)/', 'places/harlem/', ['harlem'], {}), + (r'^places/(\d+|[a-z_]+)/', NoReverseMatch, ['harlem64'], {}), ('^places/(?P\d+)/$', 'places/3/', [], {'id': 3}), + ('^people/(?P\w+)/$', NoReverseMatch, [], {}), ('^people/(?P\w+)/$', 'people/adrian/', ['adrian'], {}), ('^people/(?P\w+)/$', 'people/adrian/', [], {'name': 'adrian'}), ('^people/(?P\w+)/$', NoReverseMatch, ['name with spaces'], {}), ('^people/(?P\w+)/$', NoReverseMatch, [], {'name': 'name with spaces'}), - ('^people/(?P\w+)/$', NoReverseMatch, [], {}), + ('^people/(?:name/)', 'people/name/', [], {}), + ('^people/(?:name/)?', 'people/', [], {}), + ('^people/(?:name/(\w+)/)?', 'people/name/fred/', ['fred'], {}), ('^hardcoded/$', 'hardcoded/', [], {}), ('^hardcoded/$', 'hardcoded/', ['any arg'], {}), ('^hardcoded/$', 'hardcoded/', [], {'kwarg': 'foo'}), @@ -24,17 +37,31 @@ ('^people/(?P\w\w)/(?P\w+)/$', NoReverseMatch, [], {'name': 'adrian'}), ('^people/(?P\w\w)/(\w+)/$', NoReverseMatch, ['il'], {'name': 'adrian'}), ('^people/(?P\w\w)/(\w+)/$', 'people/il/adrian/', ['adrian'], {'state': 'il'}), + (r'^people/((?P\w\w)/test)?/(\w+)/$', 'people/il/test/adrian/', ['adrian'], {'state': 'il'}), + (r'^people/((?P\w\w)/test)?/(\w+)/$', NoReverseMatch, ['adrian'], {}), + ('^character_set/[abcdef0-9]/$', 'character_set/a/', [], {}), + ('^character_set/[\w]/$', 'character_set/a/', [], {}), + (r'^price/\$(\d+)/$', 'price/$10/', ['10'], {}), + (r'^price/[$](\d+)/$', 'price/$10/', ['10'], {}), + (r'^price/[\$](\d+)/$', 'price/$10/', ['10'], {}), + (r'^product/(?P\w+)\+\(\$(?P\d+(\.\d+)?)\)/$', 'product/chocolate+($2.00)/', ['2.00'], {'product': 'chocolate'}), + (r'^headlines/(?P\d+)\.(?P\d+)\.(?P\d+)/$', 'headlines/2007.5.21/', [], dict(year=2007, month=5, day=21)), + (r'^windows_path/(?P[A-Z]):\\(?P.+)/$', r'windows_path/C:\Documents and Settings\spam/', [], dict(drive_name='C', path=r'Documents and Settings\spam')), + (r'^special_chars/(.+)/$', r'special_chars/+\$*/', [r'+\$*'], {}), + (r'^special_chars/(.+)/$', NoReverseMatch, [''], {}), + (r'^(?P.+)/\d+/$', NoReverseMatch, [], {'name': 'john'}), + (r'^repeats/a{1,2}/$', 'repeats/a/', [], {}), + (r'^repeats/a{2,4}/$', 'repeats/aa/', [], {}), + (r'^people/(?:(?:wilma|fred)/)$', '/people/wilma', [], {}), ) 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) + got = reverse_helper(regex, *args, **kwargs) except NoReverseMatch, e: self.assertEqual(expected, NoReverseMatch) else: self.assertEquals(got, expected) -if __name__ == "__main__": - run_tests(1)