[PATCH] Remove hard-coded view paths from tests and use reverse() instead

Guilherme Salgado guilherme.salgado at linaro.org
Wed Jun 15 04:51:05 EST 2011


This way the tests won't brek when you change the URL patterns.
---
 apps/patchwork/tests/bundles.py   |   52 +++++++++++++++++--------------------
 apps/patchwork/tests/encodings.py |    8 +++---
 apps/patchwork/tests/filters.py   |    5 ++--
 apps/patchwork/tests/mboxviews.py |   11 ++++----
 apps/patchwork/tests/utils.py     |   10 +++++++
 5 files changed, 47 insertions(+), 39 deletions(-)

diff --git a/apps/patchwork/tests/bundles.py b/apps/patchwork/tests/bundles.py
index 53eee25..e5d2f35 100644
--- a/apps/patchwork/tests/bundles.py
+++ b/apps/patchwork/tests/bundles.py
@@ -22,8 +22,11 @@ import datetime
 from django.test import TestCase
 from django.test.client import Client
 from django.utils.http import urlencode
+from django.core.urlresolvers import reverse
 from patchwork.models import Patch, Bundle, BundlePatch, Person
-from patchwork.tests.utils import defaults, create_user, find_in_context
+from patchwork.tests.utils import (
+    defaults, create_user, find_in_context, get_bundle_url,
+    get_patch_list_url, get_patch_url)
 
 class BundleListTest(TestCase):
     def setUp(self):
@@ -32,7 +35,7 @@ class BundleListTest(TestCase):
                 password = self.user.username)
 
     def testNoBundles(self):
-        response = self.client.get('/user/bundles/')
+        response = self.client.get(reverse('patchwork.views.bundle.bundles'))
         self.failUnlessEqual(response.status_code, 200)
         self.failUnlessEqual(
                 len(find_in_context(response.context, 'bundles')), 0)
@@ -41,7 +44,7 @@ class BundleListTest(TestCase):
         defaults.project.save()
         bundle = Bundle(owner = self.user, project = defaults.project)
         bundle.save()
-        response = self.client.get('/user/bundles/')
+        response = self.client.get(reverse('patchwork.views.bundle.bundles'))
         self.failUnlessEqual(response.status_code, 200)
         self.failUnlessEqual(
                 len(find_in_context(response.context, 'bundles')), 1)
@@ -59,6 +62,7 @@ class BundleTestBase(TestCase):
         self.bundle = Bundle(owner = self.user, project = defaults.project,
                 name = 'testbundle')
         self.bundle.save()
+        self.bundle_url = get_bundle_url(self.bundle.id)
         self.patches = []
 
         for patch_name in patch_names:
@@ -78,7 +82,7 @@ class BundleTestBase(TestCase):
 class BundleViewTest(BundleTestBase):
 
     def testEmptyBundle(self):
-        response = self.client.get('/user/bundle/%d/' % self.bundle.id)
+        response = self.client.get(self.bundle_url)
         self.failUnlessEqual(response.status_code, 200)
         page = find_in_context(response.context, 'page')
         self.failUnlessEqual(len(page.object_list), 0)
@@ -86,7 +90,7 @@ class BundleViewTest(BundleTestBase):
     def testNonEmptyBundle(self):
         self.bundle.append_patch(self.patches[0])
 
-        response = self.client.get('/user/bundle/%d/' % self.bundle.id)
+        response = self.client.get(self.bundle_url)
         self.failUnlessEqual(response.status_code, 200)
         page = find_in_context(response.context, 'page')
         self.failUnlessEqual(len(page.object_list), 1)
@@ -95,7 +99,7 @@ class BundleViewTest(BundleTestBase):
         for patch in self.patches:
             self.bundle.append_patch(patch)
 
-        response = self.client.get('/user/bundle/%d/' % self.bundle.id)
+        response = self.client.get(self.bundle_url)
 
         pos = 0
         for patch in self.patches:
@@ -113,7 +117,7 @@ class BundleViewTest(BundleTestBase):
             bundlepatch.save()
             i += 1
 
-        response = self.client.get('/user/bundle/%d/' % self.bundle.id)
+        response = self.client.get(self.bundle_url)
         pos = len(response.content)
         for patch in self.patches:
             next_pos = response.content.find(patch.name)
@@ -130,8 +134,7 @@ class BundleCreateFromListTest(BundleTestBase):
                   'project': defaults.project.id}
 
         response = self.client.post(
-                '/project/%s/list/' % defaults.project.linkname,
-                params)
+                get_patch_list_url(defaults.project.linkname), params)
 
         self.assertContains(response, 'Bundle %s created' % newbundlename)
 
@@ -146,8 +149,7 @@ class BundleCreateFromListTest(BundleTestBase):
                   'patch_id:%d' % patch.id: 'checked'}
 
         response = self.client.post(
-                '/project/%s/list/' % defaults.project.linkname,
-                params)
+                get_patch_list_url(defaults.project.linkname), params)
 
         self.assertContains(response, 'Bundle %s created' % newbundlename)
         self.assertContains(response, 'added to bundle %s' % newbundlename,
@@ -170,8 +172,7 @@ class BundleCreateFromListTest(BundleTestBase):
                   'patch_id:%d' % patch.id: 'checked'}
 
         response = self.client.post(
-                '/project/%s/list/' % defaults.project.linkname,
-                params)
+                get_patch_list_url(defaults.project.linkname), params)
 
         self.assertContains(response, 'No bundle name was specified',
                 status_code = 200)
