Created
May 24, 2013 08:58
-
-
Save senny/5642247 to your computer and use it in GitHub Desktop.
A Builder Pattern implementation to create test data objects in Plone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from plone.dexterity.utils import createContentInContainer | |
from plone.namedfile.file import NamedBlobFile | |
import transaction | |
def Builder(name): | |
if name == "dossier": | |
return DossierBuilder(BuilderSession.instance()) | |
elif name == "document": | |
return DocumentBuilder(BuilderSession.instance()) | |
elif name == "task": | |
return TaskBuilder(BuilderSession.instance()) | |
elif name == "mail": | |
return MailBuilder(BuilderSession.instance()) | |
elif name == "repository": | |
return RepositoryBuilder(BuilderSession.instance()) | |
else: | |
raise ValueError("No Builder for %s" % name) | |
class BuilderSession(object): | |
def __init__(self): | |
self.reset() | |
def reset(self): | |
self.portal = None | |
self.auto_commit = True | |
@classmethod | |
def instance(cls, *args, **kwgs): | |
if not hasattr(cls, "_instance"): | |
cls._instance = cls(*args, **kwgs) | |
return cls._instance | |
class DexterityBuilder(object): | |
def __init__(self, session): | |
self.session = session | |
self.container = session.portal | |
self.arguments = {"checkConstraints": False} | |
def within(self, container): | |
self.container = container | |
return self | |
def titled(self, title): | |
self.arguments["title"] = title | |
return self | |
def with_description(self, description): | |
self.arguments["description"] = description | |
return self | |
def having(self, **kwargs): | |
self.arguments.update(kwargs) | |
return self | |
def create(self): | |
self.before_create() | |
obj = self.create_object() | |
self.after_create() | |
return obj | |
def before_create(self): | |
pass | |
def after_create(self): | |
if self.session.auto_commit: | |
transaction.commit() | |
class DossierBuilder(DexterityBuilder): | |
def create_object(self): | |
return createContentInContainer(self.container, | |
'opengever.dossier.businesscasedossier', | |
**self.arguments) | |
class DocumentBuilder(DexterityBuilder): | |
def with_dummy_content(self): | |
self.attach_file_containing("Test data") | |
return self | |
def attach_file_containing(self, content, name=u"test.doc"): | |
self.attach(NamedBlobFile(data=content, filename=name)) | |
return self | |
def attach(self, file_): | |
self.arguments["file"] = file_ | |
return self | |
def create_object(self): | |
return createContentInContainer(self.container, | |
'opengever.document.document', | |
**self.arguments) | |
class TaskBuilder(DexterityBuilder): | |
def create_object(self): | |
return createContentInContainer(self.container, | |
'opengever.task.task', | |
**self.arguments) | |
class MailBuilder(DexterityBuilder): | |
def with_dummy_message(self): | |
self.with_message("foobar") | |
return self | |
def with_message(self, message, filename=u'testmail.eml'): | |
file_ = NamedBlobFile(data=message, filename=filename) | |
self.arguments["message"] = file_ | |
return self | |
def create_object(self): | |
return createContentInContainer(self.container, | |
'ftw.mail.mail', | |
**self.arguments) | |
class RepositoryBuilder(DexterityBuilder): | |
def create_object(self): | |
return createContentInContainer(self.container, | |
'opengever.repository.repositoryfolder', | |
**self.arguments) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment