[PATCH 2/2] tests: Remove duplicated bulk patch creation

Stephen Finucane stephen.finucane at intel.com
Fri Feb 19 04:34:26 AEDT 2016


A number of tests were bulk creating patches. Seeing as this is a
common operation, it's better to move this into a common location.

Signed-off-by: Stephen Finucane <stephen.finucane at intel.com>
---
 patchwork/tests/test_bundles.py | 18 +++++-------------
 patchwork/tests/test_updates.py | 12 +++---------
 patchwork/tests/test_user.py    | 23 +++--------------------
 patchwork/tests/test_xmlrpc.py  | 28 +++++-----------------------
 patchwork/tests/utils.py        | 21 ++++++++++++++++++++-
 5 files changed, 36 insertions(+), 66 deletions(-)

diff --git a/patchwork/tests/test_bundles.py b/patchwork/tests/test_bundles.py
index 958bb55..cbed216 100644
--- a/patchwork/tests/test_bundles.py
+++ b/patchwork/tests/test_bundles.py
@@ -25,11 +25,12 @@ import unittest
 from django.conf import settings
 from django.test import TestCase
 from django.utils.http import urlencode
-
-from patchwork.models import Patch, Bundle, BundlePatch, Person
-from patchwork.tests.utils import defaults, create_user, find_in_context
 from django.utils.six.moves import range, zip
 
+from patchwork.models import Bundle, BundlePatch
+from patchwork.tests.utils import (defaults, create_user, find_in_context,
+                                   create_patches)
+
 
 def bundle_url(bundle):
     return '/bundle/%s/%s/' % (bundle.owner.username, bundle.name)
@@ -65,7 +66,6 @@ class BundleTestBase(TestCase):
     fixtures = ['default_states']
 
     def setUp(self, patch_count=3):
-        patch_names = ['testpatch%d' % (i) for i in range(1, patch_count + 1)]
         self.user = create_user()
         self.client.login(username=self.user.username,
                           password=self.user.username)
@@ -73,15 +73,7 @@ class BundleTestBase(TestCase):
         self.bundle = Bundle(owner=self.user, project=defaults.project,
                              name='testbundle')
         self.bundle.save()
-        self.patches = []
-
-        for patch_name in patch_names:
-            patch = Patch(project=defaults.project,
-                          msgid=patch_name, name=patch_name,
-                          submitter=Person.objects.get(user=self.user),
-                          content='')
-            patch.save()
-            self.patches.append(patch)
+        self.patches = create_patches(patch_count)
 
     def tearDown(self):
         for patch in self.patches:
diff --git a/patchwork/tests/test_updates.py b/patchwork/tests/test_updates.py
index d40a0a2..15504f8 100644
--- a/patchwork/tests/test_updates.py
+++ b/patchwork/tests/test_updates.py
@@ -20,8 +20,8 @@
 from django.core.urlresolvers import reverse
 from django.test import TestCase
 
-from patchwork.models import Patch, Person, State
-from patchwork.tests.utils import defaults, create_maintainer
+from patchwork.models import Patch, State
+from patchwork.tests.utils import defaults, create_maintainer, create_patches
 
 
 class MultipleUpdateTest(TestCase):
@@ -38,13 +38,7 @@ class MultipleUpdateTest(TestCase):
             'action': 'Update', 'project': str(defaults.project.id),
             'form': 'patchlistform', 'archived': '*', 'delegate': '*',
             'state': '*'}
-        self.patches = []
-        for name in ['patch one', 'patch two', 'patch three']:
-            patch = Patch(project=defaults.project, msgid=name,
-                          name=name, content='',
-                          submitter=Person.objects.get(user=self.user))
-            patch.save()
-            self.patches.append(patch)
+        self.patches = create_patches(3)
 
     def _selectAllPatches(self, data):
         for patch in self.patches:
diff --git a/patchwork/tests/test_user.py b/patchwork/tests/test_user.py
index bd97eb7..2d8ebf6 100644
--- a/patchwork/tests/test_user.py
+++ b/patchwork/tests/test_user.py
@@ -23,9 +23,9 @@ from django.core import mail
 from django.core.urlresolvers import reverse
 from django.test import TestCase
 
-from patchwork.models import (EmailConfirmation, Person, Bundle, UserProfile,
-                              Patch)
+from patchwork.models import EmailConfirmation, Person, Bundle, UserProfile
 from patchwork.tests.utils import defaults, error_strings
+from patchwork.tests import utils
 
 
 def _confirmation_url(conf):
@@ -144,23 +144,6 @@ class UserProfileTest(TestCase):
         self.client.login(username=self.user.username,
                           password=self.user.password)
 
