[PATCH 22/25] tests: Clean up 'test_patchparser'

Stephen Finucane stephen.finucane at intel.com
Fri Jun 24 07:53:43 AEST 2016


* Make use of 'create_' helper functions
* Include every import on its own line
* Use underscore_case, rather than camelCase
* Rename some functions to make more sense

Signed-off-by: Stephen Finucane <stephen.finucane at intel.com>
---
 patchwork/tests/test_patchparser.py |  246 ++++++++++++++++++-----------------
 1 files changed, 128 insertions(+), 118 deletions(-)

diff --git a/patchwork/tests/test_patchparser.py b/patchwork/tests/test_patchparser.py
index 7cc61ec..47a2e5a 100644
--- a/patchwork/tests/test_patchparser.py
+++ b/patchwork/tests/test_patchparser.py
@@ -23,24 +23,35 @@ from email.utils import make_msgid
 
 from django.test import TestCase
 
-from patchwork.bin.parsemail import (find_content, find_author,
-                                     find_project_by_header, parse_mail,
-                                     find_pull_request, split_prefixes,
-                                     clean_subject, parse_series_marker)
-from patchwork.models import (Project, Person, Patch, Comment, State,
-                              get_default_initial_patch_state)
-from patchwork.tests.utils import (read_patch, read_mail, create_email,
-                                   defaults, create_user)
+from patchwork.bin.parsemail import clean_subject
+from patchwork.bin.parsemail import find_author
+from patchwork.bin.parsemail import find_content
+from patchwork.bin.parsemail import find_project_by_header
+from patchwork.bin.parsemail import find_pull_request
+from patchwork.bin.parsemail import parse_mail
+from patchwork.bin.parsemail import parse_series_marker
+from patchwork.bin.parsemail import split_prefixes
+from patchwork.models import Comment
+from patchwork.models import get_default_initial_patch_state
+from patchwork.models import Patch
+from patchwork.models import Person
+from patchwork.models import State
+from patchwork.tests.utils import create_email
+from patchwork.tests.utils import create_project
+from patchwork.tests.utils import create_user
+from patchwork.tests.utils import read_patch
+from patchwork.tests.utils import read_mail
+from patchwork.tests.utils import SAMPLE_DIFF
 
 
 class PatchTest(TestCase):
+
     fixtures = ['default_states']
-    default_sender = defaults.sender
-    default_subject = defaults.subject
-    project = defaults.project
+    project = create_project()
 
 
 class InlinePatchTest(PatchTest):
+
     patch_filename = '0001-add-line.patch'
     test_content = 'Test for attached patch'
 
@@ -49,14 +60,15 @@ class InlinePatchTest(PatchTest):
         email = create_email(self.test_content + '\n' + self.orig_patch)
         self.diff, self.message = find_content(self.project, email)
 
-    def testPatchContent(self):
+    def test_patch_content(self):
         self.assertEqual(self.diff, self.orig_patch)
 
-    def testCommentContent(self):
+    def test_patch_diff(self):
         self.assertEqual(self.message, self.test_content)
 
 
 class AttachmentPatchTest(InlinePatchTest):
+
     patch_filename = '0001-add-line.patch'
     test_comment = 'Test for attached patch'
     content_subtype = 'x-patch'
@@ -70,10 +82,12 @@ class AttachmentPatchTest(InlinePatchTest):
 
 
 class AttachmentXDiffPatchTest(AttachmentPatchTest):
+
     content_subtype = 'x-diff'
 
 
 class UTF8InlinePatchTest(InlinePatchTest):
+
     patch_filename = '0002-utf-8.patch'
     patch_encoding = 'utf-8'
 
@@ -86,6 +100,7 @@ class UTF8InlinePatchTest(InlinePatchTest):
 
 class NoCharsetInlinePatchTest(InlinePatchTest):
     """Test mails with no content-type or content-encoding header."""
+
     patch_filename = '0001-add-line.patch'
 
     def setUp(self):
@@ -97,6 +112,7 @@ class NoCharsetInlinePatchTest(InlinePatchTest):
 
 
 class SignatureCommentTest(InlinePatchTest):
+
     patch_filename = '0001-add-line.patch'
     test_content = 'Test comment\nmore comment'
 
