summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-03-11 22:07:23 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-03-11 22:11:35 +0100
commitba1fd987dad9268a1e5a41fe10513727aadfd9b5 (patch)
tree742dde52873162fd1a8fb213e1c37e2f14a9aa17
parent91592efbdff6de4b21b3a0b1a4d86dff8818f580 (diff)
downloadfactory-boy-ba1fd987dad9268a1e5a41fe10513727aadfd9b5.tar
factory-boy-ba1fd987dad9268a1e5a41fe10513727aadfd9b5.tar.gz
Add FACTORY_CLASS kwarg to make_factory and friends.
-rw-r--r--docs/changelog.rst9
-rw-r--r--docs/reference.rst44
-rw-r--r--factory/base.py4
3 files changed, 41 insertions, 16 deletions
diff --git a/docs/changelog.rst b/docs/changelog.rst
index 773efac..1a82ecd 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -1,19 +1,18 @@
ChangeLog
=========
-2.0.0 (future)
---------------
+2.0.0 (current)
+---------------
.. note:: This section lists features planned for v2 of factory_boy. Changes announced here may not have been committed to the repository.
*New:*
+ - Allow overriding the base factory class for :func:`~factory.make_factory` and friends.
- Add support for Python3
- - Clean up documentation
- - Document extension points
- Add support for ``get_or_create`` in :class:`~factory.DjangoModelFactory`
-*Deprecation:*
+*Removed:*
- Remove associated class discovery
- Stop defaulting to Django's ``Foo.objects.create()`` when "creating" instances
diff --git a/docs/reference.rst b/docs/reference.rst
index 70ca38a..0bb0d74 100644
--- a/docs/reference.rst
+++ b/docs/reference.rst
@@ -1219,6 +1219,25 @@ Lightweight factory declaration
login = 'john'
email = factory.LazyAttribute(lambda u: '%s@example.com' % u.login)
+ An alternate base class to :class:`Factory` can be specified in the
+ ``FACTORY_CLASS`` argument:
+
+ .. code-block:: python
+
+ UserFactory = make_factory(models.User,
+ login='john',
+ email=factory.LazyAttribute(lambda u: '%s@example.com' % u.login),
+ FACTORY_CLASS=factory.DjangoModelFactory,
+ )
+
+ # This is equivalent to:
+
+ class UserFactory(factory.DjangoModelFactory):
+ FACTORY_FOR = models.User
+
+ login = 'john'
+ email = factory.LazyAttribute(lambda u: '%s@example.com' % u.login)
+
Instance building
"""""""""""""""""
@@ -1226,8 +1245,8 @@ Instance building
The :mod:`factory` module provides a bunch of shortcuts for creating a factory and
extracting instances from them:
-.. function:: build(klass, **kwargs)
-.. function:: build_batch(klass, size, **kwargs)
+.. function:: build(klass, FACTORY_CLASS=None, **kwargs)
+.. function:: build_batch(klass, size, FACTORY_CLASS=None, **kwargs)
Create a factory for :obj:`klass` using declarations passed in kwargs;
return an instance built from that factory,
@@ -1236,11 +1255,12 @@ extracting instances from them:
:param class klass: Class of the instance to build
:param int size: Number of instances to build
:param kwargs: Declarations to use for the generated factory
+ :param FACTORY_CLASS: Alternate base class (instead of :class:`Factory`)
-.. function:: create(klass, **kwargs)
-.. function:: create_batch(klass, size, **kwargs)
+.. function:: create(klass, FACTORY_CLASS=None, **kwargs)
+.. function:: create_batch(klass, size, FACTORY_CLASS=None, **kwargs)
Create a factory for :obj:`klass` using declarations passed in kwargs;
return an instance created from that factory,
@@ -1249,11 +1269,12 @@ extracting instances from them:
:param class klass: Class of the instance to create
:param int size: Number of instances to create
:param kwargs: Declarations to use for the generated factory
+ :param FACTORY_CLASS: Alternate base class (instead of :class:`Factory`)
-.. function:: stub(klass, **kwargs)
-.. function:: stub_batch(klass, size, **kwargs)
+.. function:: stub(klass, FACTORY_CLASS=None, **kwargs)
+.. function:: stub_batch(klass, size, FACTORY_CLASS=None, **kwargs)
Create a factory for :obj:`klass` using declarations passed in kwargs;
return an instance stubbed from that factory,
@@ -1262,11 +1283,12 @@ extracting instances from them:
:param class klass: Class of the instance to stub
:param int size: Number of instances to stub
:param kwargs: Declarations to use for the generated factory
+ :param FACTORY_CLASS: Alternate base class (instead of :class:`Factory`)
-.. function:: generate(klass, strategy, **kwargs)
-.. function:: generate_batch(klass, strategy, size, **kwargs)
+.. function:: generate(klass, strategy, FACTORY_CLASS=None, **kwargs)
+.. function:: generate_batch(klass, strategy, size, FACTORY_CLASS=None, **kwargs)
Create a factory for :obj:`klass` using declarations passed in kwargs;
return an instance generated from that factory with the :obj:`strategy` strategy,
@@ -1276,11 +1298,12 @@ extracting instances from them:
:param str strategy: The strategy to use
:param int size: Number of instances to generate
:param kwargs: Declarations to use for the generated factory
+ :param FACTORY_CLASS: Alternate base class (instead of :class:`Factory`)
-.. function:: simple_generate(klass, create, **kwargs)
-.. function:: simple_generate_batch(klass, create, size, **kwargs)
+.. function:: simple_generate(klass, create, FACTORY_CLASS=None, **kwargs)
+.. function:: simple_generate_batch(klass, create, size, FACTORY_CLASS=None, **kwargs)
Create a factory for :obj:`klass` using declarations passed in kwargs;
return an instance generated from that factory according to the :obj:`create` flag,
@@ -1290,5 +1313,6 @@ extracting instances from them:
:param bool create: Whether to build (``False``) or create (``True``) instances
:param int size: Number of instances to generate
:param kwargs: Declarations to use for the generated factory
+ :param FACTORY_CLASS: Alternate base class (instead of :class:`Factory`)
diff --git a/factory/base.py b/factory/base.py
index e798c68..a6ab98e 100644
--- a/factory/base.py
+++ b/factory/base.py
@@ -705,7 +705,9 @@ def make_factory(klass, **kwargs):
"""Create a new, simple factory for the given class."""
factory_name = '%sFactory' % klass.__name__
kwargs[FACTORY_CLASS_DECLARATION] = klass
- factory_class = type(Factory).__new__(type(Factory), factory_name, (Factory,), kwargs)
+ base_class = kwargs.pop('FACTORY_CLASS', Factory)
+
+ factory_class = type(Factory).__new__(type(Factory), factory_name, (base_class,), kwargs)
factory_class.__name__ = '%sFactory' % klass.__name__
factory_class.__doc__ = 'Auto-generated factory for class %s' % klass
return factory_class