"Unit tests for reverse URL lookup" 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/(?P\d+)/$', 'places/3/', [], {'id': 3}), ('^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, [], {}), ('^hardcoded/$', 'hardcoded/', [], {}), ('^hardcoded/$', 'hardcoded/', ['any arg'], {}), ('^hardcoded/$', 'hardcoded/', [], {'kwarg': 'foo'}), ('^people/(?P\w\w)/(?P\w+)/$', 'people/il/adrian/', [], {'state': 'il', 'name': 'adrian'}), ('^people/(?P\w\w)/(?P\d)/$', NoReverseMatch, [], {'state': 'il', 'name': 'adrian'}), ('^people/(?P\w\w)/(?P\w+)/$', NoReverseMatch, [], {'state': 'il'}), ('^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'}), ) 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) if __name__ == "__main__": run_tests(1)