summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2012-01-08 21:14:54 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2012-01-08 21:14:54 +0100
commit1d2ca8d02e190460f85d20be47bbb1e02f268bd2 (patch)
tree30b0ce9cb19a466ebb32852f0310cef68b9b2c2b
parentb81f0282acbb8ee03d56b5ea8957e439efc5bf27 (diff)
downloadfactory-boy-1d2ca8d02e190460f85d20be47bbb1e02f268bd2.tar
factory-boy-1d2ca8d02e190460f85d20be47bbb1e02f268bd2.tar.gz
Add tests for subfactory field overriding.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
-rw-r--r--tests/test_base.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/tests/test_base.py b/tests/test_base.py
index b445cb3..aaaa65f 100644
--- a/tests/test_base.py
+++ b/tests/test_base.py
@@ -367,6 +367,28 @@ class FactoryDefaultStrategyTestCase(unittest.TestCase):
self.assertEqual('x0x', test_model.two.one)
self.assertEqual('x0xx0x', test_model.two.two)
+ def testSubFactoryOverriding(self):
+ class TestObject(object):
+ def __init__(self, **kwargs):
+ for k, v in kwargs.iteritems():
+ setattr(self, k, v)
+
+ class TestObjectFactory(base.Factory):
+ FACTORY_FOR = TestObject
+
+ class WrappingTestObjectFactory(base.Factory):
+ FACTORY_FOR = TestObject
+
+ wrapped = declarations.SubFactory(TestObjectFactory, two=2, four=4)
+ wrapped__two = 4
+ wrapped__three = 3
+
+ wrapping = WrappingTestObjectFactory.build()
+ self.assertEqual(wrapping.wrapped.two, 4)
+ self.assertEqual(wrapping.wrapped.three, 3)
+ self.assertEqual(wrapping.wrapped.four, 4)
+
+
def testNestedSubFactory(self):
"""Test nested sub-factories."""
@@ -421,6 +443,31 @@ class FactoryDefaultStrategyTestCase(unittest.TestCase):
self.assertEqual(outer.wrap.wrapped.two.four, 4)
self.assertEqual(outer.wrap.friend, 5)
+ def testSubFactoryAndInheritance(self):
+ """Test inheriting from a factory with subfactories, overriding."""
+ class TestObject(object):
+ def __init__(self, **kwargs):
+ for k, v in kwargs.iteritems():
+ setattr(self, k, v)
+
+ class TestObjectFactory(base.Factory):
+ FACTORY_FOR = TestObject
+ two = 'two'
+
+ class WrappingTestObjectFactory(base.Factory):
+ FACTORY_FOR = TestObject
+
+ wrapped = declarations.SubFactory(TestObjectFactory)
+ friend = declarations.LazyAttribute(lambda o: o.wrapped.two + 1)
+
+ class ExtendedWrappingTestObjectFactory(WrappingTestObjectFactory):
+ wrapped__two = 4
+
+ wrapping = ExtendedWrappingTestObjectFactory.build()
+ self.assertEqual(wrapping.wrapped.two, 4)
+ self.assertEqual(wrapping.friend, 5)
+
+
def testStubStrategy(self):
base.Factory.default_strategy = base.STUB_STRATEGY