From 674878a940d1048e56ade5df08876ba963faeeb8 Mon Sep 17 00:00:00 2001 From: Raphaël Barrois Date: Tue, 5 Jul 2011 12:10:29 +0200 Subject: Allow users to define a custom method for retrieving the first sequence of a given factory. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Raphaël Barrois --- factory/base.py | 8 +++++++- factory/test_base.py | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) (limited to 'factory') 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' ) -- cgit v1.2.3