diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-09-17 01:12:48 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-09-17 01:12:48 +0200 |
commit | 7fe9dcaa8494e73d57613d1288b4f86c4cba5bf0 (patch) | |
tree | 5f7c3728caeb67609be6e7ba17e03b2fa93dbc22 /tests/test_using.py | |
parent | a8742c973db224968b74bb054027130b2ab458e0 (diff) | |
download | factory-boy-7fe9dcaa8494e73d57613d1288b4f86c4cba5bf0.tar factory-boy-7fe9dcaa8494e73d57613d1288b4f86c4cba5bf0.tar.gz |
Properly handle Sequence & inheritance (Closes #93).
There was also a nasty bug: with class FactoryB(FactoryA), FactoryB's sequence
counter started at the value of FactoryA's counter when FactoryB was first called.
Diffstat (limited to 'tests/test_using.py')
-rw-r--r-- | tests/test_using.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/test_using.py b/tests/test_using.py index 0898a13..01e950f 100644 --- a/tests/test_using.py +++ b/tests/test_using.py @@ -730,6 +730,78 @@ class UsingFactoryTestCase(unittest.TestCase): test_object_alt = TestObjectFactory.build() self.assertEqual(None, test_object_alt.three) + def test_inheritance_and_sequences(self): + """Sequence counters should be kept within an inheritance chain.""" + class TestObjectFactory(factory.Factory): + FACTORY_FOR = TestObject + + one = factory.Sequence(lambda n: n) + + class TestObjectFactory2(TestObjectFactory): + FACTORY_FOR = TestObject + + to1a = TestObjectFactory() + self.assertEqual(0, to1a.one) + to2a = TestObjectFactory2() + self.assertEqual(1, to2a.one) + to1b = TestObjectFactory() + self.assertEqual(2, to1b.one) + to2b = TestObjectFactory2() + self.assertEqual(3, to2b.one) + + def test_inheritance_sequence_inheriting_objects(self): + """Sequence counters are kept with inheritance, incl. misc objects.""" + class TestObject2(TestObject): + pass + + class TestObjectFactory(factory.Factory): + FACTORY_FOR = TestObject + + one = factory.Sequence(lambda n: n) + + class TestObjectFactory2(TestObjectFactory): + FACTORY_FOR = TestObject2 + + to1a = TestObjectFactory() + self.assertEqual(0, to1a.one) + to2a = TestObjectFactory2() + self.assertEqual(1, to2a.one) + to1b = TestObjectFactory() + self.assertEqual(2, to1b.one) + to2b = TestObjectFactory2() + self.assertEqual(3, to2b.one) + + def test_inheritance_sequence_unrelated_objects(self): + """Sequence counters are kept with inheritance, unrelated objects. + + See issue https://github.com/rbarrois/factory_boy/issues/93 + + Problem: sequence counter is somewhat shared between factories + until the "slave" factory has been called. + """ + + class TestObject2(object): + def __init__(self, one): + self.one = one + + class TestObjectFactory(factory.Factory): + FACTORY_FOR = TestObject + + one = factory.Sequence(lambda n: n) + + class TestObjectFactory2(TestObjectFactory): + FACTORY_FOR = TestObject2 + + to1a = TestObjectFactory() + self.assertEqual(0, to1a.one) + to2a = TestObjectFactory2() + self.assertEqual(0, to2a.one) + to1b = TestObjectFactory() + self.assertEqual(1, to1b.one) + to2b = TestObjectFactory2() + self.assertEqual(1, to2b.one) + + def test_inheritance_with_inherited_class(self): class TestObjectFactory(factory.Factory): FACTORY_FOR = TestObject |