]> git.ozlabs.org Git - patchwork/commitdiff
[test] Move helper functions into tests/utils.py
authorJeremy Kerr <jk@ozlabs.org>
Sat, 20 Sep 2008 04:11:49 +0000 (14:11 +1000)
committerJeremy Kerr <jk@ozlabs.org>
Sat, 20 Sep 2008 04:11:49 +0000 (14:11 +1000)
So that we can use them for other tests

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
apps/patchwork/tests/patchparser.py
apps/patchwork/tests/utils.py [new file with mode: 0644]

index 6d315f09ecd4729d04aa25016d4f6784f70f96a4..649da0a2f684f5ed3face1da5ffd968702afc76b 100644 (file)
@@ -21,46 +21,18 @@ import unittest
 import os
 from email import message_from_string
 from patchwork.models import Project, Person
+from patchwork.tests.utils import read_patch, create_email, defaults
 
 try:
     from email.mime.text import MIMEText
-    from email.mime.multipart import MIMEMultipart
 except ImportError:
     # Python 2.4 compatibility
     from email.MIMEText import MIMEText
-    from email.MIMEMultipart import MIMEMultipart
-
-test_mail_dir  = 'patchwork/tests/mail'
-test_patch_dir = 'patchwork/tests/patches'
 
 class PatchTest(unittest.TestCase):
-    default_sender = 'Test Author <test@exmaple.com>'
-    default_subject = 'Test Subject'
-    project = Project(linkname = 'test-project')
-
-    def create_email(self, content, subject = None, sender = None,
-            multipart = False):
-        if subject is None:
-            subject = self.default_subject
-        if sender is None:
-            sender = self.default_sender
-
-        if multipart:
-            msg = MIMEMultipart()
-            body = MIMEText(content, _subtype = 'plain')
-            msg.attach(body)
-        else:
-            msg = MIMEText(content)
-
-        msg['Subject'] = subject
-        msg['From'] = sender
-        msg['List-Id'] = self.project.linkname
-
-        return msg
-
-    def read_patch(self, filename):
-        return file(os.path.join(test_patch_dir, filename)).read()
-
+    default_sender = defaults.sender
+    default_subject = defaults.subject
+    project = defaults.project
 
 from patchwork.bin.parsemail import find_content, find_author
 
@@ -69,8 +41,8 @@ class InlinePatchTest(PatchTest):
     test_comment = 'Test for attached patch'
 
     def setUp(self):
-        self.orig_patch = self.read_patch(self.patch_filename)
-        email = self.create_email(self.test_comment + '\n' + self.orig_patch)
+        self.orig_patch = read_patch(self.patch_filename)
+        email = create_email(self.test_comment + '\n' + self.orig_patch)
         (self.patch, self.comment) = find_content(self.project, email)
 
     def testPatchPresence(self):
@@ -91,8 +63,8 @@ class AttachmentPatchTest(InlinePatchTest):
     test_comment = 'Test for attached patch'
 
     def setUp(self):
-        self.orig_patch = self.read_patch(self.patch_filename)
-        email = self.create_email(self.test_comment, multipart = True)
+        self.orig_patch = read_patch(self.patch_filename)
+        email = create_email(self.test_comment, multipart = True)
         attachment = MIMEText(self.orig_patch, _subtype = 'x-patch')
         email.attach(attachment)
         (self.patch, self.comment) = find_content(self.project, email)
@@ -103,8 +75,8 @@ class SignatureCommentTest(InlinePatchTest):
     test_comment = 'Test comment\nmore comment'
 
     def setUp(self):
-        self.orig_patch = self.read_patch(self.patch_filename)
-        email = self.create_email( \
+        self.orig_patch = read_patch(self.patch_filename)
+        email = create_email( \
                 self.test_comment + '\n' + \
                 '-- \nsig\n' + self.orig_patch)
         (self.patch, self.comment) = find_content(self.project, email)
@@ -115,8 +87,8 @@ class ListFooterTest(InlinePatchTest):
     test_comment = 'Test comment\nmore comment'
 
     def setUp(self):
-        self.orig_patch = self.read_patch(self.patch_filename)
-        email = self.create_email( \
+        self.orig_patch = read_patch(self.patch_filename)
+        email = create_email( \
                 self.test_comment + '\n' + \
                 '_______________________________________________\n' + \
                 'Linuxppc-dev mailing list\n' + \
diff --git a/apps/patchwork/tests/utils.py b/apps/patchwork/tests/utils.py
new file mode 100644 (file)
index 0000000..c7be6ab
--- /dev/null
@@ -0,0 +1,71 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+import os
+from patchwork.models import Project
+try:
+    from email.mime.text import MIMEText
+    from email.mime.multipart import MIMEMultipart
+except ImportError:
+    # Python 2.4 compatibility
+    from email.MIMEText import MIMEText
+    from email.MIMEMultipart import MIMEMultipart
+
+# helper functions for tests
+_test_mail_dir  = 'patchwork/tests/mail'
+_test_patch_dir = 'patchwork/tests/patches'
+
+class defaults(object):
+    project = Project(linkname = 'test-project', name = 'Test Project')
+
+    patch_author = 'Patch Author <patch-author@example.com>'
+
+    comment_author = 'Comment Author <comment-author@example.com>'
+
+    sender = 'Test Author <test-author@example.com>'
+
+    subject = 'Test Subject'
+
+    patch_name = 'Test Patch'
+
+
+def read_patch(filename):
+    return file(os.path.join(_test_patch_dir, filename)).read()
+
+def create_email(content, subject = None, sender = None, multipart = False,
+        project = None):
+    if subject is None:
+        subject = defaults.subject
+    if sender is None:
+        sender = defaults.sender
+    if project is None:
+        project = defaults.project
+
+    if multipart:
+        msg = MIMEMultipart()
+        body = MIMEText(content, _subtype = 'plain')
+        msg.attach(body)
+    else:
+        msg = MIMEText(content)
+
+    msg['Subject'] = subject
+    msg['From'] = sender
+    msg['List-Id'] = project.linkname
+
+    return msg