summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--factory/base.py31
-rw-r--r--tests/test_base.py2
2 files changed, 21 insertions, 12 deletions
diff --git a/factory/base.py b/factory/base.py
index aef21d5..2418675 100644
--- a/factory/base.py
+++ b/factory/base.py
@@ -41,6 +41,22 @@ CLASS_ATTRIBUTE_POSTGEN_DECLARATIONS = '_postgen_declarations'
CLASS_ATTRIBUTE_ASSOCIATED_CLASS = '_associated_class'
+class FactoryError(Exception):
+ """Any exception raised by factory_boy."""
+
+
+class AssociatedClassError(FactoryError):
+ """Exception for Factory subclasses lacking FACTORY_FOR."""
+
+
+class UnknownStrategy(FactoryError):
+ """Raised when a factory uses an unknown strategy."""
+
+
+class UnsupportedStrategy(FactoryError):
+ """Raised when trying to use a strategy on an incompatible Factory."""
+
+
# Factory metaclasses
def get_factory_bases(bases):
@@ -64,7 +80,7 @@ class FactoryMetaClass(type):
elif cls.FACTORY_STRATEGY == STUB_STRATEGY:
return cls.stub(**kwargs)
else:
- raise BaseFactory.UnknownStrategy('Unknown FACTORY_STRATEGY: {0}'.format(cls.FACTORY_STRATEGY))
+ raise UnknownStrategy('Unknown FACTORY_STRATEGY: {0}'.format(cls.FACTORY_STRATEGY))
@classmethod
def _discover_associated_class(cls, class_name, attrs, inherited=None):
@@ -200,15 +216,12 @@ class FactoryMetaClass(type):
class BaseFactory(object):
"""Factory base support for sequences, attributes and stubs."""
- class UnknownStrategy(RuntimeError):
- pass
-
- class UnsupportedStrategy(RuntimeError):
- pass
+ UnknownStrategy = UnknownStrategy
+ UnsupportedStrategy = UnsupportedStrategy
def __new__(cls, *args, **kwargs):
"""Would be called if trying to instantiate the class."""
- raise RuntimeError('You cannot instantiate BaseFactory')
+ raise FactoryError('You cannot instantiate BaseFactory')
# ID to use for the next 'declarations.Sequence' attribute.
_next_sequence = None
@@ -431,10 +444,6 @@ class BaseFactory(object):
return cls.generate_batch(strategy, size, **kwargs)
-class AssociatedClassError(RuntimeError):
- pass
-
-
class Factory(BaseFactory):
"""Factory base with build and create support.
diff --git a/tests/test_base.py b/tests/test_base.py
index e12c0ae..fb3ba30 100644
--- a/tests/test_base.py
+++ b/tests/test_base.py
@@ -60,7 +60,7 @@ class TestModel(FakeDjangoModel):
class SafetyTestCase(unittest.TestCase):
def testBaseFactory(self):
- self.assertRaises(RuntimeError, base.BaseFactory)
+ self.assertRaises(base.FactoryError, base.BaseFactory)
class FactoryTestCase(unittest.TestCase):