Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added urlpatterns_reverse unit tests, which test the reverse lookup o…

…f URLs

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3471 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3770dbde3ccfbe9150d8c18cacc1e353cc9a46b1 1 parent 36ee8f5
@adrianholovaty adrianholovaty authored
Showing with 47 additions and 0 deletions.
  1. +47 −0 tests/othertests/urlpatterns_reverse.py
View
47 tests/othertests/urlpatterns_reverse.py
@@ -0,0 +1,47 @@
+"Unit tests for reverse URL lookup"
+
+from django.core.urlresolvers import reverse_helper, NoReverseMatch
+import re
+
+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'}),
+ ('^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'}),
+)
+
+def run_tests(verbosity=0):
+ for regex, expected, args, kwargs in test_data:
+ passed = True
+ try:
+ got = reverse_helper(re.compile(regex), *args, **kwargs)
+ except NoReverseMatch, e:
+ if expected != NoReverseMatch:
+ passed, got = False, str(e)
+ else:
+ if got != expected:
+ passed, got = False, got
+ if passed and verbosity:
+ print "Passed: %s" % regex
+ elif not passed:
+ print "REVERSE LOOKUP FAILED: %s" % regex
+ print " Got: %s" % got
+ print " Expected: %r" % expected
+
+if __name__ == "__main__":
+ run_tests(1)

0 comments on commit 3770dbd

Please sign in to comment.
Something went wrong with that request. Please try again.