From patchwork Sat Mar 5 10:23:30 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dirk Wallenstein X-Patchwork-Id: 85481 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from ozlabs.org (localhost [127.0.0.1]) by ozlabs.org (Postfix) with ESMTP id 6FA02B70CF for ; Sat, 5 Mar 2011 21:23:47 +1100 (EST) Received: from mailout11.t-online.de (mailout11.t-online.de [194.25.134.85]) by ozlabs.org (Postfix) with ESMTP id 9711EB70BA for ; Sat, 5 Mar 2011 21:23:43 +1100 (EST) Received: from fwd25.aul.t-online.de (fwd25.aul.t-online.de ) by mailout11.t-online.de with smtp id 1Pvoe8-0003aS-CC; Sat, 05 Mar 2011 11:23:36 +0100 Received: from localhost (ZASz1cZTohI2uXoQnak16rIrC+YijzKnVcgS-hXk5DwyZhQ82xSmMiHS+823+iFQC2@[84.139.89.207]) by fwd25.t-online.de with esmtp id 1Pvoe5-1MVVse0; Sat, 5 Mar 2011 11:23:33 +0100 Date: Sat, 5 Mar 2011 11:23:30 +0100 From: Dirk Wallenstein To: patchwork@lists.ozlabs.org Subject: [PATCH/RFC] Provide basic test fixture through inheritance Message-ID: <20110305102330.GA15251@zap> References: <20110225193252.9630.30937.stgit@localhost6.localdomain6> <20110226090500.GA11191@zap> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20110226090500.GA11191@zap> User-Agent: Mutt/1.5.21 (2010-09-15) X-ID: ZASz1cZTohI2uXoQnak16rIrC+YijzKnVcgS-hXk5DwyZhQ82xSmMiHS+823+iFQC2 X-TOI-MSGID: a7d10ef0-b732-4457-9678-b2ef7c9cfac1 X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Signed-off-by: Dirk Wallenstein --- 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 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)