diff options
author | Raphaël Barrois <raphael.barrois@polyconseil.fr> | 2011-08-10 19:58:01 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polyconseil.fr> | 2011-08-10 19:58:01 +0200 |
commit | bbe78f8b43ee92120072a7be8c3e5b24f9f8a940 (patch) | |
tree | 4cbef9b4e872218ca028bb157d826aa3e2f95970 | |
parent | 70e961169a14588d223e282387cdbab0cb8a404c (diff) | |
download | factory-boy-bbe78f8b43ee92120072a7be8c3e5b24f9f8a940.tar factory-boy-bbe78f8b43ee92120072a7be8c3e5b24f9f8a940.tar.gz |
Use the same sequence counter for parent and child classes.
Signed-off-by: Raphaël Barrois <raphael.barrois@polyconseil.fr>
-rw-r--r-- | factory/base.py | 16 | ||||
-rw-r--r-- | factory/declarations.py | 5 | ||||
-rw-r--r-- | factory/test_base.py | 15 |
3 files changed, 30 insertions, 6 deletions
diff --git a/factory/base.py b/factory/base.py index cfbfae8..61bf07e 100644 --- a/factory/base.py +++ b/factory/base.py @@ -123,12 +123,14 @@ class FactoryMetaClass(BaseFactoryMetaClass): if own_associated_class is None and inherited_associated_class is not None: own_associated_class = inherited_associated_class + attrs['_base_factory'] = base - if not own_associated_class and used_auto_discovery: - format_args = FACTORY_CLASS_DECLARATION, associated_class_name, class_name, factory_module - raise Factory.AssociatedClassError(FactoryMetaClass.ERROR_MESSAGE_AUTODISCOVERY.format(*format_args)) - elif not own_associated_class: - raise Factory.AssociatedClassError(FactoryMetaClass.ERROR_MESSAGE.format(FACTORY_CLASS_DECLARATION)) + if not own_associated_class: + if used_auto_discovery: + format_args = FACTORY_CLASS_DECLARATION, associated_class_name, class_name, factory_module + raise Factory.AssociatedClassError(FactoryMetaClass.ERROR_MESSAGE_AUTODISCOVERY.format(*format_args)) + else: + raise Factory.AssociatedClassError(FactoryMetaClass.ERROR_MESSAGE.format(FACTORY_CLASS_DECLARATION)) extra_attrs = {CLASS_ATTRIBUTE_ASSOCIATED_CLASS: own_associated_class} return super(FactoryMetaClass, cls).__new__(cls, class_name, bases, attrs, extra_attrs=extra_attrs) @@ -145,13 +147,17 @@ class BaseFactory(object): raise RuntimeError('You cannot instantiate BaseFactory') _next_sequence = None + _base_factory = None @classmethod def _setup_next_sequence(cls): + """Set up an initial sequence value for Sequence attributes.""" return 0 @classmethod def _generate_next_sequence(cls): + if cls._base_factory: + return cls._base_factory._generate_next_sequence() if cls._next_sequence is None: cls._next_sequence = cls._setup_next_sequence() next_sequence = cls._next_sequence diff --git a/factory/declarations.py b/factory/declarations.py index d827f30..b930b4d 100644 --- a/factory/declarations.py +++ b/factory/declarations.py @@ -23,7 +23,10 @@ import threading global_counter_lock = threading.Lock() class GlobalCounter(object): - """A simple global counter.""" + """A simple global counter. + + It is used to order the various OrderedDeclaration together. + """ _value = 0 diff --git a/factory/test_base.py b/factory/test_base.py index 2b8cb14..c8d464b 100644 --- a/factory/test_base.py +++ b/factory/test_base.py @@ -219,6 +219,21 @@ class FactoryTestCase(unittest.TestCase): self.assertEqual(test_object.three, 'three') self.assertEqual(test_object.four, 'three four') + def testInheritanceWithSequence(self): + """Tests that sequence IDs are shared between parent and son.""" + class TestObjectFactory(Factory): + one = declarations.Sequence(lambda a: a) + + class TestSubFactory(TestObjectFactory): + pass + + parent = TestObjectFactory.build() + sub = TestSubFactory.build() + alt_parent = TestObjectFactory.build() + alt_sub = TestSubFactory.build() + ones = set([x.one for x in (parent, alt_parent, sub, alt_sub)]) + self.assertEqual(4, len(ones)) + def testSetCreationFunction(self): def creation_function(class_to_create, **kwargs): return "This doesn't even return an instance of {0}".format(class_to_create.__name__) |