summaryrefslogtreecommitdiff
path: root/README.rst
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polyconseil.fr>2011-07-13 12:45:24 +0200
committerRaphaël Barrois <raphael.barrois@polyconseil.fr>2011-07-13 12:45:24 +0200
commitea86cec104f3a469318349af3ced1262d47169aa (patch)
treea2c0c01f78414b9703728e905657bbc02f41622d /README.rst
parentc4e749bf464cfd9aced3adcf729052079bc25eaa (diff)
downloadfactory-boy-ea86cec104f3a469318349af3ced1262d47169aa.tar
factory-boy-ea86cec104f3a469318349af3ced1262d47169aa.tar.gz
Fix doc for _setup_next_sequence; fix trailing whitespace.
Signed-off-by: Raphaël Barrois <raphael.barrois@polyconseil.fr>
Diffstat (limited to 'README.rst')
-rw-r--r--README.rst56
1 files changed, 28 insertions, 28 deletions
diff --git a/README.rst b/README.rst
index fded509..4f7cdd7 100644
--- a/README.rst
+++ b/README.rst
@@ -18,14 +18,14 @@ Download
Github: http://github.com/dnerdy/factory_boy/tree/master
easy_install::
-
+
easy_install factory_boy
-
+
Source::
-
+
# Download the source and run
python setup.py install
-
+
Defining factories
------------------
@@ -34,17 +34,17 @@ Factories declare a set of attributes used to instantiate an object. The name of
import factory
from models import User
-
+
# This will guess the User class
class UserFactory(factory.Factory):
first_name = 'John'
last_name = 'Doe'
admin = False
-
+
# This will use the User class (Admin would have been guessed)
class AdminFactory(factory.Factory):
FACTORY_FOR = User
-
+
first_name = 'Admin'
last_name = 'User'
admin = True
@@ -56,38 +56,38 @@ factory_boy supports several different build strategies: build, create, attribut
# Returns a User instance that's not saved
user = UserFactory.build()
-
+
# Returns a saved User instance
user = UserFactory.create()
-
+
# Returns a dict of attributes that can be used to build a User instance
attributes = UserFactory.attributes()
-
+
# Returns an object with all defined attributes stubbed out:
stub = UserFactory.stub()
-
+
You can use the Factory class as a shortcut for the default build strategy::
# Same as UserFactory.create()
user = UserFactory()
-
+
The default strategy can be overridden::
UserFactory.default_strategy = factory.BUILD_STRATEGY
user = UserFactory()
-
+
The default strategy can also be overridden for all factories::
# This will set the default strategy for all factories that don't define a default build strategy
factory.Factory.default_strategy = factory.BUILD_STRATEGY
-
+
No matter which strategy is used, it's possible to override the defined attributes by passing keyword arguments::
# Build a User instance and override first_name
user = UserFactory.build(first_name='Joe')
user.first_name
# => 'Joe'
-
+
Lazy Attributes
---------------
@@ -97,32 +97,32 @@ Most factory attributes can be added using static values that are evaluated when
first_name = 'Joe'
last_name = 'Blow'
email = factory.LazyAttribute(lambda a: '{0}.{1}@example.com'.format(a.first_name, a.last_name).lower())
-
+
UserFactory().email
# => 'joe.blow@example.com'
-
+
The function passed to ``LazyAttribute`` is given the attributes defined for the factory up to the point of the LazyAttribute declaration. If a lambda won't cut it, the ``lazy_attribute`` decorator can be used to wrap a function::
# Stub factories don't have an associated class.
class SumFactory(factory.StubFactory):
lhs = 1
rhs = 1
-
+
@lazy_attribute
def sum(a):
result = a.lhs + a.rhs # Or some other fancy calculation
return result
-
+
Associations
------------
Associated instances can also be generated using ``LazyAttribute``::
from models import Post
-
+
class PostFactory(factory.Factory):
author = factory.LazyAttribute(lambda a: UserFactory())
-
+
The associated object's default strategy is always used::
# Builds and saves a User and a Post
@@ -134,7 +134,7 @@ The associated object's default strategy is always used::
post = PostFactory.build()
post.id == None # => True
post.author.id == None # => False
-
+
Inheritance
-----------
@@ -142,11 +142,11 @@ You can easily create multiple factories for the same class without repeating co
class PostFactory(factory.Factory):
title = 'A title'
-
+
class ApprovedPost(PostFactory):
approved = True
approver = factory.LazyAttribute(lambda a: UserFactory())
-
+
Sequences
---------
@@ -154,16 +154,16 @@ Unique values in a specific format (for example, e-mail addresses) can be genera
class UserFactory(factory.Factory):
email = factory.Sequence(lambda n: 'person{0}@example.com'.format(n))
-
+
UserFactory().email # => 'person0@example.com'
UserFactory().email # => 'person1@example.com'
-
+
Sequences can be combined with lazy attributes::
class UserFactory(factory.Factory):
name = 'Mark'
email = factory.LazyAttributeSequence(lambda a, n: '{0}+{1}@example.com'.format(a.name, n).lower())
-
+
UserFactory().email # => mark+0@example.com
If you wish to use a custom method to set the initial ID for a sequence, you can override the ``_setup_next_sequence`` class method::
@@ -172,7 +172,7 @@ If you wish to use a custom method to set the initial ID for a sequence, you can
@classmethod
def _setup_next_sequence(cls):
- return cls.FACTORY_FOR.objects.values_list('id').order_by('-id')[0] + 1
+ return cls._associated_class.objects.values_list('id').order_by('-id')[0] + 1
Customizing creation
--------------------