@@ -187,7 +188,7 @@ class BundleCreateFromPatchTest(BundleTestBase):
         params = {'name': newbundlename,
                   'action': 'createbundle'}
 
-        response = self.client.post('/patch/%d/' % patch.id, params)
+        response = self.client.post(get_patch_url(patch.id), params)
 
         self.assertContains(response,
                 'Bundle %s created' % newbundlename)
@@ -203,7 +204,7 @@ class BundleCreateFromPatchTest(BundleTestBase):
         params = {'name': newbundlename,
                   'action': 'createbundle'}
 
-        response = self.client.post('/patch/%d/' % patch.id, params)
+        response = self.client.post(get_patch_url(patch.id), params)
 
         self.assertContains(response,
                 'A bundle called %s already exists' % newbundlename)
@@ -221,8 +222,7 @@ class BundleAddFromListTest(BundleTestBase):
                   'patch_id:%d' % patch.id: 'checked'}
 
         response = self.client.post(
-                '/project/%s/list/' % defaults.project.linkname,
-                params)
+                get_patch_list_url(defaults.project.linkname), params)
 
         self.assertContains(response, 'added to bundle %s' % self.bundle.name,
             count = 1)
@@ -240,8 +240,7 @@ class BundleAddFromListTest(BundleTestBase):
                   'patch_id:%d' % patch.id: 'checked'}
 
         response = self.client.post(
-                '/project/%s/list/' % defaults.project.linkname,
-                params)
+                get_patch_list_url(defaults.project.linkname), params)
 
         self.assertContains(response, 'added to bundle %s' % self.bundle.name,
             count = 1)
@@ -268,8 +267,7 @@ class BundleAddFromListTest(BundleTestBase):
                   'patch_id:%d' % patch.id: 'checked'}
 
         response = self.client.post(
-                '/project/%s/list/' % defaults.project.linkname,
-                params)
+                get_patch_list_url(defaults.project.linkname), params)
 
         self.assertContains(response, 'Patch '%s' already in bundle' \
                             % patch.name, count = 1, status_code = 200)
@@ -289,8 +287,7 @@ class BundleAddFromListTest(BundleTestBase):
                   'patch_id:%d' % self.patches[1].id: 'checked'}
 
         response = self.client.post(
-                '/project/%s/list/' % defaults.project.linkname,
-                params)
+                get_patch_list_url(defaults.project.linkname), params)
 
         self.assertContains(response, 'Patch '%s' already in bundle' \
                             % patch.name, count = 1, status_code = 200)
@@ -305,7 +302,7 @@ class BundleAddFromPatchTest(BundleTestBase):
         params = {'action': 'addtobundle',
                   'bundle_id': self.bundle.id}
 
