diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-03-11 22:36:30 +0100 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-03-11 22:36:30 +0100 |
commit | 60f0969406bd349a8a8b88fcaec819fa5c0525cb (patch) | |
tree | 6cf85cd478335d196d87bf4051986e007abc05a4 | |
parent | 16e1a65f5b93615d946b74e3fb4d0b61c99ae0d2 (diff) | |
download | factory-boy-60f0969406bd349a8a8b88fcaec819fa5c0525cb.tar factory-boy-60f0969406bd349a8a8b88fcaec819fa5c0525cb.tar.gz |
Remove extract_prefix from post-generation hooks.
Magic abuse is bad.
-rw-r--r-- | docs/changelog.rst | 1 | ||||
-rw-r--r-- | docs/reference.rst | 16 | ||||
-rw-r--r-- | factory/declarations.py | 53 | ||||
-rw-r--r-- | tests/test_declarations.py | 85 | ||||
-rw-r--r-- | tests/test_using.py | 19 |
5 files changed, 13 insertions, 161 deletions
diff --git a/docs/changelog.rst b/docs/changelog.rst index 518ab9e..b73e33b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -17,6 +17,7 @@ ChangeLog - Remove associated class discovery - Remove :class:`~factory.InfiniteIterator` and :func:`~factory.infinite_iterator` - Remove :class:`~factory.CircularSubFactory` + - Remove ``extract_prefix`` kwarg to post-generation hooks. - Stop defaulting to Django's ``Foo.objects.create()`` when "creating" instances - Remove STRATEGY_* - Remove :meth:`~factory.Factory.set_building_function` / :meth:`~factory.Factory.set_creation_function` diff --git a/docs/reference.rst b/docs/reference.rst index 955d3c5..d5b14a9 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -944,10 +944,6 @@ has been generated. Its sole argument is a callable, that will be called once the base object has been generated. -.. note:: Previous versions of factory_boy supported an extra ``extract_prefix`` - argument, to use an alternate argument prefix. - This feature is deprecated in 1.3.0 and will be removed in 2.0.0. - Once the base object has been generated, the provided callable will be called as ``callable(obj, create, extracted, **kwargs)``, where: @@ -973,7 +969,7 @@ as ``callable(obj, create, extracted, **kwargs)``, where: Decorator ~~~~~~~~~ -.. function:: post_generation(extract_prefix=None) +.. function:: post_generation A decorator is also provided, decorating a single method accepting the same ``obj``, ``created``, ``extracted`` and keyword arguments as :class:`PostGeneration`. @@ -1007,7 +1003,7 @@ A decorator is also provided, decorating a single method accepting the same PostGenerationMethodCall """""""""""""""""""""""" -.. class:: PostGenerationMethodCall(method_name, extract_prefix=None, *args, **kwargs) +.. class:: PostGenerationMethodCall(method_name, *args, **kwargs) .. OHAI_VIM* @@ -1020,14 +1016,6 @@ PostGenerationMethodCall The name of the method to call on the :attr:`~Factory.FACTORY_FOR` object - .. attribute:: extract_prefix - - If a string, the keyword argument prefix by which the field will get its - overriding arguments. If ``None``, defaults to the name of the attribute. - - .. deprecated:: 1.3.0 - Will be removed in 2.0.0 - .. attribute:: args The default set of unnamed arguments to pass to the method given in diff --git a/factory/declarations.py b/factory/declarations.py index b3c9d6a..b491bfb 100644 --- a/factory/declarations.py +++ b/factory/declarations.py @@ -326,21 +326,7 @@ class SubFactory(ParameteredAttribute): class PostGenerationDeclaration(object): - """Declarations to be called once the target object has been generated. - - Attributes: - extract_prefix (str): prefix to use when extracting attributes from - the factory's declaration for this declaration. If empty, uses - the attribute name of the PostGenerationDeclaration. - """ - - def __init__(self, extract_prefix=None): - if extract_prefix: - warnings.warn( - "The extract_prefix argument to PostGeneration declarations " - "is deprecated and will be removed in the future.", - PendingDeprecationWarning, 3) - self.extract_prefix = extract_prefix + """Declarations to be called once the target object has been generated.""" def extract(self, name, attrs): """Extract relevant attributes from a dict. @@ -355,12 +341,8 @@ class PostGenerationDeclaration(object): (object, dict): a tuple containing the attribute at 'name' (if provided) and a dict of extracted attributes """ - if self.extract_prefix: - extract_prefix = self.extract_prefix - else: - extract_prefix = name - extracted = attrs.pop(extract_prefix, None) - kwargs = utils.extract_dict(extract_prefix, attrs) + extracted = attrs.pop(name, None) + kwargs = utils.extract_dict(name, attrs) return extracted, kwargs def call(self, obj, create, extracted=None, **kwargs): # pragma: no cover @@ -369,7 +351,7 @@ class PostGenerationDeclaration(object): Args: obj (object): the newly generated object create (bool): whether the object was 'built' or 'created' - extracted (object): the value given for <extract_prefix> in the + extracted (object): the value given for <name> in the object definition, or None if not provided. kwargs (dict): declarations extracted from the object definition for this hook @@ -379,30 +361,16 @@ class PostGenerationDeclaration(object): class PostGeneration(PostGenerationDeclaration): """Calls a given function once the object has been generated.""" - def __init__(self, function, extract_prefix=None): - super(PostGeneration, self).__init__(extract_prefix) + def __init__(self, function): + super(PostGeneration, self).__init__() self.function = function def call(self, obj, create, extracted=None, **kwargs): return self.function(obj, create, extracted, **kwargs) -def post_generation(*args, **kwargs): - assert len(args) + len(kwargs) <= 1, "post_generation takes at most one argument." - if args and callable(args[0]): - # Called as @post_generation applied to a function - return PostGeneration(args[0]) - else: - warnings.warn( - "The @post_generation should now be applied directly to the " - "function, without parameters. The @post_generation() and " - "@post_generation(extract_prefix='xxx') syntaxes are deprecated " - "and will be removed in the future; use @post_generation instead.", - PendingDeprecationWarning, 2) - extract_prefix = kwargs.get('extract_prefix') - def decorator(fun): - return PostGeneration(fun, extract_prefix=extract_prefix) - return decorator +def post_generation(fun): + return PostGeneration(fun) class RelatedFactory(PostGenerationDeclaration): @@ -416,7 +384,7 @@ class RelatedFactory(PostGenerationDeclaration): """ def __init__(self, factory, name='', **defaults): - super(RelatedFactory, self).__init__(extract_prefix=None) + super(RelatedFactory, self).__init__() self.name = name self.defaults = defaults @@ -464,8 +432,7 @@ class PostGenerationMethodCall(PostGenerationDeclaration): password = factory.PostGenerationMethodCall('set_password', password='') """ def __init__(self, method_name, *args, **kwargs): - extract_prefix = kwargs.pop('extract_prefix', None) - super(PostGenerationMethodCall, self).__init__(extract_prefix) + super(PostGenerationMethodCall, self).__init__() self.method_name = method_name self.method_args = args self.method_kwargs = kwargs diff --git a/tests/test_declarations.py b/tests/test_declarations.py index ceadafa..b7ae344 100644 --- a/tests/test_declarations.py +++ b/tests/test_declarations.py @@ -122,26 +122,6 @@ class PostGenerationDeclarationTestCase(unittest.TestCase): self.assertEqual(extracted, 13) self.assertEqual(kwargs, {'bar': 42}) - @tools.disable_warnings - def test_extract_with_prefix(self): - decl = declarations.PostGenerationDeclaration(extract_prefix='blah') - - extracted, kwargs = decl.extract('foo', - {'foo': 13, 'foo__bar': 42, 'blah': 42, 'blah__baz': 1}) - self.assertEqual(extracted, 42) - self.assertEqual(kwargs, {'baz': 1}) - - def test_extract_prefix_deprecated(self): - with warnings.catch_warnings(record=True) as w: - __warningregistry__.clear() - - warnings.simplefilter('always') - declarations.PostGenerationDeclaration(extract_prefix='blah') - - self.assertEqual(1, len(w)) - self.assertIn('extract_prefix', str(w[0].message)) - self.assertIn('deprecated', str(w[0].message)) - def test_decorator_simple(self): call_params = [] @declarations.post_generation @@ -160,71 +140,6 @@ class PostGenerationDeclarationTestCase(unittest.TestCase): self.assertEqual((None, False, 13), call_params[0]) self.assertEqual({'bar': 42}, call_params[1]) - @tools.disable_warnings - def test_decorator_call_no_prefix(self): - call_params = [] - @declarations.post_generation() - def foo(*args, **kwargs): - call_params.append(args) - call_params.append(kwargs) - - extracted, kwargs = foo.extract('foo', - {'foo': 13, 'foo__bar': 42, 'blah': 42, 'blah__baz': 1}) - self.assertEqual(13, extracted) - self.assertEqual({'bar': 42}, kwargs) - - # No value returned. - foo.call(None, False, extracted, **kwargs) - self.assertEqual(2, len(call_params)) - self.assertEqual((None, False, 13), call_params[0]) - self.assertEqual({'bar': 42}, call_params[1]) - - @tools.disable_warnings - def test_decorator_extract_prefix(self): - call_params = [] - @declarations.post_generation(extract_prefix='blah') - def foo(*args, **kwargs): - call_params.append(args) - call_params.append(kwargs) - - extracted, kwargs = foo.extract('foo', - {'foo': 13, 'foo__bar': 42, 'blah': 42, 'blah__baz': 1}) - self.assertEqual(42, extracted) - self.assertEqual({'baz': 1}, kwargs) - - # No value returned. - foo.call(None, False, extracted, **kwargs) - self.assertEqual(2, len(call_params)) - self.assertEqual((None, False, 42), call_params[0]) - self.assertEqual({'baz': 1}, call_params[1]) - - def test_decorator_call_no_prefix_deprecated(self): - with warnings.catch_warnings(record=True) as w: - __warningregistry__.clear() - - warnings.simplefilter('always') - @declarations.post_generation() - def foo(*args, **kwargs): - pass - - self.assertEqual(1, len(w)) - self.assertIn('post_generation', str(w[0].message)) - self.assertIn('deprecated', str(w[0].message)) - - def test_decorator_call_with_prefix_deprecated(self): - with warnings.catch_warnings(record=True) as w: - __warningregistry__.clear() - - warnings.simplefilter('always') - @declarations.post_generation(extract_prefix='blah') - def foo(*args, **kwargs): - pass - - # 2 warnings: decorator with brackets, and extract_prefix. - self.assertEqual(2, len(w)) - self.assertIn('post_generation', str(w[0].message)) - self.assertIn('deprecated', str(w[0].message)) - class SubFactoryTestCase(unittest.TestCase): diff --git a/tests/test_using.py b/tests/test_using.py index b617668..a0b0db7 100644 --- a/tests/test_using.py +++ b/tests/test_using.py @@ -1218,25 +1218,6 @@ class PostGenerationTestCase(unittest.TestCase): self.assertFalse(obj.create) self.assertEqual({'incr_one': 42}, obj.results) - @tools.disable_warnings - def test_post_generation_calling(self): - class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - - one = 1 - - @factory.post_generation() - def incr_one(self, _create, _increment): - self.one += 1 - - obj = TestObjectFactory.build() - self.assertEqual(2, obj.one) - self.assertFalse(hasattr(obj, 'incr_one')) - - obj = TestObjectFactory.build(one=2) - self.assertEqual(3, obj.one) - self.assertFalse(hasattr(obj, 'incr_one')) - def test_post_generation_extraction(self): class TestObjectFactory(factory.Factory): FACTORY_FOR = TestObject |