Django

Code

Show
Ignore:
Timestamp:
08/31/08 06:11:20 (4 months ago)
Author:
mtredinnick
Message:

A rewrite of the reverse URL parsing: the reverse() call and the "url" template tag.

This is fully backwards compatible, but it fixes a bunch of little bugs. Thanks
to SmileyChris? and Ilya Semenov for some early patches in this area that were
incorporated into this change.

Fixed #2977, #4915, #6934, #7206.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/tests/regressiontests/urlpatterns_reverse/tests.py

    r7851 r8760  
    1 "Unit tests for reverse URL lookup" 
     1""" 
     2Unit tests for reverse URL lookups. 
     3""" 
    24 
    3 from django.core.urlresolvers import reverse_helper, NoReverseMatch 
    4 import re, unittest 
     5from django.core.urlresolvers import reverse, NoReverseMatch 
     6from django.test import TestCase 
    57 
    68test_data = ( 
    7     ('^places/(\d+)/$', 'places/3/', [3], {}), 
    8     ('^places/(\d+)/$', 'places/3/', ['3'], {}), 
    9     ('^places/(\d+)/$', NoReverseMatch, ['a'], {}), 
    10     ('^places/(\d+)/$', NoReverseMatch, [], {}), 
    11     ('^places/(?P<id>\d+)/$', 'places/3/', [], {'id': 3}), 
    12     ('^people/(?P<name>\w+)/$', 'people/adrian/', ['adrian'], {}), 
    13     ('^people/(?P<name>\w+)/$', 'people/adrian/', [], {'name': 'adrian'}), 
    14     ('^people/(?P<name>\w+)/$', NoReverseMatch, ['name with spaces'], {}), 
    15     ('^people/(?P<name>\w+)/$', NoReverseMatch, [], {'name': 'name with spaces'}), 
    16     ('^people/(?P<name>\w+)/$', NoReverseMatch, [], {}), 
    17     ('^hardcoded/$', 'hardcoded/', [], {}), 
    18     ('^hardcoded/$', 'hardcoded/', ['any arg'], {}), 
    19     ('^hardcoded/$', 'hardcoded/', [], {'kwarg': 'foo'}), 
    20     ('^hardcoded/doc\\.pdf$', 'hardcoded/doc.pdf', [], {}), 
    21     ('^people/(?P<state>\w\w)/(?P<name>\w+)/$', 'people/il/adrian/', [], {'state': 'il', 'name': 'adrian'}), 
    22     ('^people/(?P<state>\w\w)/(?P<name>\d)/$', NoReverseMatch, [], {'state': 'il', 'name': 'adrian'}), 
    23     ('^people/(?P<state>\w\w)/(?P<name>\w+)/$', NoReverseMatch, [], {'state': 'il'}), 
    24     ('^people/(?P<state>\w\w)/(?P<name>\w+)/$', NoReverseMatch, [], {'name': 'adrian'}), 
    25     ('^people/(?P<state>\w\w)/(\w+)/$', NoReverseMatch, ['il'], {'name': 'adrian'}), 
    26     ('^people/(?P<state>\w\w)/(\w+)/$', 'people/il/adrian/', ['adrian'], {'state': 'il'}), 
     9    ('places', '/places/3/', [3], {}), 
     10    ('places', '/places/3/', ['3'], {}), 
     11    ('places', NoReverseMatch, ['a'], {}), 
     12    ('places', NoReverseMatch, [], {}), 
     13    ('places?', '/place/', [], {}), 
     14    ('places+', '/places/', [], {}), 
     15    ('places*', '/place/', [], {}), 
     16    ('places2?', '/', [], {}), 
     17    ('places2+', '/places/', [], {}), 
     18    ('places2*', '/', [], {}), 
     19    ('places3', '/places/4/', [4], {}), 
     20    ('places3', '/places/harlem/', ['harlem'], {}), 
     21    ('places3', NoReverseMatch, ['harlem64'], {}), 
     22    ('places4', '/places/3/', [], {'id': 3}), 
     23    ('people', NoReverseMatch, [], {}), 
     24    ('people', '/people/adrian/', ['adrian'], {}), 
     25    ('people', '/people/adrian/', [], {'name': 'adrian'}), 
     26    ('people', NoReverseMatch, ['name with spaces'], {}), 
     27    ('people', NoReverseMatch, [], {'name': 'name with spaces'}), 
     28    ('people2', '/people/name/', [], {}), 
     29    ('people2a', '/people/name/fred/', ['fred'], {}), 
     30    ('optional', '/optional/fred/', [], {'name': 'fred'}), 
     31    ('optional', '/optional/fred/', ['fred'], {}), 
     32    ('hardcoded', '/hardcoded/', [], {}), 
     33    ('hardcoded2', '/hardcoded/doc.pdf', [], {}), 
     34    ('people3', '/people/il/adrian/', [], {'state': 'il', 'name': 'adrian'}), 
     35    ('people3', NoReverseMatch, [], {'state': 'il'}), 
     36    ('people3', NoReverseMatch, [], {'name': 'adrian'}), 
     37    ('people4', NoReverseMatch, [], {'state': 'il', 'name': 'adrian'}), 
     38    ('people6', '/people/il/test/adrian/', ['il/test', 'adrian'], {}), 
     39    ('people6', '/people//adrian/', ['adrian'], {}), 
     40    ('range', '/character_set/a/', [], {}), 
     41    ('range2', '/character_set/x/', [], {}), 
     42    ('price', '/price/$10/', ['10'], {}), 
     43    ('price2', '/price/$10/', ['10'], {}), 
     44    ('price3', '/price/$10/', ['10'], {}), 
     45    ('product', '/product/chocolate+($2.00)/', [], {'price': '2.00', 'product': 'chocolate'}), 
     46    ('headlines', '/headlines/2007.5.21/', [], dict(year=2007, month=5, day=21)), 
     47    ('windows', r'/windows_path/C:%5CDocuments%20and%20Settings%5Cspam/', [], dict(drive_name='C', path=r'Documents and Settings\spam')), 
     48    ('special', r'/special_chars/+%5C$*/', [r'+\$*'], {}), 
     49    ('special', NoReverseMatch, [''], {}), 
     50    ('mixed', '/john/0/', [], {'name': 'john'}), 
     51    ('repeats', '/repeats/a/', [], {}), 
     52    ('repeats2', '/repeats/aa/', [], {}), 
     53    ('insensitive', '/CaseInsensitive/fred', ['fred'], {}), 
     54    ('test', '/test/1', [], {}), 
     55    ('test2', '/test/2', [], {}), 
     56    ('inner-nothing', '/outer/42/', [], {'outer': '42'}), 
     57    ('inner-nothing', '/outer/42/', ['42'], {}), 
     58    ('inner-nothing', NoReverseMatch, ['foo'], {}), 
     59    ('inner-extra', '/outer/42/extra/inner/', [], {'extra': 'inner', 'outer': '42'}), 
     60    ('inner-extra', '/outer/42/extra/inner/', ['42', 'inner'], {}), 
     61    ('inner-extra', NoReverseMatch, ['fred', 'inner'], {}), 
     62    ('disjunction', NoReverseMatch, ['foo'], {}), 
     63    ('inner-disjunction', NoReverseMatch, ['10', '11'], {}), 
    2764) 
    2865 
    29 class URLPatternReverse(unittest.TestCase): 
     66class URLPatternReverse(TestCase): 
     67    urls = 'regressiontests.urlpatterns_reverse.urls' 
     68 
    3069    def test_urlpattern_reverse(self): 
    31         for regex, expected, args, kwargs in test_data: 
     70        for name, expected, args, kwargs in test_data: 
    3271            try: 
    33                 got = reverse_helper(re.compile(regex), *args, **kwargs) 
     72                got = reverse(name, args=args, kwargs=kwargs) 
    3473            except NoReverseMatch, e: 
    3574                self.assertEqual(expected, NoReverseMatch) 
     
    3776                self.assertEquals(got, expected) 
    3877 
    39 if __name__ == "__main__": 
    40     run_tests(1)