diff options
author | Raphaël Barrois <raphael.barrois@polyconseil.fr> | 2011-07-05 12:10:29 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polyconseil.fr> | 2011-07-05 12:10:29 +0200 |
commit | 674878a940d1048e56ade5df08876ba963faeeb8 (patch) | |
tree | 9cbbecc771e3251363f25b3bd03bb7f81329a37c | |
parent | b832744b70ed811029edb045afc42ea307073c8e (diff) | |
download | factory-boy-674878a940d1048e56ade5df08876ba963faeeb8.tar factory-boy-674878a940d1048e56ade5df08876ba963faeeb8.tar.gz |
Allow users to define a custom method for retrieving the first sequence of a given factory.
Signed-off-by: Raphaël Barrois <raphael.barrois@polyconseil.fr>
-rw-r--r-- | factory/base.py | 8 | ||||
-rw-r--r-- | factory/test_base.py | 17 |
2 files changed, 24 insertions, 1 deletions
diff --git a/factory/base.py b/factory/base.py index ff77e77..736a0e6 100644 --- a/factory/base.py +++ b/factory/base.py @@ -144,10 +144,16 @@ class BaseFactory(object): def __new__(cls, *args, **kwargs): raise RuntimeError('You cannot instantiate BaseFactory') - _next_sequence = 0 + _next_sequence = None + + @classmethod + def _setup_next_sequence(cls): + return 0 @classmethod def _generate_next_sequence(cls): + if cls._next_sequence is None: + cls._next_sequence = cls._setup_next_sequence() next_sequence = cls._next_sequence cls._next_sequence += 1 return next_sequence diff --git a/factory/test_base.py b/factory/test_base.py index b324680..2b8cb14 100644 --- a/factory/test_base.py +++ b/factory/test_base.py @@ -74,6 +74,23 @@ class FactoryTestCase(unittest.TestCase): self.assertEqual(test_object1.one, 'one1') self.assertEqual(test_object1.two, 'two1') + def testSequenceCustomBegin(self): + class TestObjectFactory(Factory): + @classmethod + def _setup_next_sequence(cls): + return 42 + + one = declarations.Sequence(lambda n: 'one' + n) + two = declarations.Sequence(lambda n: 'two' + n) + + test_object0 = TestObjectFactory.build() + self.assertEqual('one42', test_object0.one) + self.assertEqual('two42', test_object0.two) + + test_object1 = TestObjectFactory.build() + self.assertEqual('one43', test_object1.one) + self.assertEqual('two43', test_object1.two) + def testLazyAttribute(self): class TestObjectFactory(Factory): one = declarations.LazyAttribute(lambda a: 'abc' ) |