[PATCH 03/25] tests: Add additional 'create_' helpers

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


Add some additional object creation helpers, as a way to help
eventually remove the 'default' class.

Signed-off-by: Stephen Finucane <stephen.finucane at intel.com>
---
 patchwork/tests/utils.py |  209 ++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 175 insertions(+), 34 deletions(-)

diff --git a/patchwork/tests/utils.py b/patchwork/tests/utils.py
index 8bd0288..e3ae39d 100644
--- a/patchwork/tests/utils.py
+++ b/patchwork/tests/utils.py
@@ -26,6 +26,8 @@ import os
 
 from django.contrib.auth.models import User
 
+from patchwork.models import Bundle
+from patchwork.models import Comment
 from patchwork.models import CoverLetter
 from patchwork.models import Patch
 from patchwork.models import Person
@@ -36,6 +38,14 @@ from patchwork.models import Project
 _test_mail_dir = os.path.join(os.path.dirname(__file__), 'mail')
 _test_patch_dir = os.path.join(os.path.dirname(__file__), 'patches')
 
+SAMPLE_DIFF = """--- /dev/null	2011-01-01 00:00:00.000000000 +0800
++++ a	2011-01-01 00:00:00.000000000 +0800
+@@ -0,0 +1 @@
++a
+"""
+
+SAMPLE_CONTENT = 'Hello, world.'
+
 
 class defaults(object):
     project = Project(linkname='test-project', name='Test Project',
@@ -64,24 +74,63 @@ error_strings = {
 }
 
 
-def create_user():
-    _user_id = User.objects.count()
+def create_project(**kwargs):
+    """Create a 'Project' object."""
+    num = Project.objects.count()
 
-    username = 'test%d' % _user_id
-    email = '%s at example.com' % username
+    values = {
+        'linkname': 'test-project-%d' % num,
+        'name': 'Test Project %d' % num,
+        'listid': 'test%d.example.com' % num,
+    }
+    values.update(kwargs)
+
+    project = Project(**values)
+    project.save()
+
+    return project
 
-    user = User.objects.create_user(username, email, username)
-    user.save()
 
-    person = Person(email=email, name=username, user=user)
+def create_person(**kwargs):
+    """Create a 'Person' object."""
+    num = Person.objects.count()
+
+    values = {
+        'email': 'test_person_%d at example.com' % num,
+        'name': 'test_person_%d' % num,
+        'user': None,
+    }
+    values.update(kwargs)
+
+    person = Person(**values)
     person.save()
 
+    return person
+
+
+def create_user(**kwargs):
+    """Create a 'User' object."""
+    num = User.objects.count()
+
+    values = {
+        'name': 'test_user_%d' % num,
+        'email': 'test_user_%d at example.com' % num,
+    }
+    values.update(kwargs)
+
+    user = User.objects.create_user(values['name'], values['email'],
+                                    values['name'])
+    user.save()
+
+    create_person(user=user, **values)
+
     return user
 
 
-def create_maintainer(project):
+def create_maintainer(project, **kwargs):
     """Create a 'User' and set as maintainer for provided project."""
-    user = create_user()
+    user = create_user(**kwargs)
+
     profile = user.profile
     profile.maintainer_projects.add(project)
     profile.save()
@@ -89,41 +138,133 @@ def create_maintainer(project):
     return user
 
 
-def create_patches(count=1):
-    """Create 'count' unique patches."""
-    defaults.project.save()
-    defaults.patch_author_person.save()
+def create_bundle(**kwargs):
+    """Create 'Bundle' object."""
+    num = Bundle.objects.count()
 
-    patches = []
+    values = {
+        'owner': create_user(),
+        'project': create_project(),
+        'name': 'test_bundle_%d' % num,
+    }
+    values.update(kwargs)
 
-    for i in range(0, count):
-        patch = Patch(project=defaults.project,
-                      submitter=defaults.patch_author_person,
-                      msgid=make_msgid(),
-                      name='testpatch%d' % (i + 1),
-                      diff=defaults.patch)
-        patch.save()
-        patches.append(patch)
+    bundle = Bundle(**values)
+    bundle.save()
+
+    return bundle
 
-    return patches
 
+def create_patch(**kwargs):
+    """Create 'Patch' object."""
+    num = Patch.objects.count()
 
-def create_covers(count=1):
-    """Create 'count' unique cover letters."""
+    values = {
+        'submitter': create_person(),
+        'delegate': None,
+        'project': create_project(),
+        'msgid': make_msgid(),
+        'name': 'testpatch%d' % num,
+        'headers': '',
+        'content': '',
+        'diff': SAMPLE_DIFF,
+    }
+    values.update(kwargs)
+
+    patch = Patch(**values)
+    patch.save()
+
+    return patch
+
+
+def create_cover(**kwargs):
+    """Create 'CoverLetter' object."""
+    num = CoverLetter.objects.count()
+
+    values = {
+        'submitter': create_person(),
+        'project': create_project(),
+        'msgid': make_msgid(),
+        'name': 'testpatch%d' % num,
+        'headers': '',
+        'content': '',
+    }
+    values.update(kwargs)
+
+    cover = CoverLetter(**values)
+    cover.save()
+
+    return cover
+
+
+def create_comment(**kwargs):
+    """Create 'Comment' object."""
+    values = {
+        'submitter': create_person(),
+        'submission': create_patch(),
+        'msgid': make_msgid(),
+        'content': SAMPLE_CONTENT,
+    }
+    values.update(kwargs)
+
+    comment = Comment(**values)
+    comment.save()
+
+    return comment
+
+
+def _create_submissions(create_func, count=1, **kwargs):
+    """Create 'count' Submission-based objects.
+
+    Args:
+        count (int): Number of patches to create
+        kwargs (dict): Overrides for various patch fields
+    """
     defaults.project.save()
     defaults.patch_author_person.save()
 
-    covers = []
+    values = {
+        'project': defaults.project,
+        'submitter': defaults.patch_author_person,
+    }
+    values.update(kwargs)
 
+    objects = []
     for i in range(0, count):
-        cover = CoverLetter(project=defaults.project,
-                            submitter=defaults.patch_author_person,
-                            msgid=make_msgid(),
-                            name='testcover%d' % (i + 1))
-        cover.save()
-        covers.append(cover)
-
-    return covers
+        obj = create_func(**values)
+        objects.append(obj)
+
+    return objects
+
+
+def create_patches(count=1, **kwargs):
+    """Create 'count' unique patches.
+
+    This differs from 'create_patch', in that it will ensure all
+    patches have at least the same project and submitter. In addition,
+    it is possible to set other fields to the same value, by passing
+    them as kwargs.
+
+    Args:
+        count (int): Number of patches to create
+        kwargs (dict): Overrides for various patch fields
+    """
+    return _create_submissions(create_patch, count, **kwargs)
+
+
+def create_covers(count=1, **kwargs):
+    """Create 'count' unique cover letters.
+
+    This differs from 'create_cover', in that it will ensure all cover
+    letters have at least the same project and submitter. In addition,
+    it is possible to set other fields to the same value, by passing
+    them as kwargs.
+
+    Args:
+        count (int): Number of cover letters to create
+        kwargs (dict): Overrides for various cover letter fields
+    """
+    return _create_submissions(create_cover, count, **kwargs)
 
 
 def read_patch(filename, encoding=None):
-- 
1.7.4.1



More information about the Patchwork mailing list