@@ -109,6 +125,7 @@ class SignatureCommentTest(InlinePatchTest):
 
 
 class ListFooterTest(InlinePatchTest):
+
     patch_filename = '0001-add-line.patch'
     test_content = 'Test comment\nmore comment'
 
@@ -123,23 +140,27 @@ class ListFooterTest(InlinePatchTest):
 
 
 class DiffWordInCommentTest(InlinePatchTest):
+
     test_content = 'Lines can start with words beginning in "diff"\n' + \
                    'difficult\nDifferent'
 
 
 class UpdateCommentTest(InlinePatchTest):
     """Test for '---\nUpdate: v2' style comments to patches."""
+
     patch_filename = '0001-add-line.patch'
     test_content = 'Test comment\nmore comment\n---\nUpdate: test update'
 
 
 class UpdateSigCommentTest(SignatureCommentTest):
     """Test for '---\nUpdate: v2' style comments to patches, with a sig."""
+
     patch_filename = '0001-add-line.patch'
     test_content = 'Test comment\nmore comment\n---\nUpdate: test update'
 
 
 class SenderEncodingTest(TestCase):
+
     sender_name = u'example user'
     sender_email = 'user at example.com'
     from_header = 'example user <user at example.com>'
@@ -153,39 +174,40 @@ class SenderEncodingTest(TestCase):
         self.person = find_author(self.email)
         self.person.save()
 
-    def tearDown(self):
-        self.person.delete()
-
-    def testName(self):
+    def test_name(self):
         self.assertEqual(self.person.name, self.sender_name)
 
-    def testEmail(self):
+    def test_email(self):
         self.assertEqual(self.person.email, self.sender_email)
 
-    def testDBQueryName(self):
+    def test_db_query_name(self):
         db_person = Person.objects.get(name=self.sender_name)
         self.assertEqual(self.person, db_person)
 
-    def testDBQueryEmail(self):
+    def test_db_query_email(self):
         db_person = Person.objects.get(email=self.sender_email)
         self.assertEqual(self.person, db_person)
 
 
 class SenderUTF8QPEncodingTest(SenderEncodingTest):
+
     sender_name = u'\xe9xample user'
     from_header = '=?utf-8?q?=C3=A9xample=20user?= <user at example.com>'
 
 
 class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
+
     sender_name = u'\xe9xample user'
     from_header = '=?utf-8?q?=C3=A9xample?= user <user at example.com>'
 
 
 class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest):
+
     from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= <user at example.com>'
 
 
 class SubjectEncodingTest(PatchTest):
+
     sender = 'example user <user at example.com>'
     subject = 'test subject'
     subject_header = 'test subject'
@@ -194,20 +216,22 @@ class SubjectEncodingTest(PatchTest):
         mail = 'Message-Id: %s\n' % make_msgid() + \
                'From: %s\n' % self.sender + \
                'Subject: %s\n\n' % self.subject_header + \
-               'test\n\n' + defaults.patch
+               'test\n\n' + SAMPLE_DIFF
         self.email = message_from_string(mail)
 
-    def testSubjectEncoding(self):
+    def test_subject_encoding(self):
         name, _ = clean_subject(self.email.get('Subject'))
         self.assertEqual(name, self.subject)
 
 
 class SubjectUTF8QPEncodingTest(SubjectEncodingTest):
+
     subject = u'test s\xfcbject'
     subject_header = '=?utf-8?q?test=20s=c3=bcbject?='
 
 
 class SubjectUTF8QPMultipleEncodingTest(SubjectEncodingTest):
+
     subject = u'test s\xfcbject'
     subject_header = 'test =?utf-8?q?s=c3=bcbject?='
 
