aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-09-17 01:12:48 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-09-17 01:12:48 +0200
commit7fe9dcaa8494e73d57613d1288b4f86c4cba5bf0 (patch)
tree5f7c3728caeb67609be6e7ba17e03b2fa93dbc22 /tests
parenta8742c973db224968b74bb054027130b2ab458e0 (diff)
downloadfactory-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')
-rw-r--r--tests/test_alchemy.py2
-rw-r--r--tests/test_using.py72
2 files changed, 73 insertions, 1 deletions
diff --git a/tests/test_alchemy.py b/tests/test_alchemy.py
index cfbc835..4255417 100644
--- a/tests/test_alchemy.py
+++ b/tests/test_alchemy.py
@@ -65,7 +65,7 @@ class SQLAlchemyPkSequenceTestCase(unittest.TestCase):
def setUp(self):
super(SQLAlchemyPkSequenceTestCase, self).setUp()
- StandardFactory.reset_sequence()
+ StandardFactory.reset_sequence(1)
NonIntegerPkFactory.FACTORY_SESSION.rollback()
def test_pk_first(self):
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