summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polyconseil.fr>2011-05-24 09:58:16 +0200
committerRaphaël Barrois <raphael.barrois@polyconseil.fr>2011-05-24 09:58:16 +0200
commitcb66c88859d8de161561ae42c9eccd8af56440c8 (patch)
treea9c37f4ad39c661b5f698f08080fb64adda41e44
parente42516b995053d0d5f8d4d13a94cea4953ea1850 (diff)
downloadfactory-boy-cb66c88859d8de161561ae42c9eccd8af56440c8.tar
factory-boy-cb66c88859d8de161561ae42c9eccd8af56440c8.tar.gz
Fix the SubFactory/Sequence behaviour.
Signed-off-by: Raphaël Barrois <raphael.barrois@polyconseil.fr>
-rw-r--r--factory/base.py3
-rw-r--r--factory/containers.py2
-rw-r--r--factory/declarations.py16
-rw-r--r--factory/test_containers.py38
4 files changed, 47 insertions, 12 deletions
diff --git a/factory/base.py b/factory/base.py
index d38c82d..ff77e77 100644
--- a/factory/base.py
+++ b/factory/base.py
@@ -163,9 +163,6 @@ class BaseFactory(object):
applicable; the current list of computed attributes is available for
to the currently processed object.
"""
- attributes = {}
- cls.sequence = cls._generate_next_sequence()
-
return getattr(cls, CLASS_ATTRIBUTE_DECLARATIONS).build_attributes(cls, create, kwargs)
@classmethod
diff --git a/factory/containers.py b/factory/containers.py
index 8aa9955..be61641 100644
--- a/factory/containers.py
+++ b/factory/containers.py
@@ -139,6 +139,8 @@ class DeclarationsHolder(object):
if not extra:
extra = {}
+ factory.sequence = factory._generate_next_sequence()
+
attributes = {}
sub_fields = {}
for key in list(extra.keys()):
diff --git a/factory/declarations.py b/factory/declarations.py
index a4a62af..238e592 100644
--- a/factory/declarations.py
+++ b/factory/declarations.py
@@ -76,7 +76,13 @@ class LazyAttributeSequence(Sequence):
return self.function(attributes, self.type(factory.sequence))
class SubFactory(OrderedDeclaration):
- """Base class for attributes based upon a sub-factory."""
+ """Base class for attributes based upon a sub-factory.
+
+ Attributes:
+ defaults: DeclarationsHolder, the declarations from the wrapped factory
+ factory: Factory, the wrapped factory
+ """
+
def __init__(self, factory, **kwargs):
super(SubFactory, self).__init__()
self.defaults = factory.declarations()
@@ -84,6 +90,14 @@ class SubFactory(OrderedDeclaration):
self.factory = factory
def evaluate(self, factory, create, attributes):
+ """Evaluate the current definition and fill its attributes.
+
+ Uses attributes definition in the following order:
+ - attributes defined in the wrapped factory class
+ - values defined when defining the SubFactory
+ - additional valued defined in attributes
+ """
+
attrs = self.defaults.build_attributes(self.factory, create, attributes)
if create:
return self.factory.create(**attrs)
diff --git a/factory/test_containers.py b/factory/test_containers.py
index 3cc61e1..00a0151 100644
--- a/factory/test_containers.py
+++ b/factory/test_containers.py
@@ -114,6 +114,11 @@ class DeclarationsHolderTestCase(unittest.TestCase):
def test_simple(self):
"""Tests a simple use case without OrderedDeclaration."""
+ class ExampleFactory(object):
+ @classmethod
+ def _generate_next_sequence(cls):
+ return 42
+
holder = DeclarationsHolder({'one': 1, 'two': 2})
self.assertTrue('one' in holder)
self.assertTrue('two' in holder)
@@ -122,15 +127,16 @@ class DeclarationsHolderTestCase(unittest.TestCase):
self.assertEqual(3, holder['two'])
self.assertEqual(3, holder['three'])
- attrs = holder.build_attributes(None)
+ attrs = holder.build_attributes(ExampleFactory)
self.assertEqual(1, attrs['one'])
self.assertEqual(3, attrs['two'])
self.assertEqual(3, attrs['three'])
+ self.assertEqual(42, ExampleFactory.sequence)
self.assertEqual(set([('one', 1), ('two', 3), ('three', 3)]),
set(holder.items()))
- attrs = holder.build_attributes(None, False, {'two': 2})
+ attrs = holder.build_attributes(ExampleFactory, False, {'two': 2})
self.assertEqual(1, attrs['one'])
self.assertEqual(2, attrs['two'])
self.assertEqual(3, attrs['three'])
@@ -148,6 +154,11 @@ class DeclarationsHolderTestCase(unittest.TestCase):
def test_ordered(self):
"""Tests the handling of OrderedDeclaration."""
+ class ExampleFactory(object):
+ @classmethod
+ def _generate_next_sequence(cls):
+ return 42
+
two = declarations.LazyAttribute(lambda o: 2 * o.one)
three = declarations.LazyAttribute(lambda o: o.one + o.two)
holder = DeclarationsHolder({'one': 1, 'two': two, 'three': three})
@@ -157,17 +168,17 @@ class DeclarationsHolderTestCase(unittest.TestCase):
self.assertEqual(two, holder['two'])
- attrs = holder.build_attributes(None)
+ attrs = holder.build_attributes(ExampleFactory)
self.assertEqual(1, attrs['one'])
self.assertEqual(2, attrs['two'])
self.assertEqual(3, attrs['three'])
- attrs = holder.build_attributes(None, False, {'one': 4})
+ attrs = holder.build_attributes(ExampleFactory, False, {'one': 4})
self.assertEqual(4, attrs['one'])
self.assertEqual(8, attrs['two'])
self.assertEqual(12, attrs['three'])
- attrs = holder.build_attributes(None, False, {'one': 4, 'two': 2})
+ attrs = holder.build_attributes(ExampleFactory, False, {'one': 4, 'two': 2})
self.assertEqual(4, attrs['one'])
self.assertEqual(2, attrs['two'])
self.assertEqual(6, attrs['three'])
@@ -175,15 +186,26 @@ class DeclarationsHolderTestCase(unittest.TestCase):
def test_sub_factory(self):
"""Tests the behaviour of sub-factories."""
class TestObject(object):
- def __init__(self, one=None, two=None, three=None, four=None):
+ def __init__(self, one=None, two=None, three=None, four=None, five=None):
self.one = one
self.two = two
self.three = three
self.four = four
+ self.five = five
class TestObjectFactory(base.Factory):
FACTORY_FOR = TestObject
two = 2
+ five = declarations.Sequence(lambda n: n+1, type=int)
+
+ @classmethod
+ def _generate_next_sequence(cls):
+ return 42
+
+ class ExampleFactory(object):
+ @classmethod
+ def _generate_next_sequence(cls):
+ return 42
sub = declarations.SubFactory(TestObjectFactory,
three=3,
@@ -194,13 +216,13 @@ class DeclarationsHolderTestCase(unittest.TestCase):
self.assertEqual(sub, holder['sub'])
self.assertTrue('sub' in holder)
- attrs = holder.build_attributes(None)
+ attrs = holder.build_attributes(ExampleFactory)
self.assertEqual(1, attrs['one'])
self.assertEqual(2, attrs['sub'].two)
self.assertEqual(3, attrs['sub'].three)
self.assertEqual(4, attrs['sub'].four)
- attrs = holder.build_attributes(None, False, {'sub__two': 8, 'three__four': 4})
+ attrs = holder.build_attributes(ExampleFactory, False, {'sub__two': 8, 'three__four': 4})
self.assertEqual(1, attrs['one'])
self.assertEqual(4, attrs['three__four'])
self.assertEqual(8, attrs['sub'].two)