summaryrefslogtreecommitdiff
path: root/tests/test_using.py
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-03-03 20:32:42 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-03-03 21:38:46 +0100
commitf248ebda408faee17f32c8f260bcf2d5df27b83d (patch)
tree8318d2e6e691e1f3391a19da9102bc1b776469ef /tests/test_using.py
parent37621846d541d7afcad52e6dba8281bd1146cf09 (diff)
downloadfactory-boy-f248ebda408faee17f32c8f260bcf2d5df27b83d.tar
factory-boy-f248ebda408faee17f32c8f260bcf2d5df27b83d.tar.gz
Improve coverage.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
Diffstat (limited to 'tests/test_using.py')
-rw-r--r--tests/test_using.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/tests/test_using.py b/tests/test_using.py
index fd8befb..20593f4 100644
--- a/tests/test_using.py
+++ b/tests/test_using.py
@@ -296,6 +296,24 @@ class UsingFactoryTestCase(unittest.TestCase):
self.assertEqual('one43', test_object1.one)
self.assertEqual('two43', test_object1.two)
+ def test_custom_create(self):
+ class TestModelFactory(factory.Factory):
+ FACTORY_FOR = TestModel
+
+ two = 2
+
+ @classmethod
+ def _create(cls, target_class, *args, **kwargs):
+ obj = target_class.create(**kwargs)
+ obj.properly_created = True
+ return obj
+
+ obj = TestModelFactory.create(one=1)
+ self.assertEqual(1, obj.one)
+ self.assertEqual(2, obj.two)
+ self.assertEqual(1, obj.id)
+ self.assertTrue(obj.properly_created)
+
def test_sequence_batch(self):
class TestObjectFactory(factory.Factory):
FACTORY_FOR = TestObject
@@ -661,6 +679,19 @@ class UsingFactoryTestCase(unittest.TestCase):
self.assertEqual('four', obj.four)
@tools.disable_warnings
+ def test_set_building_function(self):
+ def building_function(class_to_create, **kwargs):
+ return "This doesn't even return an instance of {0}".format(class_to_create.__name__)
+
+ class TestModelFactory(FakeModelFactory):
+ FACTORY_FOR = TestModel
+
+ TestModelFactory.set_building_function(building_function)
+
+ test_object = TestModelFactory.build()
+ self.assertEqual(test_object, "This doesn't even return an instance of TestModel")
+
+ @tools.disable_warnings
def testSetCreationFunction(self):
def creation_function(class_to_create, **kwargs):
return "This doesn't even return an instance of {0}".format(class_to_create.__name__)
@@ -1309,6 +1340,50 @@ class PostGenerationTestCase(unittest.TestCase):
# RelatedFactory received "parent" object
self.assertEqual(obj, obj.related.three)
+ def test_related_factory_no_name(self):
+ relateds = []
+ class TestRelatedObject(object):
+ def __init__(self, obj=None, one=None, two=None):
+ relateds.append(self)
+ self.one = one
+ self.two = two
+ self.three = obj
+
+ class TestRelatedObjectFactory(factory.Factory):
+ FACTORY_FOR = TestRelatedObject
+ one = 1
+ two = factory.LazyAttribute(lambda o: o.one + 1)
+
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ one = 3
+ two = 2
+ three = factory.RelatedFactory(TestRelatedObjectFactory)
+
+ obj = TestObjectFactory.build()
+ # Normal fields
+ self.assertEqual(3, obj.one)
+ self.assertEqual(2, obj.two)
+ # RelatedFactory was built
+ self.assertIsNone(obj.three)
+ self.assertEqual(1, len(relateds))
+ related = relateds[0]
+ self.assertEqual(1, related.one)
+ self.assertEqual(2, related.two)
+ self.assertIsNone(related.three)
+
+ obj = TestObjectFactory.build(three__one=3)
+ # Normal fields
+ self.assertEqual(3, obj.one)
+ self.assertEqual(2, obj.two)
+ # RelatedFactory was build
+ self.assertIsNone(obj.three)
+ self.assertEqual(2, len(relateds))
+
+ related = relateds[1]
+ self.assertEqual(3, related.one)
+ self.assertEqual(4, related.two)
+
class CircularTestCase(unittest.TestCase):
def test_example(self):