summaryrefslogtreecommitdiff
path: root/factory
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2014-05-18 12:16:51 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2014-05-18 14:18:49 +0200
commit80eaa0c8711f2c3ca82eb7953db49c7c61bd9ffa (patch)
tree8bdfc502d8a231fdc32a414eee61da628670a727 /factory
parent69894fce7977ea55f8cc3ad141840bab49330859 (diff)
downloadfactory-boy-80eaa0c8711f2c3ca82eb7953db49c7c61bd9ffa.tar
factory-boy-80eaa0c8711f2c3ca82eb7953db49c7c61bd9ffa.tar.gz
factory.django: Fix counter inheritance with abstract models.
Diffstat (limited to 'factory')
-rw-r--r--factory/base.py19
-rw-r--r--factory/django.py16
2 files changed, 24 insertions, 11 deletions
diff --git a/factory/base.py b/factory/base.py
index cc1fb57..d255dbf 100644
--- a/factory/base.py
+++ b/factory/base.py
@@ -218,13 +218,7 @@ class FactoryOptions(object):
if self.target is None:
self.abstract = True
- if (self.target is not None
- and self.base_factory is not None
- and self.base_factory._meta.target is not None
- and issubclass(self.target, base_factory._meta.target)):
- self.counter_reference = self.base_factory
- else:
- self.counter_reference = self.factory
+ self.counter_reference = self._get_counter_reference()
for parent in self.factory.__mro__[1:]:
if not hasattr(parent, '_meta'):
@@ -238,6 +232,17 @@ class FactoryOptions(object):
if self._is_postgen_declaration(k, v):
self.postgen_declarations[k] = v
+ def _get_counter_reference(self):
+ """Identify which factory should be used for a shared counter."""
+
+ if (self.target is not None
+ and self.base_factory is not None
+ and self.base_factory._meta.target is not None
+ and issubclass(self.target, self.base_factory._meta.target)):
+ return self.base_factory
+ else:
+ return self.factory
+
def _is_declaration(self, name, value):
"""Determines if a class attribute is a field value declaration.
diff --git a/factory/django.py b/factory/django.py
index 9a4e07a..77afd8c 100644
--- a/factory/django.py
+++ b/factory/django.py
@@ -58,6 +58,18 @@ class DjangoOptions(base.FactoryOptions):
base.OptionDefault('django_get_or_create', (), inherit=True),
]
+ def _get_counter_reference(self):
+ counter_reference = super(DjangoOptions, self)._get_counter_reference()
+ if (counter_reference == self.base_factory
+ and self.base_factory._meta.target is not None
+ and self.base_factory._meta.target._meta.abstract
+ and self.target is not None
+ and not self.target._meta.abstract):
+ # Target factory is for an abstract model, yet we're for another,
+ # concrete subclass => don't reuse the counter.
+ return self.factory
+ return counter_reference
+
class DjangoModelFactory(base.Factory):
"""Factory for Django models.
@@ -72,10 +84,6 @@ class DjangoModelFactory(base.Factory):
class Meta:
abstract = True # Optional, but explicit.
- @classmethod
- def _get_blank_options(cls):
- return DjangoOptions()
-
_OLDSTYLE_ATTRIBUTES = base.Factory._OLDSTYLE_ATTRIBUTES.copy()
_OLDSTYLE_ATTRIBUTES.update({
'FACTORY_DJANGO_GET_OR_CREATE': 'django_get_or_create',