summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_using.py32
1 files changed, 29 insertions, 3 deletions
diff --git a/tests/test_using.py b/tests/test_using.py
index 01e950f..3979cd0 100644
--- a/tests/test_using.py
+++ b/tests/test_using.py
@@ -1018,9 +1018,9 @@ class SubFactoryTestCase(unittest.TestCase):
class TestModel2Factory(FakeModelFactory):
FACTORY_FOR = TestModel2
two = factory.SubFactory(TestModelFactory,
- one=factory.Sequence(lambda n: 'x%dx' % n),
- two=factory.LazyAttribute(
- lambda o: '%s%s' % (o.one, o.one)))
+ one=factory.Sequence(lambda n: 'x%dx' % n),
+ two=factory.LazyAttribute(lambda o: '%s%s' % (o.one, o.one)),
+ )
test_model = TestModel2Factory(one=42)
self.assertEqual('x0x', test_model.two.one)
@@ -1128,6 +1128,32 @@ class SubFactoryTestCase(unittest.TestCase):
self.assertEqual(outer.wrap.wrapped.two.four, 4)
self.assertEqual(outer.wrap.friend, 5)
+ def test_nested_subfactory_with_override(self):
+ """Tests replacing a SubFactory field with an actual value."""
+
+ # The test class
+ class TestObject(object):
+ def __init__(self, two='one', wrapped=None):
+ self.two = two
+ self.wrapped = wrapped
+
+ # Innermost factory
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ two = 'two'
+
+ # Intermediary factory
+ class WrappingTestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+
+ wrapped = factory.SubFactory(TestObjectFactory)
+ wrapped__two = 'three'
+
+ obj = TestObject(two='four')
+ outer = WrappingTestObjectFactory(wrapped=obj)
+ self.assertEqual(obj, outer.wrapped)
+ self.assertEqual('four', outer.wrapped.two)
+
def test_sub_factory_and_inheritance(self):
"""Test inheriting from a factory with subfactories, overriding."""
class TestObject(object):