From patchwork Fri Apr 1 17:07:03 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dirk Wallenstein X-Patchwork-Id: 89307 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from ozlabs.org (localhost [IPv6:::1]) by ozlabs.org (Postfix) with ESMTP id C4A701007DB for ; Sat, 2 Apr 2011 04:07:10 +1100 (EST) Received: from mailout04.t-online.de (mailout04.t-online.de [194.25.134.18]) by ozlabs.org (Postfix) with ESMTP id 9CEFCB6F94 for ; Sat, 2 Apr 2011 04:07:07 +1100 (EST) Received: from fwd18.aul.t-online.de (fwd18.aul.t-online.de ) by mailout04.t-online.de with smtp id 1Q5hoS-0005rY-QM; Fri, 01 Apr 2011 19:07:08 +0200 Received: from localhost (GQhlW8ZQQhZ2fW4ip5FsxvZ62lL9uWwe2TMlhEWr3RiHnTfdLMkINOop7dU2XPuQgS@[84.139.82.68]) by fwd18.t-online.de with esmtp id 1Q5hoM-1LKuy80; Fri, 1 Apr 2011 19:07:02 +0200 Date: Fri, 1 Apr 2011 19:07:03 +0200 From: Dirk Wallenstein To: patchwork@lists.ozlabs.org Subject: Re: [PATCH/RFC v2] Provide basic test fixture through inheritance Message-ID: <20110401170703.GA17690@bottich> References: <20110225193252.9630.30937.stgit@localhost6.localdomain6> <20110226090500.GA11191@zap> <20110305102330.GA15251@zap> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20110305102330.GA15251@zap> User-Agent: Mutt/1.5.20 (2009-06-14) X-ID: GQhlW8ZQQhZ2fW4ip5FsxvZ62lL9uWwe2TMlhEWr3RiHnTfdLMkINOop7dU2XPuQgS X-TOI-MSGID: 016a0fa6-e536-42c8-ade6-b628120d2667 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 Provide project and user objects for test cases through inheritance. Just derive from BaseFixtureTestCase instead of the normal TestCase. The provided objects are accessible as attributes. Signed-off-by: Dirk Wallenstein --- With my new installation I've had errors if the user or user-profile objects don't have consecutive "pk" starting at 1. That's the only change. On Sat, Mar 05, 2011 at 11:23:30AM +0100, Dirk Wallenstein wrote: > 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 7013e85..e3c0bb3 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 @@ -235,7 +236,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 """ @@ -244,33 +245,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): @@ -283,7 +272,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 @@ -293,7 +282,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 f1c95e8..078d982 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 from django.contrib.auth.models import User +import django.test from email import message_from_file try: @@ -127,3 +128,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)