summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-06-10 01:18:54 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-06-10 01:18:54 +0200
commit85dde20cf2e337a4e0b7de47d067edfaf2e633ab (patch)
treea966d40b8dc24c65e8c84c282231a662eb55be46 /tests
parent639e5cd1c6baf1cb19d9134545e29fbb5ba16d99 (diff)
downloadfactory-boy-85dde20cf2e337a4e0b7de47d067edfaf2e633ab.tar
factory-boy-85dde20cf2e337a4e0b7de47d067edfaf2e633ab.tar.gz
Add Factory.reset_sequence.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_base.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/tests/test_base.py b/tests/test_base.py
index 73e59fa..4978d10 100644
--- a/tests/test_base.py
+++ b/tests/test_base.py
@@ -34,6 +34,7 @@ class TestObject(object):
self.three = three
self.four = four
+
class FakeDjangoModel(object):
@classmethod
def create(cls, **kwargs):
@@ -46,6 +47,7 @@ class FakeDjangoModel(object):
setattr(self, name, value)
self.id = None
+
class FakeModelFactory(base.Factory):
ABSTRACT_FACTORY = True
@@ -116,6 +118,84 @@ class FactoryTestCase(unittest.TestCase):
self.assertEqual(4, len(ones))
+class FactorySequenceTestCase(unittest.TestCase):
+ def setUp(self):
+ super(FactorySequenceTestCase, self).setUp()
+
+ class TestObjectFactory(base.Factory):
+ FACTORY_FOR = TestObject
+ one = declarations.Sequence(lambda n: n)
+
+ self.TestObjectFactory = TestObjectFactory
+
+ def test_reset_sequence(self):
+ o1 = self.TestObjectFactory()
+ self.assertEqual(0, o1.one)
+
+ o2 = self.TestObjectFactory()
+ self.assertEqual(1, o2.one)
+
+ self.TestObjectFactory.reset_sequence()
+ o3 = self.TestObjectFactory()
+ self.assertEqual(0, o3.one)
+
+ def test_reset_sequence_with_value(self):
+ o1 = self.TestObjectFactory()
+ self.assertEqual(0, o1.one)
+
+ o2 = self.TestObjectFactory()
+ self.assertEqual(1, o2.one)
+
+ self.TestObjectFactory.reset_sequence(42)
+ o3 = self.TestObjectFactory()
+ self.assertEqual(42, o3.one)
+
+ def test_reset_sequence_subclass_fails(self):
+ """Tests that the sequence of a 'slave' factory cannot be reseted."""
+ class SubTestObjectFactory(self.TestObjectFactory):
+ pass
+
+ self.assertRaises(ValueError, SubTestObjectFactory.reset_sequence)
+
+ def test_reset_sequence_subclass_force(self):
+ """Tests that reset_sequence(force=True) works."""
+ class SubTestObjectFactory(self.TestObjectFactory):
+ pass
+
+ o1 = SubTestObjectFactory()
+ self.assertEqual(0, o1.one)
+
+ o2 = SubTestObjectFactory()
+ self.assertEqual(1, o2.one)
+
+ SubTestObjectFactory.reset_sequence(force=True)
+ o3 = SubTestObjectFactory()
+ self.assertEqual(0, o3.one)
+
+ # The master sequence counter has been reset
+ o4 = self.TestObjectFactory()
+ self.assertEqual(1, o4.one)
+
+ def test_reset_sequence_subclass_parent(self):
+ """Tests that the sequence of a 'slave' factory cannot be reseted."""
+ class SubTestObjectFactory(self.TestObjectFactory):
+ pass
+
+ o1 = SubTestObjectFactory()
+ self.assertEqual(0, o1.one)
+
+ o2 = SubTestObjectFactory()
+ self.assertEqual(1, o2.one)
+
+ self.TestObjectFactory.reset_sequence()
+ o3 = SubTestObjectFactory()
+ self.assertEqual(0, o3.one)
+
+ o4 = self.TestObjectFactory()
+ self.assertEqual(1, o4.one)
+
+
+
class FactoryDefaultStrategyTestCase(unittest.TestCase):
def setUp(self):
self.default_strategy = base.Factory.FACTORY_STRATEGY