summaryrefslogtreecommitdiff
path: root/factory
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-04-15 02:21:08 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-04-15 02:21:08 +0200
commite7a9a87320c78ec05a5d548516fe17c258e6d4c7 (patch)
tree78d92de6e928ded94f7fda979be6f560844aff85 /factory
parent54971381fb31167d1f47b5e705480e044d702602 (diff)
downloadfactory-boy-e7a9a87320c78ec05a5d548516fe17c258e6d4c7.tar
factory-boy-e7a9a87320c78ec05a5d548516fe17c258e6d4c7.tar.gz
Allow overriding the sequence counter.
Diffstat (limited to 'factory')
-rw-r--r--factory/base.py8
-rw-r--r--factory/containers.py13
2 files changed, 16 insertions, 5 deletions
diff --git a/factory/base.py b/factory/base.py
index 13a0623..25f4714 100644
--- a/factory/base.py
+++ b/factory/base.py
@@ -282,7 +282,13 @@ class BaseFactory(object):
applicable; the current list of computed attributes is available
to the currently processed object.
"""
- return containers.AttributeBuilder(cls, extra).build(create)
+ force_sequence = None
+ if extra:
+ force_sequence = extra.pop('__sequence', None)
+ return containers.AttributeBuilder(cls, extra).build(
+ create=create,
+ force_sequence=force_sequence,
+ )
@classmethod
def declarations(cls, extra_defs=None):
diff --git a/factory/containers.py b/factory/containers.py
index e02f9f9..ee2ad82 100644
--- a/factory/containers.py
+++ b/factory/containers.py
@@ -236,16 +236,21 @@ class AttributeBuilder(object):
attrs_with_subfields, self._attrs)
def has_subfields(self, value):
- return isinstance(value, declarations.SubFactory)
+ return isinstance(value, declarations.ParameteredAttribute)
- def build(self, create):
+ def build(self, create, force_sequence=None):
"""Build a dictionary of attributes.
Args:
create (bool): whether to 'build' or 'create' the subfactories.
+ force_sequence (int or None): if set to an int, use this value for
+ the sequence counter; don't advance the related counter.
"""
# Setup factory sequence.
- self.factory.sequence = self.factory._generate_next_sequence()
+ if force_sequence is None:
+ sequence = self.factory._generate_next_sequence()
+ else:
+ sequence = force_sequence
# Parse attribute declarations, wrapping SubFactory and
# OrderedDeclaration.
@@ -253,7 +258,7 @@ class AttributeBuilder(object):
for k, v in self._attrs.items():
if isinstance(v, declarations.OrderedDeclaration):
v = OrderedDeclarationWrapper(v,
- sequence=self.factory.sequence,
+ sequence=sequence,
create=create,
extra=self._subfields.get(k, {}),
)