Patchwork [PATCH/RFC] Provide basic test fixture through inheritance

login
register
mail settings
Submitter Dirk Wallenstein
Date March 5, 2011, 10:23 a.m.
Message ID <20110305102330.GA15251@zap>
Download mbox | patch
Permalink /patch/85481/
State Changes Requested
Headers show

Comments

Dirk Wallenstein - March 5, 2011, 10:23 a.m.
Signed-off-by: Dirk Wallenstein <halsmit@t-online.de>
---
This is what I meant.  No need to create any projects or users in the
majority of test cases.  The only problem I see is that the attribute
syntax somewhat hides the query that is taking place and so a local
reference is necessary to be able to save changes.  This won't work:

   self.project_a.linkname = "any"
   self.project_a.save()

Caching cannot be done because then changes wouldn't be undone after a
test.  

However, changing user or project settings seems to be the rare exception,
and so I think a fat note should do.

 apps/patchwork/fixtures/testbase.json |  142 +++++++++++++++++++++++++++++++++
 apps/patchwork/tests/patchparser.py   |   29 ++-----
 apps/patchwork/tests/utils.py         |   26 ++++++
 3 files changed, 177 insertions(+), 20 deletions(-)
 create mode 100644 apps/patchwork/fixtures/testbase.json

Patch

diff --git a/apps/patchwork/fixtures/testbase.json b/apps/patchwork/fixtures/testbase.json
new file mode 100644
index 0000000..33ef842
--- /dev/null
+++ b/apps/patchwork/fixtures/testbase.json
@@ -0,0 +1,142 @@ 
+[
+    {
+        "pk": 4, 
+        "model": "auth.user", 
+        "fields": {
+            "username": "user_a", 
+            "first_name": "A", 
+            "last_name": "User", 
+            "is_active": true, 
+            "is_superuser": false, 
+            "is_staff": false, 
+            "last_login": "2011-02-28 20:08:51", 
+            "groups": [], 
+            "user_permissions": [], 
+            "password": "sha1$ccf11$82751d31568a5e6880cb3ae32d3726b29631d2da", 
+            "email": "user-a@test.net", 
+            "date_joined": "2011-02-28 20:08:51"
+        }
+    }, 
+    {
+        "pk": 5, 
+        "model": "auth.user", 
+        "fields": {
+            "username": "user_b", 
+            "first_name": "B", 
+            "last_name": "User", 
+            "is_active": true, 
+            "is_superuser": false, 
+            "is_staff": false, 
+            "last_login": "2011-02-28 20:10:21", 
+            "groups": [], 
+            "user_permissions": [], 
+            "password": "sha1$973b0$8474b91f6b0c87d946963da46e433ed2aae0b75b", 
+            "email": "user-b@test.net", 
+            "date_joined": "2011-02-28 20:10:21"
+        }
+    }, 
+    {
+        "pk": 6, 
+        "model": "auth.user", 
+        "fields": {
+            "username": "maintainer_a", 
+            "first_name": "A", 
+            "last_name": "Maintainer", 
+            "is_active": true, 
+            "is_superuser": false, 
+            "is_staff": true, 
+            "last_login": "2011-03-03 15:34:04", 
+            "groups": [], 
+            "user_permissions": [], 
+            "password": "sha1$04f46$90527a49e7ead0c92c4a2c74445d7e94f95c85f3", 
+            "email": "maintainer-a@test.net", 
+            "date_joined": "2011-03-03 15:34:04"
+        }
+    }, 
+    {
+        "pk": 7, 
+        "model": "auth.user", 
+        "fields": {
+            "username": "maintainer_b", 
+            "first_name": "B", 
+            "last_name": "Maintainer", 
+            "is_active": true, 
+            "is_superuser": false, 
+            "is_staff": true, 
+            "last_login": "2011-03-03 15:35:01", 
+            "groups": [], 
+            "user_permissions": [], 
+            "password": "sha1$9543d$ad920f6fbf1da12093a2f44a4f7d377d665e0a1c", 
+            "email": "maintainer-b@test.net", 
+            "date_joined": "2011-03-03 15:35:01"
+        }
+    }, 
+    {
+        "pk": 3, 
+        "model": "patchwork.userprofile", 
+        "fields": {
+            "maintainer_projects": [], 
+            "send_email": false, 
+            "patches_per_page": 100, 
+            "user": 4, 
+            "primary_project": 4
+        }
+    }, 
+    {
+        "pk": 4, 
+        "model": "patchwork.userprofile", 
+        "fields": {
+            "maintainer_projects": [], 
+            "send_email": false, 
+            "patches_per_page": 100, 
+            "user": 5, 
+            "primary_project": 5
+        }
+    }, 
+    {
+        "pk": 5, 
+        "model": "patchwork.userprofile", 
+        "fields": {
+            "maintainer_projects": [
+                4
+            ], 
+            "send_email": true, 
+            "patches_per_page": 100, 
+            "user": 6, 
+            "primary_project": 4
+        }
+    }, 
+    {
+        "pk": 6, 
+        "model": "patchwork.userprofile", 
+        "fields": {
+            "maintainer_projects": [
+                5
+            ], 
+            "send_email": true, 
+            "patches_per_page": 100, 
+            "user": 7, 
+            "primary_project": 5
+        }
+    }, 
+    {
+        "pk": 4, 
+        "model": "patchwork.project", 
+        "fields": {
+            "linkname": "project_a_link", 
+            "listemail": "project-a@test.net", 
+            "name": "project_a", 
+            "listid": "project-a.lists.test.net"
+        }
+    }, 
+    {
+        "pk": 5, 
+        "model": "patchwork.project", 
+        "fields": {
+            "linkname": "project_b_link", 
+            "listemail": "project-b@test.net", 
+            "name": "project_b", 
+            "listid": "project-b.lists.test.net"
+        }
+    }
+]
diff --git a/apps/patchwork/tests/patchparser.py b/apps/patchwork/tests/patchparser.py
index 357ffc1..0cfab9b 100644
--- a/apps/patchwork/tests/patchparser.py
+++ b/apps/patchwork/tests/patchparser.py
@@ -21,7 +21,8 @@  import unittest
 import os
 from email import message_from_string
 from patchwork.models import Project, Person, Patch, Comment
