diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-03-04 23:18:02 +0100 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-03-04 23:18:02 +0100 |
commit | 7d792430e103984a91c102c33da79be2426bc632 (patch) | |
tree | 0044c449e933e93d4fecf1e80e6b361c6c3e8150 /tests | |
parent | 9422cf12516143650f1014f34f996260c00d4c0a (diff) | |
download | factory-boy-7d792430e103984a91c102c33da79be2426bc632.tar factory-boy-7d792430e103984a91c102c33da79be2426bc632.tar.gz |
Add a 'after post_generation' hook to Factory.
Use it in DjangoModelFactory to save objects again if a post_generation hook ran.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_using.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/tests/test_using.py b/tests/test_using.py index e5af8fb..9bc466e 100644 --- a/tests/test_using.py +++ b/tests/test_using.py @@ -1216,6 +1216,27 @@ class PostGenerationTestCase(unittest.TestCase): self.assertEqual(3, obj.one) self.assertFalse(hasattr(obj, 'incr_one')) + def test_post_generation_hook(self): + class TestObjectFactory(factory.Factory): + FACTORY_FOR = TestObject + + one = 1 + + @factory.post_generation + def incr_one(self, _create, _increment): + self.one += 1 + return 42 + + @classmethod + def _after_postgeneration(cls, obj, create, results): + obj.create = create + obj.results = results + + obj = TestObjectFactory.build() + self.assertEqual(2, obj.one) + self.assertFalse(obj.create) + self.assertEqual({'incr_one': 42}, obj.results) + @tools.disable_warnings def test_post_generation_calling(self): class TestObjectFactory(factory.Factory): |