From patchwork Fri Apr 29 13:07:45 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guilherme Salgado X-Patchwork-Id: 93422 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 305F71007F7 for ; Fri, 29 Apr 2011 23:07:54 +1000 (EST) Received: from adelie.canonical.com (adelie.canonical.com [91.189.90.139]) by ozlabs.org (Postfix) with ESMTP id 7A9EB1007DB for ; Fri, 29 Apr 2011 23:07:52 +1000 (EST) Received: from youngberry.canonical.com ([91.189.89.112]) by adelie.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1QFnQD-0005l7-Iv; Fri, 29 Apr 2011 13:07:49 +0000 Received: from [189.25.78.82] (helo=feioso) by youngberry.canonical.com with esmtpsa (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1QFnQC-0001qh-Op; Fri, 29 Apr 2011 13:07:49 +0000 Received: from localhost6.localdomain6 (localhost.localdomain [127.0.0.1]) by feioso (Postfix) with ESMTP id C76F840356; Fri, 29 Apr 2011 10:07:45 -0300 (BRT) Subject: [PATCH V3] New factory class to create arbitrary model objects, to be used in tests To: patchwork@lists.ozlabs.org From: Guilherme Salgado Date: Fri, 29 Apr 2011 10:07:45 -0300 Message-ID: <20110429130721.5454.68349.stgit@localhost6.localdomain6> User-Agent: StGit/0.15 MIME-Version: 1.0 Cc: patches@linaro.org 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: Guilherme Salgado --- apps/patchwork/tests/factory.py | 122 +++++++++++++++++++++++++++++++++++++++ 1 files changed, 122 insertions(+), 0 deletions(-) create mode 100644 apps/patchwork/tests/factory.py diff --git a/apps/patchwork/tests/factory.py b/apps/patchwork/tests/factory.py new file mode 100644 index 0000000..1eed452 --- /dev/null +++ b/apps/patchwork/tests/factory.py @@ -0,0 +1,122 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2011 Guilherme Salgado +# +# 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 + +from datetime import datetime +import email +from itertools import count +import textwrap + +from django.contrib.auth.models import User + +from patchwork.models import Bundle, Patch, Person, Project + + +class ObjectFactory(object): + """Factory methods for creating basic Python objects.""" + + def __init__(self): + # Initialise the unique identifier. + self.integer = count() + + def getUniqueEmailAddress(self): + return "%s@example.com" % self.getUniqueString('email') + + def getUniqueString(self, prefix = None): + """Return a string unique to this factory instance. + + :param prefix: Used as a prefix for the unique string. If unspecified, + defaults to 'generic-string'. + """ + if prefix is None: + prefix = "generic-string" + string = "%s%s" % (prefix, self.getUniqueInteger()) + return string.lower() + + def getUniqueInteger(self): + """Return an integer unique to this factory instance.""" + return self.integer.next() + + def makeUser(self): + userid = password = self.getUniqueString() + user = User.objects.create_user(userid, self.getUniqueEmailAddress(), + password) + user.save() + return user + + def makeProject(self): + name = self.getUniqueString() + project = Project(linkname = name, name = name, + listid = self.getUniqueString(), + listemail = self.getUniqueEmailAddress()) + project.save() + return project + + def makePerson(self, is_user = True): + person = Person(email = self.getUniqueEmailAddress(), + name = self.getUniqueString()) + person.save() + if is_user: + # Must create the user after the person is created or else + # create_user() will trigger the creation of a person. + user = User.objects.create_user(person.name, person.email, + password = None) + user.save() + # Re-fetch the person object so that our callsite sees the link to the + # newly created user. + person = Person.objects.get(email = person.email) + return person + + def makeBundle(self, patches = None): + if patches is None: + patches = [self.makePatch()] + bundle = Bundle(owner = self.makeUser(), project = self.makeProject(), + name = self.getUniqueString()) + bundle.save() + for patch in patches: + bundle.append_patch(patch) + bundle.save() + return bundle + + def makePatch(self, project = None, submitter = None, date = None, + content = None): + if date is None: + date = datetime.now() + if project is None: + project = self.makeProject() + if submitter is None: + submitter = self.makePerson() + if content is None: + content = textwrap.dedent("""\ + --- a/apps/patchwork/bin/parsemail.py + +++ b/apps/patchwork/bin/parsemail.py + @@ -183,4 +183,3 @@ def find_content(project, mail): + patch = None + comment = None + - if patchbuf: + - mail_headers(mail) + """) + msgid = email.utils.make_msgid(idstring = self.getUniqueString()) + patch = Patch(project = project, msgid = msgid, + name = self.getUniqueString(), submitter = submitter, + date = date, content = content) + patch.save() + return patch + + +factory = ObjectFactory()