-from patchwork.tests.utils import read_patch, read_mail, create_email, defaults
+from patchwork.tests.utils import read_patch, read_mail, create_email, defaults, \
+        BaseFixtureTestCase
 
 try:
     from email.mime.text import MIMEText
@@ -215,7 +216,7 @@  class SenderCorrelationTest(unittest.TestCase):
     def tearDown(self):
         self.person.delete()
 
-class MultipleProjectPatchTest(unittest.TestCase):
+class MultipleProjectPatchTest(BaseFixtureTestCase):
     """ Test that patches sent to multiple patchwork projects are
         handled correctly """
 
@@ -224,33 +225,21 @@  class MultipleProjectPatchTest(unittest.TestCase):
     msgid = '<1@example.com>'
 
     def setUp(self):
-        self.p1 = Project(linkname = 'test-project-1', name = 'Project 1',
-                listid = '1.example.com', listemail='1@example.com')
-        self.p2 = Project(linkname = 'test-project-2', name = 'Project 2',
-                listid = '2.example.com', listemail='2@example.com')
-
-        self.p1.save()
-        self.p2.save()
-
         patch = read_patch(self.patch_filename)
         email = create_email(self.test_comment + '\n' + patch)
         email['Message-Id'] = self.msgid
 
         del email['List-ID']
-        email['List-ID'] = '<' + self.p1.listid + '>'
+        email['List-ID'] = '<' + self.project_a.listid + '>'
         parse_mail(email)
 
         del email['List-ID']
-        email['List-ID'] = '<' + self.p2.listid + '>'
+        email['List-ID'] = '<' + self.project_b.listid + '>'
         parse_mail(email)
 
     def testParsedProjects(self):
-        self.assertEquals(Patch.objects.filter(project = self.p1).count(), 1)
-        self.assertEquals(Patch.objects.filter(project = self.p2).count(), 1)
-
-    def tearDown(self):
-        self.p1.delete()
-        self.p2.delete()
+        self.assertEquals(Patch.objects.filter(project=self.project_a).count(), 1)
+        self.assertEquals(Patch.objects.filter(project=self.project_b).count(), 1)
 
 
 class MultipleProjectPatchCommentTest(MultipleProjectPatchTest):
@@ -263,7 +252,7 @@  class MultipleProjectPatchCommentTest(MultipleProjectPatchTest):
     def setUp(self):
         super(MultipleProjectPatchCommentTest, self).setUp()
 
-        for project in [self.p1, self.p2]:
+        for project in [self.project_a, self.project_b]:
             email = MIMEText(self.comment_content)
             email['From'] = defaults.sender
             email['Subject'] = defaults.subject
@@ -273,7 +262,7 @@  class MultipleProjectPatchCommentTest(MultipleProjectPatchTest):
             parse_mail(email)
 
     def testParsedComment(self):
-        for project in [self.p1, self.p2]:
+        for project in [self.project_a, self.project_b]:
             patch = Patch.objects.filter(project = project)[0]
             # we should see two comments now - the original mail with the patch,
             # and the one we parsed in setUp()
diff --git a/apps/patchwork/tests/utils.py b/apps/patchwork/tests/utils.py
index 5fd2dae..dec9610 100644
--- a/apps/patchwork/tests/utils.py
+++ b/apps/patchwork/tests/utils.py
@@ -21,6 +21,7 @@  import os
 import codecs
 from patchwork.models import Project, Person, UserProfile
 from django.contrib.auth.models import User
+import django.test
 
 from email import message_from_file
 try:
@@ -124,3 +125,28 @@  def create_email(content, subject = None, sender = None, multipart = False,
 
 
     return msg
+
+class BaseFixtureTestCase(django.test.TestCase):
+    """ Facilitate access to the objects provided in the testbase fixture. 
+
+        The objects listed in _projects and _users are directly accessible as
+        attributes.  Note that the access through attributes just wraps a
+        query.  This means that a local reference to call save() on is
+        necessary to apply changes.
+    """ 
+
+    fixtures = ['testbase']
+
+    # Two arbitrary projects
+    _projects = ['project_a', 'project_b']
+    # Two standard users with the corresponding project as primary project, and
+    # a maintainer for each project with staff status
+    _users = ['user_a', 'user_b', 'maintainer_a', 'maintainer_b']
+
+    def __getattr__(self, name):
+        if name in self._projects:
+            return Project.objects.get(name=name)
+        if name in self._users:
+            return User.objects.get(name=name)
+        else:
+            super(BaseFixtureTestCase, self).__getattr__(name)