-        response = self.client.post('/patch/%d/' % patch.id, params)
+        response = self.client.post(get_patch_url(patch.id), params)
 
         self.assertContains(response,
                 'added to bundle "%s"' % self.bundle.name,
@@ -320,7 +317,7 @@ class BundleAddFromPatchTest(BundleTestBase):
         params = {'action': 'addtobundle',
                   'bundle_id': self.bundle.id}
 
-        response = self.client.post('/patch/%d/' % patch.id, params)
+        response = self.client.post(get_patch_url(patch.id), params)
 
         self.assertContains(response,
                 'added to bundle "%s"' % self.bundle.name,
@@ -368,7 +365,7 @@ class BundleInitialOrderTest(BundleTestBase):
                ''.join([ '&patch_id:%d=checked' % i for i in ids ])
 
         response = self.client.post(
-                '/project/%s/list/' % defaults.project.linkname,
+                get_patch_list_url(defaults.project.linkname),
                 data = data,
                 content_type = 'application/x-www-form-urlencoded',
                 )
@@ -413,8 +410,7 @@ class BundleReorderTest(BundleTestBase):
                   'order_start': firstpatch.id,
                   'neworder': slice_ids}
 
-        response = self.client.post('/user/bundle/%d/' % self.bundle.id,
-                                    params)
+        response = self.client.post(self.bundle_url, params)
 
         self.failUnlessEqual(response.status_code, 200)
 
diff --git a/apps/patchwork/tests/encodings.py b/apps/patchwork/tests/encodings.py
index b9032bb..c67048c 100644
--- a/apps/patchwork/tests/encodings.py
+++ b/apps/patchwork/tests/encodings.py
@@ -21,7 +21,7 @@ import unittest
 import os
 import time
 from patchwork.models import Patch, Person
-from patchwork.tests.utils import defaults, read_patch
+from patchwork.tests.utils import defaults, read_patch, get_patch_url
 from django.test import TestCase
 from django.test.client import Client
 
@@ -42,17 +42,17 @@ class UTF8PatchViewTest(TestCase):
         self.client = Client()
 
     def testPatchView(self):
-        response = self.client.get('/patch/%d/' % self.patch.id)
+        response = self.client.get(get_patch_url(self.patch.id))
         self.assertContains(response, self.patch.name)
 
     def testMboxView(self):
-        response = self.client.get('/patch/%d/mbox/' % self.patch.id)
+        response = self.client.get(get_patch_url(self.patch.id) + 'mbox/')
         self.assertEquals(response.status_code, 200)
         self.assertTrue(self.patch.content in \
                 response.content.decode(self.patch_encoding))
 
     def testRawView(self):
-        response = self.client.get('/patch/%d/raw/' % self.patch.id)
+        response = self.client.get(get_patch_url(self.patch.id) + 'raw/')
         self.assertEquals(response.status_code, 200)
         self.assertEquals(response.content.decode(self.patch_encoding),
                 self.patch.content)
diff --git a/apps/patchwork/tests/filters.py b/apps/patchwork/tests/filters.py
index 4744583..ea0d5ec 100644
--- a/apps/patchwork/tests/filters.py
+++ b/apps/patchwork/tests/filters.py
@@ -20,7 +20,8 @@
 import unittest
 from django.test import TestCase
 from django.test.client import Client
-from patchwork.tests.utils import defaults, create_user, find_in_context
+from patchwork.tests.utils import (
+    defaults, create_user, find_in_context, get_patch_list_url)
 
 class FilterQueryStringTest(TestCase):
     def testFilterQSEscaping(self):
@@ -29,7 +30,7 @@ class FilterQueryStringTest(TestCase):
            links"""
         project = defaults.project
         defaults.project.save()
-        url = '/project/%s/list/?submitter=a%%26b=c' % project.linkname
+        url = get_patch_list_url(project.linkname) + '?submitter=a%%26b=c'
         response = self.client.get(url)
         self.failUnlessEqual(response.status_code, 200)
         self.failIf('submitter=a&b=c' in response.content)
diff --git a/apps/patchwork/tests/mboxviews.py b/apps/patchwork/tests/mboxviews.py
index a3c10cf..444b2b6 100644
--- a/apps/patchwork/tests/mboxviews.py
+++ b/apps/patchwork/tests/mboxviews.py
@@ -21,7 +21,8 @@ import unittest
 from django.test import TestCase
 from django.test.client import Client
 from patchwork.models import Patch, Comment, Person
-from patchwork.tests.utils import defaults, create_user, find_in_context
+from patchwork.tests.utils import (
+    defaults, create_user, find_in_context, get_patch_url)
 
 class MboxPatchResponseTest(TestCase):
     """ Test that the mbox view appends the Acked-by from a patch comment """
@@ -46,7 +47,7 @@ class MboxPatchResponseTest(TestCase):
         comment.save()
 
     def testPatchResponse(self):
-        response = self.client.get('/patch/%d/mbox/' % self.patch.id)
+        response = self.client.get(get_patch_url(self.patch.id) + 'mbox/')
         self.assertContains(response,
                 'Acked-by: 1\nAcked-by: 2\n')
 
@@ -74,7 +75,7 @@ class MboxPatchSplitResponseTest(TestCase):
         comment.save()
 
     def testPatchResponse(self):
-        response = self.client.get('/patch/%d/mbox/' % self.patch.id)
+        response = self.client.get(get_patch_url(self.patch.id) + 'mbox/')
         self.assertContains(response,
                 'Acked-by: 1\nAcked-by: 2\n')
 
@@ -98,12 +99,12 @@ class MboxPassThroughHeaderTest(TestCase):
         self.patch.headers = self.cc_header + '\n'
         self.patch.save()
 
-        response = self.client.get('/patch/%d/mbox/' % self.patch.id)
+        response = self.client.get(get_patch_url(self.patch.id) + 'mbox/')
         self.assertContains(response, self.cc_header)
 
     def testToHeader(self):
         self.patch.headers = self.to_header + '\n'
         self.patch.save()
 
-        response = self.client.get('/patch/%d/mbox/' % self.patch.id)
+        response = self.client.get(get_patch_url(self.patch.id) + 'mbox/')
         self.assertContains(response, self.to_header)
diff --git a/apps/patchwork/tests/utils.py b/apps/patchwork/tests/utils.py
index f1c95e8..dc3948c 100644
--- a/apps/patchwork/tests/utils.py
+++ b/apps/patchwork/tests/utils.py
@@ -20,6 +20,7 @@
 import os
 import codecs
 from patchwork.models import Project, Person
+from django.core.urlresolvers import reverse
 from django.contrib.auth.models import User
 
 from email import message_from_file
@@ -127,3 +128,12 @@ def create_email(content, subject = None, sender = None, multipart = False,
 
 
     return msg
+
+def get_patch_url(patch_id):
+    return reverse('patchwork.views.patch.patch', args=[patch_id])
+
+def get_patch_list_url(project_id):
+    return reverse('patchwork.views.patch.list', args=[project_id])
+
+def get_bundle_url(bundle_id):
+    return reverse('patchwork.views.bundle.bundle', args=[bundle_id])



More information about the Patchwork mailing list