diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2012-08-17 01:22:47 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2012-08-17 01:22:47 +0200 |
commit | 2611973bb62d38ff23471fdc115cdd09e351be50 (patch) | |
tree | 113cf3c40090b02d10f210ad20224cc40492f3dd /tests | |
parent | 20515351bb0db936873db5dae133ea317050079d (diff) | |
download | factory-boy-2611973bb62d38ff23471fdc115cdd09e351be50.tar factory-boy-2611973bb62d38ff23471fdc115cdd09e351be50.tar.gz |
Add support for passing non-kwarg parameters to factories.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_containers.py | 11 | ||||
-rw-r--r-- | tests/test_using.py | 49 |
2 files changed, 60 insertions, 0 deletions
diff --git a/tests/test_containers.py b/tests/test_containers.py index 797c480..b1ed6ed 100644 --- a/tests/test_containers.py +++ b/tests/test_containers.py @@ -178,6 +178,17 @@ class DeclarationDictTestCase(unittest.TestCase): self.assertEqual(set(['one', 'three']), set(d)) self.assertEqual(set([1, 3]), set(d.values())) + def test_update_with_public_ignores_factory_attributes(self): + """Ensure that a DeclarationDict ignores FACTORY_ keys.""" + d = containers.DeclarationDict() + d.update_with_public({ + 'one': 1, + 'FACTORY_FOR': 2, + 'FACTORY_ARG_PARAMETERS': 3, + }) + self.assertEqual(['one'], list(d)) + self.assertEqual([1], list(d.values())) + class AttributeBuilderTestCase(unittest.TestCase): def test_empty(self): diff --git a/tests/test_using.py b/tests/test_using.py index 8620127..7e141eb 100644 --- a/tests/test_using.py +++ b/tests/test_using.py @@ -675,6 +675,55 @@ class UsingFactoryTestCase(unittest.TestCase): self.assertEqual(TestObjectFactory.alt_create(foo=1), {"foo": 1}) +class NonKwargParametersTestCase(unittest.TestCase): + def test_build(self): + class TestObject(object): + def __init__(self, *args, **kwargs): + self.args = args + self.kwargs = kwargs + + class TestObjectFactory(factory.Factory): + FACTORY_FOR = TestObject + FACTORY_ARG_PARAMETERS = ('one', 'two',) + + one = 1 + two = 2 + three = 3 + + obj = TestObjectFactory.build() + self.assertEqual((1, 2), obj.args) + self.assertEqual({'three': 3}, obj.kwargs) + + def test_create(self): + class TestObject(object): + def __init__(self, *args, **kwargs): + self.args = None + self.kwargs = None + + @classmethod + def create(cls, *args, **kwargs): + inst = cls() + inst.args = args + inst.kwargs = kwargs + return inst + + class TestObjectFactory(factory.Factory): + FACTORY_FOR = TestObject + FACTORY_ARG_PARAMETERS = ('one', 'two') + + one = 1 + two = 2 + three = 3 + + @classmethod + def _create(cls, target_class, *args, **kwargs): + return target_class.create(*args, **kwargs) + + obj = TestObjectFactory.create() + self.assertEqual((1, 2), obj.args) + self.assertEqual({'three': 3}, obj.kwargs) + + class SubFactoryTestCase(unittest.TestCase): def testSubFactory(self): class TestModel2(FakeModel): |