diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-04-02 23:07:03 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-04-02 23:07:03 +0200 |
commit | 69e7a86875f97dc12e941302fabe417122f2cb7e (patch) | |
tree | 096d558041ad5ff0794a159580aab09b8f7bb9a5 /docs/reference.rst | |
parent | 54a915fa25a66d3b7732a096eba7c2dd4a7b5a8e (diff) | |
download | factory-boy-69e7a86875f97dc12e941302fabe417122f2cb7e.tar factory-boy-69e7a86875f97dc12e941302fabe417122f2cb7e.tar.gz |
Add Factory.FACTORY_HIDDEN_ARGS.
Fields listed in this class attributes will be removed from the
kwargs dict passed to the associated class for building/creation.
Diffstat (limited to 'docs/reference.rst')
-rw-r--r-- | docs/reference.rst | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/docs/reference.rst b/docs/reference.rst index d5b14a9..a2af327 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -50,6 +50,37 @@ The :class:`Factory` class <User: john> >>> User('john', 'john@example.com', firstname="John") # actual call + .. attribute:: FACTORY_HIDDEN_ARGS + + 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 + hold a reference time used by other objects. + + Factory fields whose name are listed in :attr:`FACTORY_HIDDEN_ARGS` 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',) + + now = factory.LazyAttribute(lambda o: datetime.datetime.utcnow()) + started_at = factory.LazyAttribute(lambda o: o.now - datetime.timedelta(hours=1)) + paid_at = factory.LazyAttribute(lambda o: o.now - datetime.timedelta(minutes=50)) + + .. code-block:: pycon + + >>> OrderFactory() # The value of 'now' isn't passed to Order() + <Order: started 2013-04-01 12:00:00, paid 2013-04-01 12:10:00> + + >>> # An alternate value may be passed for 'now' + >>> OrderFactory(now=datetime.datetime(2013, 4, 1, 10)) + <Order: started 2013-04-01 09:00:00, paid 2013-04-01 09:10:00> + + **Base functions:** The :class:`Factory` class provides a few methods for getting objects; |