diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-03-02 01:33:53 +0100 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-03-03 21:38:46 +0100 |
commit | 37621846d541d7afcad52e6dba8281bd1146cf09 (patch) | |
tree | 6b361c8f3b9e9d7d95c5324e08d030a555ecf3fc /tests | |
parent | aecf1b73d9c9d653220b6d8825d5eacd2cb6bd74 (diff) | |
download | factory-boy-37621846d541d7afcad52e6dba8281bd1146cf09.tar factory-boy-37621846d541d7afcad52e6dba8281bd1146cf09.tar.gz |
Deprecate the extract_prefix option to PostGeneration.
Introduces a new, call-less syntax for the @post_generation decorator.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_declarations.py | 96 | ||||
-rw-r--r-- | tests/test_using.py | 21 |
2 files changed, 115 insertions, 2 deletions
diff --git a/tests/test_declarations.py b/tests/test_declarations.py index ecec244..ef7cc0f 100644 --- a/tests/test_declarations.py +++ b/tests/test_declarations.py @@ -122,6 +122,7 @@ 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') @@ -130,6 +131,100 @@ class PostGenerationDeclarationTestCase(unittest.TestCase): 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 + 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_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): def test_lazyness(self): @@ -174,7 +269,6 @@ class CircularSubFactoryTestCase(unittest.TestCase): self.assertIn('CircularSubFactory', str(w[0].message)) self.assertIn('deprecated', str(w[0].message)) - @tools.disable_warnings def test_lazyness(self): f = declarations.CircularSubFactory('factory.declarations', 'Sequence', x=3) diff --git a/tests/test_using.py b/tests/test_using.py index 2bab8c0..fd8befb 100644 --- a/tests/test_using.py +++ b/tests/test_using.py @@ -1173,6 +1173,25 @@ class PostGenerationTestCase(unittest.TestCase): 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')) + + @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 @@ -1191,7 +1210,7 @@ class PostGenerationTestCase(unittest.TestCase): one = 1 - @factory.post_generation() + @factory.post_generation def incr_one(self, _create, increment=1): self.one += increment |