Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Adrian Holovaty authored

Showing 1 changed file with 47 additions and 0 deletions. Show diff stats Hide diff stats

  1. 47  tests/othertests/urlpatterns_reverse.py
47  tests/othertests/urlpatterns_reverse.py
... ...
@@ -0,0 +1,47 @@
  1
+"Unit tests for reverse URL lookup"
  2
+
  3
+from django.core.urlresolvers import reverse_helper, NoReverseMatch
  4
+import re
  5
+
  6
+test_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
+    ('^people/(?P<state>\w\w)/(?P<name>\w+)/$', 'people/il/adrian/', [], {'state': 'il', 'name': 'adrian'}),
  21
+    ('^people/(?P<state>\w\w)/(?P<name>\d)/$', NoReverseMatch, [], {'state': 'il', 'name': 'adrian'}),
  22
+    ('^people/(?P<state>\w\w)/(?P<name>\w+)/$', NoReverseMatch, [], {'state': 'il'}),
  23
+    ('^people/(?P<state>\w\w)/(?P<name>\w+)/$', NoReverseMatch, [], {'name': 'adrian'}),
  24
+    ('^people/(?P<state>\w\w)/(\w+)/$', NoReverseMatch, ['il'], {'name': 'adrian'}),
  25
+    ('^people/(?P<state>\w\w)/(\w+)/$', 'people/il/adrian/', ['adrian'], {'state': 'il'}),
  26
+)
  27
+
  28
+def run_tests(verbosity=0):
  29
+    for regex, expected, args, kwargs in test_data:
  30
+        passed = True
  31
+        try:
  32
+            got = reverse_helper(re.compile(regex), *args, **kwargs)
  33
+        except NoReverseMatch, e:
  34
+            if expected != NoReverseMatch:
  35
+                passed, got = False, str(e)
  36
+        else:
  37
+            if got != expected:
  38
+                passed, got = False, got
  39
+        if passed and verbosity:
  40
+            print "Passed: %s" % regex
  41
+        elif not passed:
  42
+            print "REVERSE LOOKUP FAILED: %s" % regex
  43
+            print "   Got: %s" % got
  44
+            print "   Expected: %r" % expected
  45
+
  46
+if __name__ == "__main__":
  47
+    run_tests(1)

0 notes on commit 3770dbd

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