diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-03-15 01:39:57 +0100 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-03-15 01:39:57 +0100 |
commit | efc0ca41dcec074176064faf1e899ea275bb2901 (patch) | |
tree | 0c69c443ae2022f8a7ca2f1df786e094ae9c15e7 | |
parent | 6e9bf5af909e1e164a294fd5589edc4fada06731 (diff) | |
download | factory-boy-efc0ca41dcec074176064faf1e899ea275bb2901.tar factory-boy-efc0ca41dcec074176064faf1e899ea275bb2901.tar.gz |
Fix exception hierarchy.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
-rw-r--r-- | factory/base.py | 31 | ||||
-rw-r--r-- | tests/test_base.py | 2 |
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): |