summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorThomas Goirand <thomas@goirand.fr>2014-07-03 15:13:48 +0800
committerThomas Goirand <thomas@goirand.fr>2014-07-03 15:13:48 +0800
commit45918ff19633304a0e2a4d389a958bee46c03eff (patch)
tree8fb81a4a9bb21ef113fa14eeaec41ee8ceb1c1cb /docs
parent80628cd9bde3d8ffc989541a9171a569a5101dc0 (diff)
parent87f8cc0cc0d2f48f489c81b8c93e8ab6de6cff26 (diff)
downloadfactory-boy-45918ff19633304a0e2a4d389a958bee46c03eff.tar
factory-boy-45918ff19633304a0e2a4d389a958bee46c03eff.tar.gz
Merge tag '2.4.1' into debian/unstable
Release of factory_boy 2.4.1
Diffstat (limited to 'docs')
-rw-r--r--docs/changelog.rst44
-rw-r--r--docs/examples.rst18
-rw-r--r--docs/fuzzy.rst40
-rw-r--r--docs/ideas.rst3
-rw-r--r--docs/introduction.rst41
-rw-r--r--docs/orms.rst97
-rw-r--r--docs/recipes.rst62
-rw-r--r--docs/reference.rst243
8 files changed, 428 insertions, 120 deletions
diff --git a/docs/changelog.rst b/docs/changelog.rst
index 4917578..7d77f7f 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -2,6 +2,50 @@ ChangeLog
=========
+.. _v2.4.1:
+
+2.4.1 (2014-06-23)
+------------------
+
+*Bugfix:*
+
+ - Fix overriding deeply inherited attributes (set in one factory, overridden in a subclass, used in a sub-sub-class).
+
+.. _v2.4.0:
+
+2.4.0 (2014-06-21)
+------------------
+
+*New:*
+
+ - Add support for :attr:`factory.fuzzy.FuzzyInteger.step`, thanks to `ilya-pirogov <https://github.com/ilya-pirogov>`_ (:issue:`120`)
+ - Add :meth:`~factory.django.mute_signals` decorator to temporarily disable some signals, thanks to `ilya-pirogov <https://github.com>`_ (:issue:`122`)
+ - Add :class:`~factory.fuzzy.FuzzyFloat` (:issue:`124`)
+ - Declare target model and other non-declaration fields in a ``class Meta`` section.
+
+*Deprecation:*
+
+ - Use of ``FACTORY_FOR`` and other ``FACTORY`` class-level attributes is deprecated and will be removed in 2.5.
+ Those attributes should now declared within the :class:`class Meta <factory.FactoryOptions>` attribute:
+
+ For :class:`factory.Factory`:
+
+ * Rename :attr:`~factory.Factory.FACTORY_FOR` to :attr:`~factory.FactoryOptions.model`
+
+ * Rename :attr:`~factory.Factory.FACTORY_FOR` to :attr:`~factory.FactoryOptions.model`
+ * Rename :attr:`~factory.Factory.ABSTRACT_FACTORY` to :attr:`~factory.FactoryOptions.abstract`
+ * Rename :attr:`~factory.Factory.FACTORY_STRATEGY` to :attr:`~factory.FactoryOptions.strategy`
+ * Rename :attr:`~factory.Factory.FACTORY_ARG_PARAMETERS` to :attr:`~factory.FactoryOptions.inline_args`
+ * Rename :attr:`~factory.Factory.FACTORY_HIDDEN_ARGS` to :attr:`~factory.FactoryOptions.exclude`
+
+ For :class:`factory.django.DjangoModelFactory`:
+
+ * Rename :attr:`~factory.django.DjangoModelFactory.FACTORY_DJANGO_GET_OR_CREATE` to :attr:`~factory.django.DjangoOptions.django_get_or_create`
+
+ For :class:`factory.alchemy.SQLAlchemyModelFactory`:
+
+ * Rename :attr:`~factory.alchemy.SQLAlchemyModelFactory.FACTORY_SESSION` to :attr:`~factory.alchemy.SQLAlchemyOptions.sqlalchemy_session`
+
.. _v2.3.1:
2.3.1 (2014-01-22)
diff --git a/docs/examples.rst b/docs/examples.rst
index aab990a..ee521e3 100644
--- a/docs/examples.rst
+++ b/docs/examples.rst
@@ -56,14 +56,16 @@ And now, we'll define the related factories:
class AccountFactory(factory.Factory):
- FACTORY_FOR = objects.Account
+ class Meta:
+ model = objects.Account
username = factory.Sequence(lambda n: 'john%s' % n)
email = factory.LazyAttribute(lambda o: '%s@example.org' % o.username)
class ProfileFactory(factory.Factory):
- FACTORY_FOR = objects.Profile
+ class Meta:
+ model = objects.Profile
account = factory.SubFactory(AccountFactory)
gender = factory.Iterator([objects.Profile.GENDER_MALE, objects.Profile.GENDER_FEMALE])
@@ -112,12 +114,9 @@ We can now use our factories, for tests:
def test_get_profile_stats(self):
profiles = []
- for _ in xrange(4):
- profiles.append(factories.ProfileFactory())
- for _ in xrange(2):
- profiles.append(factories.FemaleProfileFactory())
- for _ in xrange(2):
- profiles.append(factories.ProfileFactory(planet='Tatooine'))
+ profiles.extend(factories.ProfileFactory.batch_create(4))
+ profiles.extend(factories.FemaleProfileFactory.batch_create(2))
+ profiles.extend(factories.ProfileFactory.batch_create(2, planet="Tatooine"))
stats = business_logic.profile_stats(profiles)
self.assertEqual({'Earth': 6, 'Mars': 2}, stats.planets)
@@ -131,8 +130,7 @@ Or for fixtures:
from . import factories
def make_objects():
- for _ in xrange(50):
- factories.ProfileFactory()
+ factories.ProfileFactory.batch_create(size=50)
# Let's create a few, known objects.
factories.ProfileFactory(
diff --git a/docs/fuzzy.rst b/docs/fuzzy.rst
index b94dfa5..1480419 100644
--- a/docs/fuzzy.rst
+++ b/docs/fuzzy.rst
@@ -73,7 +73,7 @@ FuzzyChoice
FuzzyInteger
------------
-.. class:: FuzzyInteger(low[, high])
+.. class:: FuzzyInteger(low[, high[, step]])
The :class:`FuzzyInteger` fuzzer generates random integers within a given
inclusive range.
@@ -82,7 +82,7 @@ FuzzyInteger
.. code-block:: pycon
- >>> FuzzyInteger(0, 42)
+ >>> fi = FuzzyInteger(0, 42)
>>> fi.low, fi.high
0, 42
@@ -98,12 +98,18 @@ FuzzyInteger
int, the inclusive higher bound of generated integers
+ .. attribute:: step
+
+ int, the step between values in the range; for instance, a ``FuzzyInteger(0, 42, step=3)``
+ might only yield values from ``[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42]``.
+
+
FuzzyDecimal
------------
-.. class:: FuzzyDecimal(low[, high])
+.. class:: FuzzyDecimal(low[, high[, precision=2]])
- The :class:`FuzzyDecimal` fuzzer generates random integers within a given
+ The :class:`FuzzyDecimal` fuzzer generates random :class:`decimals <decimal.Decimal>` within a given
inclusive range.
The :attr:`low` bound may be omitted, in which case it defaults to 0:
@@ -134,6 +140,32 @@ FuzzyDecimal
int, the number of digits to generate after the dot. The default is 2 digits.
+FuzzyFloat
+----------
+
+.. class:: FuzzyFloat(low[, high])
+
+ The :class:`FuzzyFloat` fuzzer provides random :class:`float` objects within a given inclusive range.
+
+ .. code-block:: pycon
+
+ >>> FuzzyFloat(0.5, 42.7)
+ >>> fi.low, fi.high
+ 0.5, 42.7
+
+ >>> fi = FuzzyFloat(42.7)
+ >>> fi.low, fi.high
+ 0.0, 42.7
+
+
+ .. attribute:: low
+
+ decimal, the inclusive lower bound of generated floats
+
+ .. attribute:: high
+
+ decimal, the inclusive higher bound of generated floats
+
FuzzyDate
---------
diff --git a/docs/ideas.rst b/docs/ideas.rst
index 914e640..f3c9e62 100644
--- a/docs/ideas.rst
+++ b/docs/ideas.rst
@@ -4,5 +4,6 @@ Ideas
This is a list of future features that may be incorporated into factory_boy:
-* **A 'options' attribute**: instead of adding more class-level constants, use a django-style ``class Meta`` Factory attribute with all options there
+* When a :class:`Factory` is built or created, pass the calling context throughout the calling chain instead of custom solutions everywhere
+* Define a proper set of rules for the support of third-party ORMs
diff --git a/docs/introduction.rst b/docs/introduction.rst
index 86e2046..d00154d 100644
--- a/docs/introduction.rst
+++ b/docs/introduction.rst
@@ -18,10 +18,11 @@ Basic usage
-----------
-Factories declare a set of attributes used to instantiate an object, whose class is defined in the FACTORY_FOR attribute:
+Factories declare a set of attributes used to instantiate an object, whose class is defined in the ``class Meta``'s ``model`` attribute:
- Subclass ``factory.Factory`` (or a more suitable subclass)
-- Set its ``FACTORY_FOR`` attribute to the target class
+- Add a ``class Meta:`` block
+- Set its ``model`` attribute to the target class
- Add defaults for keyword args to pass to the associated class' ``__init__`` method
@@ -31,7 +32,8 @@ Factories declare a set of attributes used to instantiate an object, whose class
from . import base
class UserFactory(factory.Factory):
- FACTORY_FOR = base.User
+ class Meta:
+ model = base.User
firstname = "John"
lastname = "Doe"
@@ -56,7 +58,8 @@ A given class may be associated to many :class:`~factory.Factory` subclasses:
.. code-block:: python
class EnglishUserFactory(factory.Factory):
- FACTORY_FOR = base.User
+ class Meta:
+ model = base.User
firstname = "John"
lastname = "Doe"
@@ -64,7 +67,8 @@ A given class may be associated to many :class:`~factory.Factory` subclasses:
class FrenchUserFactory(factory.Factory):
- FACTORY_FOR = base.User
+ class Meta:
+ model = base.User
firstname = "Jean"
lastname = "Dupont"
@@ -88,7 +92,8 @@ This is achieved with the :class:`~factory.Sequence` declaration:
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = models.User
+ class Meta:
+ model = models.User
username = factory.Sequence(lambda n: 'user%d' % n)
@@ -104,7 +109,8 @@ This is achieved with the :class:`~factory.Sequence` declaration:
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = models.User
+ class Meta:
+ model = models.User
@factory.sequence
def username(n):
@@ -121,7 +127,8 @@ taking the object being built and returning the value for the field:
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = models.User
+ class Meta:
+ model = models.User
username = factory.Sequence(lambda n: 'user%d' % n)
email = factory.LazyAttribute(lambda obj: '%s@example.com' % obj.username)
@@ -146,7 +153,8 @@ taking the object being built and returning the value for the field:
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = models.User
+ class Meta:
+ model = models.User
username = factory.Sequence(lambda n: 'user%d' % n)
@@ -168,7 +176,8 @@ and update them with its own declarations:
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = base.User
+ class Meta:
+ model = base.User
firstname = "John"
lastname = "Doe"
@@ -209,13 +218,14 @@ Non-kwarg arguments
Some classes take a few, non-kwarg arguments first.
-This is handled by the :data:`~factory.Factory.FACTORY_ARG_PARAMETERS` attribute:
+This is handled by the :data:`~factory.FactoryOptions.inline_args` attribute:
.. code-block:: python
class MyFactory(factory.Factory):
- FACTORY_FOR = MyClass
- FACTORY_ARG_PARAMETERS = ('x', 'y')
+ class Meta:
+ model = MyClass
+ inline_args = ('x', 'y')
x = 1
y = 2
@@ -251,7 +261,8 @@ Calling a :class:`~factory.Factory` subclass will provide an object through the
.. code-block:: python
class MyFactory(factory.Factory):
- FACTORY_FOR = MyClass
+ class Meta:
+ model = MyClass
.. code-block:: pycon
@@ -265,6 +276,6 @@ Calling a :class:`~factory.Factory` subclass will provide an object through the
<MyClass: X (saved)>
-The default strategy can be changed by setting the class-level :attr:`~factory.Factory.FACTORY_STRATEGY` attribute.
+The default strategy can be changed by setting the ``class Meta`` :attr:`~factory.FactoryOptions.strategy` attribute.
diff --git a/docs/orms.rst b/docs/orms.rst
index e50e706..2aa27b2 100644
--- a/docs/orms.rst
+++ b/docs/orms.rst
@@ -32,7 +32,7 @@ All factories for a Django :class:`~django.db.models.Model` should use the
This class provides the following features:
- * The :attr:`~factory.Factory.FACTORY_FOR` attribute also supports the ``'app.Model'``
+ * The :attr:`~factory.FactoryOptions.model` attribute also supports the ``'app.Model'``
syntax
* :func:`~factory.Factory.create()` uses :meth:`Model.objects.create() <django.db.models.query.QuerySet.create>`
* :func:`~factory.Factory._setup_next_sequence()` selects the next unused primary key value
@@ -42,6 +42,18 @@ All factories for a Django :class:`~django.db.models.Model` should use the
.. attribute:: FACTORY_DJANGO_GET_OR_CREATE
+ .. deprecated:: 2.4.0
+ See :attr:`DjangoOptions.django_get_or_create`.
+
+
+.. class:: DjangoOptions(factory.base.FactoryOptions)
+
+ The ``class Meta`` on a :class:`~DjangoModelFactory` supports an extra parameter:
+
+ .. attribute:: django_get_or_create
+
+ .. versionadded:: 2.4.0
+
Fields whose name are passed in this list will be used to perform a
:meth:`Model.objects.get_or_create() <django.db.models.query.QuerySet.get_or_create>`
instead of the usual :meth:`Model.objects.create() <django.db.models.query.QuerySet.create>`:
@@ -49,8 +61,9 @@ All factories for a Django :class:`~django.db.models.Model` should use the
.. code-block:: python
class UserFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = 'myapp.User' # Equivalent to ``FACTORY_FOR = myapp.models.User``
- FACTORY_DJANGO_GET_OR_CREATE = ('username',)
+ class Meta:
+ model = 'myapp.User' # Equivalent to ``model = myapp.models.User``
+ django_get_or_create = ('username',)
username = 'john'
@@ -80,15 +93,21 @@ All factories for a Django :class:`~django.db.models.Model` should use the
.. code-block:: python
class MyAbstractModelFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = models.MyAbstractModel
- ABSTRACT_FACTORY = True
+ class Meta:
+ model = models.MyAbstractModel
+ abstract = True
class MyConcreteModelFactory(MyAbstractModelFactory):
- FACTORY_FOR = models.MyConcreteModel
+ class Meta:
+ model = models.MyConcreteModel
Otherwise, factory_boy will try to get the 'next PK' counter from the abstract model.
+Extra fields
+""""""""""""
+
+
.. class:: FileField
Custom declarations for :class:`django.db.models.FileField`
@@ -108,7 +127,8 @@ All factories for a Django :class:`~django.db.models.Model` should use the
.. code-block:: python
class MyFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = models.MyModel
+ class Meta:
+ model = models.MyModel
the_file = factory.django.FileField(filename='the_file.dat')
@@ -145,7 +165,8 @@ All factories for a Django :class:`~django.db.models.Model` should use the
.. code-block:: python
class MyFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = models.MyModel
+ class Meta:
+ model = models.MyModel
the_image = factory.django.ImageField(color='blue')
@@ -157,6 +178,44 @@ All factories for a Django :class:`~django.db.models.Model` should use the
None
+Disabling signals
+"""""""""""""""""
+
+Signals are often used to plug some custom code into external components code;
+for instance to create ``Profile`` objects on-the-fly when a new ``User`` object is saved.
+
+This may interfere with finely tuned :class:`factories <DjangoModelFactory>`, which would
+create both using :class:`~factory.RelatedFactory`.
+
+To work around this problem, use the :meth:`mute_signals()` decorator/context manager:
+
+.. method:: mute_signals(signal1, ...)
+
+ Disable the list of selected signals when calling the factory, and reactivate them upon leaving.
+
+.. code-block:: python
+
+ # foo/factories.py
+
+ import factory
+ import factory.django
+
+ from . import models
+ from . import signals
+
+ @factory.django.mute_signals(signals.pre_save, signals.post_save)
+ class FooFactory(factory.django.DjangoModelFactory):
+ class Meta:
+ model = models.Foo
+
+ # ...
+
+ def make_chain():
+ with factory.django.mute_signals(signals.pre_save, signals.post_save):
+ # pre_save/post_save won't be called here.
+ return SomeFactory(), SomeOtherFactory()
+
+
Mogo
----
@@ -200,7 +259,7 @@ factory_boy supports `MongoEngine`_-style models, through the :class:`MongoEngin
* :func:`~factory.Factory.create()` builds an instance through ``__init__`` then
saves it.
- .. note:: If the :attr:`associated class <factory.Factory.FACTORY_FOR>` is a :class:`mongoengine.EmbeddedDocument`,
+ .. note:: If the :attr:`associated class <factory.FactoryOptions.model` is a :class:`mongoengine.EmbeddedDocument`,
the :meth:`~MongoEngineFactory.create` function won't "save" it, since this wouldn't make sense.
This feature makes it possible to use :class:`~factory.SubFactory` to create embedded document.
@@ -214,7 +273,7 @@ SQLAlchemy
Factoy_boy also supports `SQLAlchemy`_ models through the :class:`SQLAlchemyModelFactory` class.
-To work, this class needs an `SQLAlchemy`_ session object affected to "FACTORY_SESSION" class attribute.
+To work, this class needs an `SQLAlchemy`_ session object affected to the :attr:`Meta.sqlalchemy_session <SQLAlchemyOptions.sqlalchemy_session>` attribute.
.. _SQLAlchemy: http://www.sqlalchemy.org/
@@ -229,7 +288,18 @@ To work, this class needs an `SQLAlchemy`_ session object affected to "FACTORY_S
.. attribute:: FACTORY_SESSION
- Fields whose SQLAlchemy session object are passed will be used to communicate with the database
+ .. deprecated:: 2.4.0
+ See :attr:`~SQLAlchemyOptions.sqlalchemy_session`.
+
+.. class:: SQLAlchemyOptions(factory.base.FactoryOptions)
+
+ In addition to the usual parameters available in :class:`class Meta <factory.base.FactoryOptions>`,
+ a :class:`SQLAlchemyModelFactory` also supports the following settings:
+
+ .. attribute:: sqlalchemy_session
+
+ SQLAlchemy session to use to communicate with the database when creating
+ an object through this :class:`SQLAlchemyModelFactory`.
A (very) simple exemple:
@@ -256,8 +326,9 @@ A (very) simple exemple:
class UserFactory(SQLAlchemyModelFactory):
- FACTORY_FOR = User
- FACTORY_SESSION = session # the SQLAlchemy session object
+ class Meta:
+ model = User
+ sqlalchemy_session = session # the SQLAlchemy session object
id = factory.Sequence(lambda n: n)
name = factory.Sequence(lambda n: u'User %d' % n)
diff --git a/docs/recipes.rst b/docs/recipes.rst
index c1f3700..72dacef 100644
--- a/docs/recipes.rst
+++ b/docs/recipes.rst
@@ -26,7 +26,8 @@ use the :class:`~factory.SubFactory` declaration:
from . import models
class UserFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = models.User
+ class Meta:
+ model = models.User
first_name = factory.Sequence(lambda n: "Agent %03d" % n)
group = factory.SubFactory(GroupFactory)
@@ -53,7 +54,8 @@ use a :class:`~factory.RelatedFactory` declaration:
# factories.py
class UserFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = models.User
+ class Meta:
+ model = models.User
log = factory.RelatedFactory(UserLogFactory, 'user', action=models.UserLog.ACTION_CREATE)
@@ -66,7 +68,7 @@ Example: Django's Profile
"""""""""""""""""""""""""
Django (<1.5) provided a mechanism to attach a ``Profile`` to a ``User`` instance,
-using a :class:`~django.db.models.ForeignKey` from the ``Profile`` to the ``User``.
+using a :class:`~django.db.models.OneToOneField` from the ``Profile`` to the ``User``.
A typical way to create those profiles was to hook a post-save signal to the ``User`` model.
@@ -75,7 +77,8 @@ factory_boy allows to define attributes of such profiles dynamically when creati
.. code-block:: python
class ProfileFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = my_models.Profile
+ class Meta:
+ model = my_models.Profile
title = 'Dr'
# We pass in profile=None to prevent UserFactory from creating another profile
@@ -83,7 +86,8 @@ factory_boy allows to define attributes of such profiles dynamically when creati
user = factory.SubFactory('app.factories.UserFactory', profile=None)
class UserFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = auth_models.User
+ class Meta:
+ model = auth_models.User
username = factory.Sequence(lambda n: "user_%d" % n)
@@ -145,12 +149,14 @@ hook:
# factories.py
class GroupFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = models.Group
+ class Meta:
+ model = models.Group
name = factory.Sequence(lambda n: "Group #%s" % n)
class UserFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = models.User
+ class Meta:
+ model = models.User
name = "John Doe"
@@ -200,17 +206,20 @@ If more links are needed, simply add more :class:`RelatedFactory` declarations:
# factories.py
class UserFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = models.User
+ class Meta:
+ model = models.User
name = "John Doe"
class GroupFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = models.Group
+ class Meta:
+ model = models.Group
name = "Admins"
class GroupLevelFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = models.GroupLevel
+ class Meta:
+ model = models.GroupLevel
user = factory.SubFactory(UserFactory)
group = factory.SubFactory(GroupFactory)
@@ -273,21 +282,48 @@ Here, we want:
# factories.py
class CountryFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = models.Country
+ class Meta:
+ model = models.Country
name = factory.Iterator(["France", "Italy", "Spain"])
lang = factory.Iterator(['fr', 'it', 'es'])
class UserFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = models.User
+ class Meta:
+ model = models.User
name = "John"
lang = factory.SelfAttribute('country.lang')
country = factory.SubFactory(CountryFactory)
class CompanyFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = models.Company
+ class Meta:
+ model = models.Company
name = "ACME, Inc."
country = factory.SubFactory(CountryFactory)
owner = factory.SubFactory(UserFactory, country=factory.SelfAttribute('..country'))
+
+
+Custom manager methods
+----------------------
+
+Sometimes you need a factory to call a specific manager method other then the
+default :meth:`Model.objects.create() <django.db.models.query.QuerySet.create>` method:
+
+.. code-block:: python
+
+ class UserFactory(factory.DjangoModelFactory):
+ class Meta:
+ model = UserenaSignup
+
+ username = "l7d8s"
+ email = "my_name@example.com"
+ password = "my_password"
+
+ @classmethod
+ def _create(cls, model_class, *args, **kwargs):
+ """Override the default ``_create`` with our custom call."""
+ manager = cls._get_manager(model_class)
+ # The default would use ``manager.create(*args, **kwargs)``
+ return manager.create_user(*args, **kwargs)
diff --git a/docs/reference.rst b/docs/reference.rst
index 53584a0..b0dda50 100644
--- a/docs/reference.rst
+++ b/docs/reference.rst
@@ -11,37 +11,54 @@ For internals and customization points, please refer to the :doc:`internals` sec
The :class:`Factory` class
--------------------------
-.. class:: Factory
+.. class:: FactoryOptions
+
+ .. versionadded:: 2.4.0
+
+ A :class:`Factory`'s behaviour can be tuned through a few settings.
- The :class:`Factory` class is the base of factory_boy features.
+ For convenience, they are declared in a single ``class Meta`` attribute:
+
+ .. code-block:: python
- It accepts a few specific attributes (must be specified on class declaration):
+ class MyFactory(factory.Factory):
+ class Meta:
+ model = MyObject
+ abstract = False
- .. attribute:: FACTORY_FOR
+ .. attribute:: model
This optional attribute describes the class of objects to generate.
If unset, it will be inherited from parent :class:`Factory` subclasses.
- .. attribute:: ABSTRACT_FACTORY
+ .. versionadded:: 2.4.0
+
+ .. attribute:: abstract
This attribute indicates that the :class:`Factory` subclass should not
be used to generate objects, but instead provides some extra defaults.
It will be automatically set to ``True`` if neither the :class:`Factory`
- subclass nor its parents define the :attr:`~Factory.FACTORY_FOR` attribute.
+ subclass nor its parents define the :attr:`~FactoryOptions.model` attribute.
+
+ .. warning:: This flag is reset to ``False`` When a :class:`Factory` subclasses
+ another one if a :attr:`~FactoryOptions.model` is set.
+
+ .. versionadded:: 2.4.0
- .. attribute:: FACTORY_ARG_PARAMETERS
+ .. attribute:: inline_args
Some factories require non-keyword arguments to their :meth:`~object.__init__`.
- They should be listed, in order, in the :attr:`FACTORY_ARG_PARAMETERS`
+ They should be listed, in order, in the :attr:`inline_args`
attribute:
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
- FACTORY_ARG_PARAMETERS = ('login', 'email')
+ class Meta:
+ model = User
+ inline_args = ('login', 'email')
login = 'john'
email = factory.LazyAttribute(lambda o: '%s@example.com' % o.login)
@@ -53,22 +70,25 @@ The :class:`Factory` class
<User: john>
>>> User('john', 'john@example.com', firstname="John") # actual call
- .. attribute:: FACTORY_HIDDEN_ARGS
+ .. versionadded:: 2.4.0
+
+ .. attribute:: exclude
While writing a :class:`Factory` for some object, it may be useful to
have general fields helping defining others, but that should not be
- passed to the target class; for instance, a field named 'now' that would
+ passed to the model class; for instance, a field named 'now' that would
hold a reference time used by other objects.
- Factory fields whose name are listed in :attr:`FACTORY_HIDDEN_ARGS` will
+ Factory fields whose name are listed in :attr:`exclude` will
be removed from the set of args/kwargs passed to the underlying class;
they can be any valid factory_boy declaration:
.. code-block:: python
class OrderFactory(factory.Factory):
- FACTORY_FOR = Order
- FACTORY_HIDDEN_ARGS = ('now',)
+ class Meta:
+ model = Order
+ exclude = ('now',)
now = factory.LazyAttribute(lambda o: datetime.datetime.utcnow())
started_at = factory.LazyAttribute(lambda o: o.now - datetime.timedelta(hours=1))
@@ -83,6 +103,67 @@ The :class:`Factory` class
>>> OrderFactory(now=datetime.datetime(2013, 4, 1, 10))
<Order: started 2013-04-01 09:00:00, paid 2013-04-01 09:10:00>
+ .. versionadded:: 2.4.0
+
+
+ .. attribute:: strategy
+
+ Use this attribute to change the strategy used by a :class:`Factory`.
+ The default is :data:`BUILD_STRATEGY`.
+
+
+
+.. class:: Factory
+
+ .. note:: In previous versions, the fields of :class:`class Meta <factory.FactoryOptions>` were
+ defined as class attributes on :class:`Factory`. This is now deprecated and will be removed
+ in 2.5.0.
+
+ .. attribute:: FACTORY_FOR
+
+ .. deprecated:: 2.4.0
+ See :attr:`FactoryOptions.model`.
+
+ .. attribute:: ABSTRACT_FACTORY
+
+ .. deprecated:: 2.4.0
+ See :attr:`FactoryOptions.abstract`.
+
+ .. attribute:: FACTORY_ARG_PARAMETERS
+
+ .. deprecated:: 2.4.0
+ See :attr:`FactoryOptions.inline_args`.
+
+ .. attribute:: FACTORY_HIDDEN_ARGS
+
+ .. deprecated:: 2.4.0
+ See :attr:`FactoryOptions.exclude`.
+
+ .. attribute:: FACTORY_STRATEGY
+
+ .. deprecated:: 2.4.0
+ See :attr:`FactoryOptions.strategy`.
+
+
+ **Class-level attributes:**
+
+ .. attribute:: _meta
+
+ .. versionadded:: 2.4.0
+
+ The :class:`FactoryOptions` instance attached to a :class:`Factory` class is available
+ as a :attr:`_meta` attribute.
+
+ .. attribute:: _options_class
+
+ .. versionadded:: 2.4.0
+
+ If a :class:`Factory` subclass needs to define additional, extra options, it has to
+ provide a custom :class:`FactoryOptions` subclass.
+
+ A pointer to that custom class should be provided as :attr:`_options_class` so that
+ the :class:`Factory`-building metaclass can use it instead.
+
**Base functions:**
@@ -162,7 +243,7 @@ The :class:`Factory` class
The :meth:`_adjust_kwargs` extension point allows for late fields tuning.
It is called once keyword arguments have been resolved and post-generation
- items removed, but before the :attr:`FACTORY_ARG_PARAMETERS` extraction
+ items removed, but before the :attr:`~FactoryOptions.inline_args` extraction
phase.
.. code-block:: python
@@ -189,19 +270,19 @@ The :class:`Factory` class
Subclasses may fetch the next free ID from the database, for instance.
- .. classmethod:: _build(cls, target_class, *args, **kwargs)
+ .. classmethod:: _build(cls, model_class, *args, **kwargs)
.. OHAI_VIM*
This class method is called whenever a new instance needs to be built.
- It receives the target class (provided to :attr:`FACTORY_FOR`), and
+ It receives the model class (provided to :attr:`~FactoryOptions.model`), and
the positional and keyword arguments to use for the class once all has
been computed.
Subclasses may override this for custom APIs.
- .. classmethod:: _create(cls, target_class, *args, **kwargs)
+ .. classmethod:: _create(cls, model_class, *args, **kwargs)
.. OHAI_VIM*
@@ -214,10 +295,11 @@ The :class:`Factory` class
.. code-block:: python
class BaseBackendFactory(factory.Factory):
- ABSTRACT_FACTORY = True # Optional
+ class Meta:
+ abstract = True # Optional
- def _create(cls, target_class, *args, **kwargs):
- obj = target_class(*args, **kwargs)
+ def _create(cls, model_class, *args, **kwargs):
+ obj = model_class(*args, **kwargs)
obj.save()
return obj
@@ -254,7 +336,7 @@ The :class:`Factory` class
>>> SomeFactory._next_sequence
4
- Since subclasses of a non-:attr:`abstract <factory.Factory.ABSTRACT_FACTORY>`
+ Since subclasses of a non-:attr:`abstract <factory.FactoryOptions.abstract>`
:class:`~factory.Factory` share the same sequence counter, special care needs
to be taken when resetting the counter of such a subclass.
@@ -293,7 +375,7 @@ factory_boy supports two main strategies for generating instances, plus stubs.
but not persisted to any datastore.
It is usually a simple call to the :meth:`~object.__init__` method of the
- :attr:`~Factory.FACTORY_FOR` class.
+ :attr:`~FactoryOptions.model` class.
.. data:: CREATE_STRATEGY
@@ -316,7 +398,7 @@ factory_boy supports two main strategies for generating instances, plus stubs.
when using the ``create`` strategy.
That policy will be used if the
- :attr:`associated class <Factory.FACTORY_FOR>` has an ``objects``
+ :attr:`associated class <FactoryOptions.model` has an ``objects``
attribute *and* the :meth:`~Factory._create` classmethod of the
:class:`Factory` wasn't overridden.
@@ -337,7 +419,7 @@ factory_boy supports two main strategies for generating instances, plus stubs.
.. data:: STUB_STRATEGY
The 'stub' strategy is an exception in the factory_boy world: it doesn't return
- an instance of the :attr:`~Factory.FACTORY_FOR` class, and actually doesn't
+ an instance of the :attr:`~FactoryOptions.model` class, and actually doesn't
require one to be present.
Instead, it returns an instance of :class:`StubObject` whose attributes have been
@@ -359,7 +441,7 @@ factory_boy supports two main strategies for generating instances, plus stubs.
.. class:: StubFactory(Factory)
- An :attr:`abstract <Factory.ABSTRACT_FACTORY>` :class:`Factory`,
+ An :attr:`abstract <FactoryOptions.abstract>` :class:`Factory`,
with a default strategy set to :data:`STUB_STRATEGY`.
@@ -414,7 +496,8 @@ accept the object being built as sole argument, and return a value.
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
username = 'john'
email = factory.LazyAttribute(lambda o: '%s@example.com' % o.username)
@@ -449,7 +532,8 @@ return value of the method:
.. code-block:: python
class UserFactory(factory.Factory)
- FACTORY_FOR = User
+ class Meta:
+ model = User
name = u"Jean"
@@ -487,7 +571,8 @@ This declaration takes a single argument, a function accepting a single paramete
.. code-block:: python
class UserFactory(factory.Factory)
- FACTORY_FOR = User
+ class Meta:
+ model = User
phone = factory.Sequence(lambda n: '123-555-%04d' % n)
@@ -512,7 +597,8 @@ be the sequence counter - this might be confusing:
.. code-block:: python
class UserFactory(factory.Factory)
- FACTORY_FOR = User
+ class Meta:
+ model = User
@factory.sequence
def phone(n):
@@ -537,7 +623,8 @@ The sequence counter is shared across all :class:`Sequence` attributes of the
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
phone = factory.Sequence(lambda n: '%04d' % n)
office = factory.Sequence(lambda n: 'A23-B%03d' % n)
@@ -561,7 +648,8 @@ sequence counter is shared:
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
phone = factory.Sequence(lambda n: '123-555-%04d' % n)
@@ -596,7 +684,8 @@ class-level value.
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
uid = factory.Sequence(int)
@@ -631,7 +720,8 @@ It takes a single argument, a function whose two parameters are, in order:
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
login = 'john'
email = factory.LazyAttributeSequence(lambda o, n: '%s@s%d.example.com' % (o.login, n))
@@ -655,7 +745,8 @@ handles more complex cases:
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
login = 'john'
@@ -692,7 +783,8 @@ The :class:`SubFactory` attribute should be called with:
.. code-block:: python
class FooFactory(factory.Factory):
- FACTORY_FOR = Foo
+ class Meta:
+ model = Foo
bar = factory.SubFactory(BarFactory) # Not BarFactory()
@@ -705,7 +797,8 @@ Definition
# A standard factory
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
# Various fields
first_name = 'John'
@@ -714,7 +807,8 @@ Definition
# A factory for an object with a 'User' field
class CompanyFactory(factory.Factory):
- FACTORY_FOR = Company
+ class Meta:
+ model = Company
name = factory.Sequence(lambda n: 'FactoryBoyz' + 'z' * n)
@@ -794,13 +888,15 @@ This issue can be handled by passing the absolute import path to the target
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
username = 'john'
main_group = factory.SubFactory('users.factories.GroupFactory')
class GroupFactory(factory.Factory):
- FACTORY_FOR = Group
+ class Meta:
+ model = Group
name = "MyGroup"
owner = factory.SubFactory(UserFactory)
@@ -828,7 +924,8 @@ That declaration takes a single argument, a dot-delimited path to the attribute
.. code-block:: python
class UserFactory(factory.Factory)
- FACTORY_FOR = User
+ class Meta:
+ model = User
birthdate = factory.Sequence(lambda n: datetime.date(2000, 1, 1) + datetime.timedelta(days=n))
birthmonth = factory.SelfAttribute('birthdate.month')
@@ -854,13 +951,15 @@ gains an "upward" semantic through the double-dot notation, as used in Python im
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
language = 'en'
class CompanyFactory(factory.Factory):
- FACTORY_FOR = Company
+ class Meta:
+ model = Company
country = factory.SubFactory(CountryFactory)
owner = factory.SubFactory(UserFactory, language=factory.SelfAttribute('..country.language'))
@@ -888,7 +987,8 @@ through the :attr:`~containers.LazyStub.factory_parent` attribute of the passed-
.. code-block:: python
class CompanyFactory(factory.Factory):
- FACTORY_FOR = Company
+ class Meta:
+ model = Company
country = factory.SubFactory(CountryFactory)
owner = factory.SubFactory(UserFactory,
language=factory.LazyAttribute(lambda user: user.factory_parent.country.language),
@@ -966,7 +1066,8 @@ adequate value.
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
# CATEGORY_CHOICES is a list of (key, title) tuples
category = factory.Iterator(User.CATEGORY_CHOICES, getter=lambda c: c[0])
@@ -987,7 +1088,8 @@ use the :func:`iterator` decorator:
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
@factory.iterator
def name():
@@ -1030,7 +1132,8 @@ with the :class:`Dict` and :class:`List` attributes:
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
is_superuser = False
roles = factory.Dict({
@@ -1066,7 +1169,8 @@ with the :class:`Dict` and :class:`List` attributes:
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
flags = factory.List([
'user',
@@ -1113,7 +1217,8 @@ For instance, a :class:`PostGeneration` hook is declared as ``post``:
.. code-block:: python
class SomeFactory(factory.Factory):
- FACTORY_FOR = SomeObject
+ class Meta:
+ model = SomeObject
@post_generation
def post(self, create, extracted, **kwargs):
@@ -1128,7 +1233,7 @@ When calling the factory, some arguments will be extracted for this method:
- Any argument starting with ``post__XYZ`` will be extracted, its ``post__`` prefix
removed, and added to the kwargs passed to the post-generation hook.
-Extracted arguments won't be passed to the :attr:`~Factory.FACTORY_FOR` class.
+Extracted arguments won't be passed to the :attr:`~FactoryOptions.model` class.
Thus, in the following call:
@@ -1142,7 +1247,7 @@ Thus, in the following call:
)
The ``post`` hook will receive ``1`` as ``extracted`` and ``{'y': 3, 'z__t': 42}``
-as keyword arguments; ``{'post_x': 2}`` will be passed to ``SomeFactory.FACTORY_FOR``.
+as keyword arguments; ``{'post_x': 2}`` will be passed to ``SomeFactory._meta.model``.
RelatedFactory
@@ -1184,7 +1289,8 @@ RelatedFactory
.. code-block:: python
class FooFactory(factory.Factory):
- FACTORY_FOR = Foo
+ class Meta:
+ model = Foo
bar = factory.RelatedFactory(BarFactory) # Not BarFactory()
@@ -1192,13 +1298,15 @@ RelatedFactory
.. code-block:: python
class CityFactory(factory.Factory):
- FACTORY_FOR = City
+ class Meta:
+ model = City
capital_of = None
name = "Toronto"
class CountryFactory(factory.Factory):
- FACTORY_FOR = Country
+ class Meta:
+ model = Country
lang = 'fr'
capital_city = factory.RelatedFactory(CityFactory, 'capital_of', name="Paris")
@@ -1240,7 +1348,7 @@ PostGeneration
.. class:: PostGeneration(callable)
-The :class:`PostGeneration` declaration performs actions once the target object
+The :class:`PostGeneration` declaration performs actions once the model object
has been generated.
Its sole argument is a callable, that will be called once the base object has
@@ -1260,7 +1368,8 @@ as ``callable(obj, create, extracted, **kwargs)``, where:
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
login = 'john'
make_mbox = factory.PostGeneration(
@@ -1280,7 +1389,8 @@ A decorator is also provided, decorating a single method accepting the same
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
login = 'john'
@@ -1316,7 +1426,7 @@ PostGenerationMethodCall
.. attribute:: method_name
- The name of the method to call on the :attr:`~Factory.FACTORY_FOR` object
+ The name of the method to call on the :attr:`~FactoryOptions.model` object
.. attribute:: args
@@ -1340,7 +1450,8 @@ attribute like below:
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
username = 'user'
password = factory.PostGenerationMethodCall('set_password',
@@ -1390,7 +1501,8 @@ factory during instantiation.
.. code-block:: python
class UserFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
username = 'user'
password = factory.PostGenerationMethodCall('set_password',
@@ -1404,7 +1516,8 @@ example, if we declared the ``password`` attribute like the following,
.. code-block:: python
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
username = 'user'
password = factory.PostGenerationMethodCall('set_password', '', 'sha1')
@@ -1467,7 +1580,8 @@ Lightweight factory declaration
# This is equivalent to:
class UserFactory(factory.Factory):
- FACTORY_FOR = User
+ class Meta:
+ model = User
login = 'john'
email = factory.LazyAttribute(lambda u: '%s@example.com' % u.login)
@@ -1486,7 +1600,8 @@ Lightweight factory declaration
# This is equivalent to:
class UserFactory(factory.django.DjangoModelFactory):
- FACTORY_FOR = models.User
+ class Meta:
+ model = models.User
login = 'john'
email = factory.LazyAttribute(lambda u: '%s@example.com' % u.login)