@@ -219,6 +243,7 @@ class SenderCorrelationTest(TestCase):
 
     http://stackoverflow.com/a/19379636/613428
     """
+
     existing_sender = 'Existing Sender <existing at example.com>'
     non_existing_sender = 'Non-existing Sender <nonexisting at example.com>'
 
@@ -235,48 +260,41 @@ class SenderCorrelationTest(TestCase):
         self.person = find_author(self.existing_sender_mail)
         self.person.save()
 
-    def testExistingSender(self):
+    def test_existing_sender(self):
         person = find_author(self.existing_sender_mail)
         self.assertEqual(person._state.adding, False)
         self.assertEqual(person.id, self.person.id)
 
-    def testNonExistingSender(self):
+    def test_non_existing_sender(self):
         person = find_author(self.non_existing_sender_mail)
         self.assertEqual(person._state.adding, True)
         self.assertEqual(person.id, None)
 
-    def testExistingDifferentFormat(self):
+    def test_existing_different_format(self):
         mail = self.mail('existing at example.com')
         person = find_author(mail)
         self.assertEqual(person._state.adding, False)
         self.assertEqual(person.id, self.person.id)
 
-    def testExistingDifferentCase(self):
+    def test_existing_different_case(self):
         mail = self.mail(self.existing_sender.upper())
         person = find_author(mail)
         self.assertEqual(person._state.adding, False)
         self.assertEqual(person.id, self.person.id)
 
-    def tearDown(self):
-        self.person.delete()
-
 
 class MultipleProjectPatchTest(TestCase):
     """Test that patches sent to multiple patchwork projects are
        handled correctly."""
+
     fixtures = ['default_states']
     test_content = 'Test Comment'
     patch_filename = '0001-add-line.patch'
     msgid = '<1 at example.com>'
 
     def setUp(self):
-        self.p1 = Project(linkname='test-project-1', name='Project 1',
-                          listid='1.example.com', listemail='1 at example.com')
-        self.p2 = Project(linkname='test-project-2', name='Project 2',
-                          listid='2.example.com', listemail='2 at example.com')
-
-        self.p1.save()
-        self.p2.save()
+        self.p1 = create_project()
+        self.p2 = create_project()
 
         patch = read_patch(self.patch_filename)
         email = create_email(self.test_content + '\n' + patch)
@@ -291,18 +309,15 @@ class MultipleProjectPatchTest(TestCase):
         email['List-ID'] = '<' + self.p2.listid + '>'
         parse_mail(email)
 
-    def testParsedProjects(self):
+    def test_parsed_projects(self):
         self.assertEqual(Patch.objects.filter(project=self.p1).count(), 1)
         self.assertEqual(Patch.objects.filter(project=self.p2).count(), 1)
 
-    def tearDown(self):
-        self.p1.delete()
-        self.p2.delete()
-
 
 class MultipleProjectPatchCommentTest(MultipleProjectPatchTest):
     """Test that followups to multiple-project patches end up on the
        correct patch."""
+
     comment_msgid = '<2 at example.com>'
     comment_content = 'test comment'
 
@@ -311,14 +326,14 @@ class MultipleProjectPatchCommentTest(MultipleProjectPatchTest):
 
         for project in [self.p1, self.p2]:
             email = MIMEText(self.comment_content)
-            email['From'] = defaults.sender
-            email['Subject'] = defaults.subject
+            email['From'] = 'Patch Author <patch-author at example.com>'
+            email['Subject'] = 'Test Subject'
             email['Message-Id'] = self.comment_msgid
             email['List-ID'] = '<' + project.listid + '>'
             email['In-Reply-To'] = self.msgid
             parse_mail(email)
 
-    def testParsedComment(self):
+    def test_parsed_comment(self):
         for project in [self.p1, self.p2]:
             patch = Patch.objects.filter(project=project)[0]
             # we should see the reply comment only
@@ -328,37 +343,34 @@ class MultipleProjectPatchCommentTest(MultipleProjectPatchTest):
 
 class ListIdHeaderTest(TestCase):
     """Test that we parse List-Id headers from mails correctly."""
+
     def setUp(self):
-        self.project = Project(linkname='test-project-1',
-                               name='Project 1',
-                               listid='1.example.com',
-                               listemail='1 at example.com')
-        self.project.save()
+        self.project = create_project()
 
-    def testNoListId(self):
+    def test_no_list_id(self):
         email = MIMEText('')
         project = find_project_by_header(email)
         self.assertEqual(project, None)
 
-    def testBlankListId(self):
+    def test_blank_list_id(self):
         email = MIMEText('')
         email['List-Id'] = ''
         project = find_project_by_header(email)
         self.assertEqual(project, None)
 
-    def testWhitespaceListId(self):
+    def test_whitespace_list_id(self):
         email = MIMEText('')
         email['List-Id'] = ' '
         project = find_project_by_header(email)
         self.assertEqual(project, None)
 
-    def testSubstringListId(self):
+    def test_substring_list_id(self):
         email = MIMEText('')
         email['List-Id'] = 'example.com'
         project = find_project_by_header(email)
         self.assertEqual(project, None)
 
-    def testShortListId(self):
+    def test_short_list_id(self):
         """Some mailing lists have List-Id headers in short formats, where it
            is only the list ID itself (without enclosing angle-brackets). """
         email = MIMEText('')
@@ -366,25 +378,24 @@ class ListIdHeaderTest(TestCase):
         project = find_project_by_header(email)
         self.assertEqual(project, self.project)
 
-    def testLongListId(self):
+    def test_long_list_id(self):
         email = MIMEText('')
         email['List-Id'] = 'Test text <%s>' % self.project.listid
         project = find_project_by_header(email)
         self.assertEqual(project, self.project)
 
-    def tearDown(self):
-        self.project.delete()
-
 
 class MBoxPatchTest(PatchTest):
+
     def setUp(self):
         self.mail = read_mail(self.mail_file, project=self.project)
 
 
 class GitPullTest(MBoxPatchTest):
+
     mail_file = '0001-git-pull-request.mbox'
 
-    def testGitPullRequest(self):
+    def test_git_pull_request(self):
         diff, message = find_content(self.project, self.mail)
         pull_url = find_pull_request(message)
         self.assertTrue(diff is None)
@@ -393,13 +404,15 @@ class GitPullTest(MBoxPatchTest):
 
 
 class GitPullWrappedTest(GitPullTest):
+
     mail_file = '0002-git-pull-request-wrapped.mbox'
 
 
 class GitPullWithDiffTest(MBoxPatchTest):
+
     mail_file = '0003-git-pull-request-with-diff.mbox'
 
-    def testGitPullWithDiff(self):
+    def test_git_pull_with_diff(self):
         diff, message = find_content(self.project, self.mail)
         pull_url = find_pull_request(message)
         self.assertEqual('git://git.kernel.org/pub/scm/linux/kernel/git/tip/'
@@ -412,21 +425,25 @@ class GitPullWithDiffTest(MBoxPatchTest):
 
 
 class GitPullGitSSHUrlTest(GitPullTest):
+
     mail_file = '0004-git-pull-request-git+ssh.mbox'
 
 
 class GitPullSSHUrlTest(GitPullTest):
+
     mail_file = '0005-git-pull-request-ssh.mbox'
 
 
 class GitPullHTTPUrlTest(GitPullTest):
+
     mail_file = '0006-git-pull-request-http.mbox'
 
 
 class GitRenameOnlyTest(MBoxPatchTest):
+
     mail_file = '0008-git-rename.mbox'
 
-    def testGitRename(self):
+    def test_git_rename(self):
         diff, _ = find_content(self.project, self.mail)
         self.assertTrue(diff is not None)
         self.assertEqual(diff.count("\nrename from "), 2)
@@ -434,9 +451,10 @@ class GitRenameOnlyTest(MBoxPatchTest):
 
 
 class GitRenameWithDiffTest(MBoxPatchTest):
+
     mail_file = '0009-git-rename-with-diff.mbox'
 
-    def testGitRename(self):
+    def test_git_rename(self):
         diff, message = find_content(self.project, self.mail)
         self.assertTrue(diff is not None)
         self.assertTrue(message is not None)
@@ -446,9 +464,10 @@ class GitRenameWithDiffTest(MBoxPatchTest):
 
 
 class CVSFormatPatchTest(MBoxPatchTest):
+
     mail_file = '0007-cvs-format-diff.mbox'
 
-    def testPatch(self):
+    def test_patch(self):
         diff, message = find_content(self.project, self.mail)
         self.assertTrue(diff.startswith('Index'))
 
@@ -456,18 +475,20 @@ class CVSFormatPatchTest(MBoxPatchTest):
 class CharsetFallbackPatchTest(MBoxPatchTest):
     """Test mail with and invalid charset name, and check that we can parse
        with one of the fallback encodings."""
+
     mail_file = '0010-invalid-charset.mbox'
 
-    def testPatch(self):
+    def test_patch(self):
         diff, message = find_content(self.project, self.mail)
         self.assertTrue(diff is not None)
         self.assertTrue(message is not None)
 
 
 class NoNewlineAtEndOfFilePatchTest(MBoxPatchTest):
+
     mail_file = '0011-no-newline-at-end-of-file.mbox'
 
-    def testPatch(self):
+    def test_patch(self):
         diff, message = find_content(self.project, self.mail)
         self.assertTrue(diff is not None)
         self.assertTrue(message is not None)
@@ -484,6 +505,7 @@ class NoNewlineAtEndOfFilePatchTest(MBoxPatchTest):
 
 
 class DelegateRequestTest(TestCase):
+
     fixtures = ['default_states']
     patch_filename = '0001-add-line.patch'
     msgid = '<1 at example.com>'
@@ -492,45 +514,40 @@ class DelegateRequestTest(TestCase):
     def setUp(self):
         self.patch = read_patch(self.patch_filename)
         self.user = create_user()
-        self.p1 = Project(linkname='test-project-1', name='Project 1',
-                          listid='1.example.com', listemail='1 at example.com')
-        self.p1.save()
+        self.project = create_project()
 
-    def get_email(self):
+    def _get_email(self):
         email = create_email(self.patch)
         del email['List-ID']
-        email['List-ID'] = '<' + self.p1.listid + '>'
+        email['List-ID'] = '<' + self.project.listid + '>'
         email['Message-Id'] = self.msgid
         return email
 
-    def _assertDelegate(self, delegate):
-        query = Patch.objects.filter(project=self.p1)
+    def assertDelegate(self, delegate):
+        query = Patch.objects.filter(project=self.project)
         self.assertEqual(query.count(), 1)
         self.assertEqual(query[0].delegate, delegate)
 
-    def testDelegate(self):
-        email = self.get_email()
+    def test_delegate(self):
+        email = self._get_email()
         email['X-Patchwork-Delegate'] = self.user.email
         parse_mail(email)
-        self._assertDelegate(self.user)
+        self.assertDelegate(self.user)
 
-    def testNoDelegate(self):
-        email = self.get_email()
+    def test_no_delegate(self):
+        email = self._get_email()
         parse_mail(email)
-        self._assertDelegate(None)
+        self.assertDelegate(None)
 
-    def testInvalidDelegateFallsBackToNoDelegate(self):
-        email = self.get_email()
+    def test_invalid_delegate(self):
+        email = self._get_email()
         email['X-Patchwork-Delegate'] = self.invalid_delegate_email
         parse_mail(email)
-        self._assertDelegate(None)
-
-    def tearDown(self):
-        self.p1.delete()
-        self.user.delete()
+        self.assertDelegate(None)
 
 
 class InitialPatchStateTest(TestCase):
+
     fixtures = ['default_states']
     patch_filename = '0001-add-line.patch'
     msgid = '<1 at example.com>'
@@ -539,77 +556,70 @@ class InitialPatchStateTest(TestCase):
     def setUp(self):
         self.patch = read_patch(self.patch_filename)
         self.user = create_user()
-        self.p1 = Project(linkname='test-project-1', name='Project 1',
-                          listid='1.example.com', listemail='1 at example.com')
-        self.p1.save()
+        self.project = create_project()
         self.default_state = get_default_initial_patch_state()
         self.nondefault_state = State.objects.get(name="Accepted")
 
-    def get_email(self):
+    def _get_email(self):
         email = create_email(self.patch)
         del email['List-ID']
-        email['List-ID'] = '<' + self.p1.listid + '>'
+        email['List-ID'] = '<' + self.project.listid + '>'
         email['Message-Id'] = self.msgid
         return email
 
-    def _assertState(self, state):
-        query = Patch.objects.filter(project=self.p1)
+    def assertState(self, state):
+        query = Patch.objects.filter(project=self.project)
         self.assertEqual(query.count(), 1)
         self.assertEqual(query[0].state, state)
 
-    def testNonDefaultStateIsActuallyNotTheDefaultState(self):
+    def test_non_default_state(self):
         self.assertNotEqual(self.default_state, self.nondefault_state)
 
-    def testExplicitNonDefaultStateRequest(self):
-        email = self.get_email()
+    def test_explicit_non_default_state_request(self):
+        email = self._get_email()
         email['X-Patchwork-State'] = self.nondefault_state.name
         parse_mail(email)
-        self._assertState(self.nondefault_state)
+        self.assertState(self.nondefault_state)
 
-    def testExplicitDefaultStateRequest(self):
-        email = self.get_email()
+    def test_explicit_default_state_request(self):
+        email = self._get_email()
         email['X-Patchwork-State'] = self.default_state.name
         parse_mail(email)
-        self._assertState(self.default_state)
+        self.assertState(self.default_state)
 
-    def testImplicitDefaultStateRequest(self):
-        email = self.get_email()
+    def test_implicit_default_state_request(self):
+        email = self._get_email()
         parse_mail(email)
-        self._assertState(self.default_state)
+        self.assertState(self.default_state)
 
-    def testInvalidTestStateDoesNotExist(self):
+    def test_invalid_state(self):
+        # make sure it's actually invalid
         with self.assertRaises(State.DoesNotExist):
             State.objects.get(name=self.invalid_state_name)
 
-    def testInvalidStateRequestFallsBackToDefaultState(self):
-        email = self.get_email()
+        email = self._get_email()
         email['X-Patchwork-State'] = self.invalid_state_name
         parse_mail(email)
-        self._assertState(self.default_state)
-
-    def tearDown(self):
-        self.p1.delete()
-        self.user.delete()
+        self.assertState(self.default_state)
 
 
 class ParseInitialTagsTest(PatchTest):
+
+    fixtures = ['default_tags', 'default_states']
     patch_filename = '0001-add-line.patch'
     test_content = ('test comment\n\n' +
                     'Tested-by: Test User <test at example.com>\n' +
                     'Reviewed-by: Test User <test at example.com>\n')
-    fixtures = ['default_tags', 'default_states']
 
     def setUp(self):
-        project = defaults.project
-        project.listid = 'test.example.com'
-        project.save()
+        project = create_project(listid='test.example.com')
         self.orig_patch = read_patch(self.patch_filename)
         email = create_email(self.test_content + '\n' + self.orig_patch,
                              project=project)
         email['Message-Id'] = '<1 at example.com>'
         parse_mail(email)
 
-    def testTags(self):
+    def test_tags(self):
         self.assertEqual(Patch.objects.count(), 1)
         patch = Patch.objects.all()[0]
         self.assertEqual(patch.patchtag_set.filter(
@@ -622,7 +632,7 @@ class ParseInitialTagsTest(PatchTest):
 
 class PrefixTest(TestCase):
 
-    def testSplitPrefixes(self):
+    def test_split_prefixes(self):
         self.assertEqual(split_prefixes('PATCH'), ['PATCH'])
         self.assertEqual(split_prefixes('PATCH,RFC'), ['PATCH', 'RFC'])
         self.assertEqual(split_prefixes(''), [])
@@ -631,7 +641,7 @@ class PrefixTest(TestCase):
         self.assertEqual(split_prefixes('PATCH,RFC'), ['PATCH', 'RFC'])
         self.assertEqual(split_prefixes('PATCH 1/2'), ['PATCH', '1/2'])
 
-    def testSeriesMarkers(self):
+    def test_series_markers(self):
         self.assertEqual(parse_series_marker([]), (None, None))
         self.assertEqual(parse_series_marker(['bar']), (None, None))
         self.assertEqual(parse_series_marker(['bar', '1/2']), (1, 2))
@@ -640,7 +650,7 @@ class PrefixTest(TestCase):
 
 class SubjectTest(TestCase):
 
-    def testCleanSubject(self):
+    def test_clean_subject(self):
         self.assertEqual(clean_subject('meep'), ('meep', []))
         self.assertEqual(clean_subject('Re: meep'), ('meep', []))
         self.assertEqual(clean_subject('[PATCH] meep'), ('meep', []))
-- 
1.7.4.1



More information about the Patchwork mailing list