diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-03-11 22:17:57 +0100 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-03-11 22:22:41 +0100 |
commit | f4100b373418a58dba7ff4f29cfb44df4eca3d15 (patch) | |
tree | d0efb5073048bf1f18ab59b5cc7ae4ebcfc51492 | |
parent | e8327fcb2e31dd7a9ffc7f53c7a678d1c1135cb2 (diff) | |
download | factory-boy-f4100b373418a58dba7ff4f29cfb44df4eca3d15.tar factory-boy-f4100b373418a58dba7ff4f29cfb44df4eca3d15.tar.gz |
Remove automagic associated class discovery.
-rw-r--r-- | factory/base.py | 36 | ||||
-rw-r--r-- | tests/test_base.py | 33 |
2 files changed, 5 insertions, 64 deletions
diff --git a/factory/base.py b/factory/base.py index 06730ba..8bb6d95 100644 --- a/factory/base.py +++ b/factory/base.py @@ -151,7 +151,6 @@ class FactoryMetaClass(BaseFactoryMetaClass): to a class. """ own_associated_class = None - used_auto_discovery = False if FACTORY_CLASS_DECLARATION in attrs: return attrs[FACTORY_CLASS_DECLARATION] @@ -161,37 +160,10 @@ class FactoryMetaClass(BaseFactoryMetaClass): if inherited is not None: return inherited - if '__module__' in attrs: - factory_module = sys.modules[attrs['__module__']] - if class_name.endswith('Factory'): - # Try a module lookup - used_auto_discovery = True - associated_name = class_name[:-len('Factory')] - if associated_name and hasattr(factory_module, associated_name): - warnings.warn( - "Auto-discovery of associated class is deprecated, and " - "will be removed in the future. Please set '%s = %s' " - "in the %s class definition." % ( - FACTORY_CLASS_DECLARATION, - associated_name, - class_name, - ), DeprecationWarning, 3) - - return getattr(factory_module, associated_name) - - # Unable to guess a good option; return the inherited class. - # Unable to find an associated class; fail. - if used_auto_discovery: - raise Factory.AssociatedClassError( - FactoryMetaClass.ERROR_MESSAGE_AUTODISCOVERY.format( - FACTORY_CLASS_DECLARATION, - associated_name, - class_name, - factory_module,)) - else: - raise Factory.AssociatedClassError( - FactoryMetaClass.ERROR_MESSAGE.format( - FACTORY_CLASS_DECLARATION)) + raise Factory.AssociatedClassError( + "Could not determine the class associated with %s. " + "Use the FACTORY_FOR attribute to specify an associated class." % + class_name) def __new__(cls, class_name, bases, attrs): """Determine the associated class based on the factory class name. Record the associated class diff --git a/tests/test_base.py b/tests/test_base.py index 6f16e8f..e86eae3 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -189,29 +189,6 @@ class FactoryCreationTestCase(unittest.TestCase): self.assertTrue(isinstance(TestFactory.build(), TestObject)) - def testAutomaticAssociatedClassDiscovery(self): - with warnings.catch_warnings(): - warnings.simplefilter('ignore') - class TestObjectFactory(base.Factory): - pass - - self.assertTrue(isinstance(TestObjectFactory.build(), TestObject)) - - def testDeprecationWarning(self): - """Make sure the 'auto-discovery' deprecation warning is issued.""" - - with warnings.catch_warnings(record=True) as w: - # Clear the warning registry. - __warningregistry__.clear() - - warnings.simplefilter('always') - class TestObjectFactory(base.Factory): - pass - - self.assertEqual(1, len(w)) - self.assertIn('discovery', str(w[0].message)) - self.assertIn('deprecated', str(w[0].message)) - def testStub(self): class TestFactory(base.StubFactory): pass @@ -250,15 +227,7 @@ class FactoryCreationTestCase(unittest.TestCase): # Errors - def testNoAssociatedClassWithAutodiscovery(self): - try: - class TestFactory(base.Factory): - pass - self.fail() - except base.Factory.AssociatedClassError as e: - self.assertTrue('autodiscovery' in str(e)) - - def testNoAssociatedClassWithoutAutodiscovery(self): + def test_no_associated_class(self): try: class Test(base.Factory): pass |