-    # FIXME(stephenfin) Remove duplication from this and test_xmlrpc
-    def _createPatches(self, count=1):
-        defaults.project.save()
-        defaults.patch_author_person.save()
-
-        patches = []
-
-        for _ in range(0, count):
-            patch = Patch(project=defaults.project,
-                          submitter=defaults.patch_author_person,
-                          msgid=make_msgid(),
-                          content=defaults.patch)
-            patch.save()
-            patches.append(patch)
-
-        return patches
-
     def testUserProfile(self):
         response = self.client.get('/user/')
         self.assertContains(response, 'User Profile: %s' % self.user.username)
@@ -183,7 +166,7 @@ class UserProfileTest(TestCase):
         self.assertContains(response, bundle.get_absolute_url())
 
     def testUserProfileTodos(self):
-        patches = self._createPatches(5)
+        patches = utils.create_patches(5)
         for patch in patches:
             patch.delegate = self.user.user
             patch.save()
diff --git a/patchwork/tests/test_xmlrpc.py b/patchwork/tests/test_xmlrpc.py
index bbb7dfb..a5b69fb 100644
--- a/patchwork/tests/test_xmlrpc.py
+++ b/patchwork/tests/test_xmlrpc.py
@@ -17,7 +17,6 @@
 # along with Patchwork; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-from email.utils import make_msgid
 import unittest
 
 from django.conf import settings
@@ -25,8 +24,7 @@ from django.core.urlresolvers import reverse
 from django.test import LiveServerTestCase
 from django.utils.six.moves import xmlrpc_client
 
-from patchwork.models import Patch
-from patchwork.tests.utils import defaults
+from patchwork.tests import utils
 
 
 @unittest.skipUnless(settings.ENABLE_XMLRPC,
@@ -44,41 +42,25 @@ class XMLRPCTest(LiveServerTestCase):
         self.assertRedirects(response,
                              reverse('help', kwargs={'path': 'pwclient/'}))
 
-    def _createPatches(self, count=1):
-        defaults.project.save()
-        defaults.patch_author_person.save()
-
-        patches = []
-
-        for _ in range(0, count):
-            patch = Patch(project=defaults.project,
-                          submitter=defaults.patch_author_person,
-                          msgid=make_msgid(),
-                          content=defaults.patch)
-            patch.save()
-            patches.append(patch)
-
-        return patches
-
     def testListSingle(self):
-        patch_objs = self._createPatches()
+        patch_objs = utils.create_patches()
         patches = self.rpc.patch_list()
         self.assertEqual(len(patches), 1)
         self.assertEqual(patches[0]['id'], patch_objs[0].id)
 
     def testListMultiple(self):
-        self._createPatches(5)
+        utils.create_patches(5)
         patches = self.rpc.patch_list()
         self.assertEqual(len(patches), 5)
 
     def testListMaxCount(self):
-        patch_objs = self._createPatches(5)
+        patch_objs = utils.create_patches(5)
         patches = self.rpc.patch_list({'max_count': 2})
         self.assertEqual(len(patches), 2)
         self.assertEqual(patches[0]['id'], patch_objs[0].id)
 
     def testListNegativeMaxCount(self):
-        patch_objs = self._createPatches(5)
+        patch_objs = utils.create_patches(5)
         patches = self.rpc.patch_list({'max_count': -1})
         self.assertEqual(len(patches), 1)
         self.assertEqual(patches[0]['id'], patch_objs[-1].id)
diff --git a/patchwork/tests/utils.py b/patchwork/tests/utils.py
index 04250a8..45eb2d3 100644
--- a/patchwork/tests/utils.py
+++ b/patchwork/tests/utils.py
@@ -26,7 +26,7 @@ import os
 
 from django.contrib.auth.models import User
 
-from patchwork.models import Project, Person
+from patchwork.models import Project, Person, Patch
 
 
 # helper functions for tests
@@ -86,6 +86,25 @@ def create_maintainer(project):
     return user
 
 
+def create_patches(count=1):
+    """Create 'count' unique patches."""
+    defaults.project.save()
+    defaults.patch_author_person.save()
+
+    patches = []
+
+    for i in range(0, count):
+        patch = Patch(project=defaults.project,
+                      submitter=defaults.patch_author_person,
+                      msgid=make_msgid(),
+                      name='testpatch%d' % (i + 1),
+                      content=defaults.patch)
+        patch.save()
+        patches.append(patch)
+
+    return patches
+
+
 def find_in_context(context, key):
     if isinstance(context, list):
         for c in context:
-- 
2.0.0



More information about the Patchwork mailing list