summaryrefslogtreecommitdiff
path: root/factory/base.py
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2011-12-21 00:01:40 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2011-12-21 00:01:40 +0100
commit776674da04856635518d41de5375cea04f24d3d7 (patch)
tree9b56fa253f49e5be814fbe04bfd1a5bfd131d0cd /factory/base.py
parent7897776dd1c6f8129e283fc6b21161121bc32dd9 (diff)
downloadfactory-boy-776674da04856635518d41de5375cea04f24d3d7.tar
factory-boy-776674da04856635518d41de5375cea04f24d3d7.tar.gz
Allow custom 'build' functions.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
Diffstat (limited to 'factory/base.py')
-rw-r--r--factory/base.py55
1 files changed, 53 insertions, 2 deletions
diff --git a/factory/base.py b/factory/base.py
index f25fd5c..8ae166e 100644
--- a/factory/base.py
+++ b/factory/base.py
@@ -34,6 +34,12 @@ STUB_STRATEGY = 'stub'
DJANGO_CREATION = lambda class_to_create, **kwargs: class_to_create.objects.create(**kwargs)
+# Building functions. Use Factory.set_building_function() to set a building functions appropriate for your ORM.
+
+NAIVE_BUILD = lambda class_to_build, **kwargs: class_to_build(**kwargs)
+MOGO_BUILD = lambda class_to_build, **kwargs: class_to_build.new(**kwargs)
+
+
# Special declarations
FACTORY_CLASS_DECLARATION = 'FACTORY_FOR'
@@ -214,16 +220,61 @@ class Factory(BaseFactory):
default_strategy = CREATE_STRATEGY
- class AssociatedClassError(RuntimeError): pass
+ class AssociatedClassError(RuntimeError):
+ pass
+ # Customizing 'create' strategy
_creation_function = (DJANGO_CREATION,) # Using a tuple to keep the creation function from turning into an instance method
+
@classmethod
def set_creation_function(cls, creation_function):
+ """Set the creation function for this class.
+
+ Args:
+ creation_function (function): the new creation function. That
+ function should take one non-keyword argument, the 'class' for
+ which an instance will be created. The value of the various
+ fields are passed as keyword arguments.
+ """
cls._creation_function = (creation_function,)
+
@classmethod
def get_creation_function(cls):
+ """Retrieve the creation function for this class.
+
+ Returns:
+ function: A function that takes one parameter, the class for which
+ an instance will be created, and keyword arguments for the value
+ of the fields of the instance.
+ """
return cls._creation_function[0]
+ # Customizing 'build' strategy
+ _building_function = (NAIVE_BUILD,)
+
+ @classmethod
+ def set_building_function(cls, building_function):
+ """Set the building function for this class.
+
+ Args:
+ building_function (function): the new building function. That
+ function should take one non-keyword argument, the 'class' for
+ which an instance will be built. The value of the various
+ fields are passed as keyword arguments.
+ """
+ cls._building_function = (building_function,)
+
+ @classmethod
+ def get_building_function(cls):
+ """Retrieve the building function for this class.
+
+ Returns:
+ function: A function that takes one parameter, the class for which
+ an instance will be created, and keyword arguments for the value
+ of the fields of the instance.
+ """
+ return cls._building_function[0]
+
@classmethod
def _prepare(cls, create, **kwargs):
"""Prepare an object for this factory.
@@ -235,7 +286,7 @@ class Factory(BaseFactory):
if create:
return cls.get_creation_function()(getattr(cls, CLASS_ATTRIBUTE_ASSOCIATED_CLASS), **kwargs)
else:
- return getattr(cls, CLASS_ATTRIBUTE_ASSOCIATED_CLASS)(**kwargs)
+ return cls.get_building_function()(getattr(cls, CLASS_ATTRIBUTE_ASSOCIATED_CLASS), **kwargs)
@classmethod
def _build(cls, **kwargs):