summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2012-02-24 23:31:02 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2012-02-24 23:31:02 +0100
commitcbbe5cc359412c8e6c49e06d5d1f35680ad88c40 (patch)
tree0615f9a4ef481b77cd436b44047b81f07cd2a72e
parent86cd8c2472dff6fe62e3c1f83bd9a1a3523ce08e (diff)
downloadfactory-boy-cbbe5cc359412c8e6c49e06d5d1f35680ad88c40.tar
factory-boy-cbbe5cc359412c8e6c49e06d5d1f35680ad88c40.tar.gz
Add support for 'generate' and 'simple_generate'
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
-rw-r--r--factory/__init__.py4
-rw-r--r--factory/base.py85
-rw-r--r--tests/test_using.py195
3 files changed, 284 insertions, 0 deletions
diff --git a/factory/__init__.py b/factory/__init__.py
index 990f39f..cf9cc3e 100644
--- a/factory/__init__.py
+++ b/factory/__init__.py
@@ -31,11 +31,15 @@ from base import (
build,
create,
stub,
+ generate,
+ simple_generate,
make_factory,
build_batch,
create_batch,
stub_batch,
+ generate_batch,
+ simple_generate_batch,
BUILD_STRATEGY,
CREATE_STRATEGY,
diff --git a/factory/base.py b/factory/base.py
index 99cf49d..62131fb 100644
--- a/factory/base.py
+++ b/factory/base.py
@@ -361,6 +361,72 @@ class BaseFactory(object):
"""
return [cls.stub(**kwargs) for _ in xrange(size)]
+ @classmethod
+ def generate(cls, strategy, **kwargs):
+ """Generate a new instance.
+
+ The instance will be created with the given strategy (one of
+ BUILD_STRATEGY, CREATE_STRATEGY, STUB_STRATEGY).
+
+ Args:
+ strategy (str): the strategy to use for generating the instance.
+
+ Returns:
+ object: the generated instance
+ """
+ assert strategy in (STUB_STRATEGY, BUILD_STRATEGY, CREATE_STRATEGY)
+ action = getattr(cls, strategy)
+ return action(**kwargs)
+
+ @classmethod
+ def generate_batch(cls, strategy, size, **kwargs):
+ """Generate a batch of instances.
+
+ The instances will be created with the given strategy (one of
+ BUILD_STRATEGY, CREATE_STRATEGY, STUB_STRATEGY).
+
+ Args:
+ strategy (str): the strategy to use for generating the instance.
+ size (int): the number of instances to generate
+
+ Returns:
+ object list: the generated instances
+ """
+ assert strategy in (STUB_STRATEGY, BUILD_STRATEGY, CREATE_STRATEGY)
+ batch_action = getattr(cls, '%s_batch' % strategy)
+ return batch_action(size, **kwargs)
+
+ @classmethod
+ def simple_generate(cls, create, **kwargs):
+ """Generate a new instance.
+
+ The instance will be either 'built' or 'created'.
+
+ Args:
+ create (bool): whether to 'build' or 'create' the instance.
+
+ Returns:
+ object: the generated instance
+ """
+ strategy = CREATE_STRATEGY if create else BUILD_STRATEGY
+ return cls.generate(strategy, **kwargs)
+
+ @classmethod
+ def simple_generate_batch(cls, create, size, **kwargs):
+ """Generate a batch of instances.
+
+ These instances will be either 'built' or 'created'.
+
+ Args:
+ size (int): the number of instances to generate
+ create (bool): whether to 'build' or 'create' the instances.
+
+ Returns:
+ object list: the generated instances
+ """
+ strategy = CREATE_STRATEGY if create else BUILD_STRATEGY
+ return cls.generate_batch(strategy, size, **kwargs)
+
class StubFactory(BaseFactory):
__metaclass__ = BaseFactoryMetaClass
@@ -529,3 +595,22 @@ def stub_batch(klass, size, **kwargs):
"""Create a factory for the given class, and stub a batch of instances."""
return make_factory(klass, **kwargs).stub_batch(size)
+
+def generate(klass, strategy, **kwargs):
+ """Create a factory for the given class, and generate an instance."""
+ return make_factory(klass, **kwargs).generate(strategy)
+
+
+def generate_batch(klass, strategy, size, **kwargs):
+ """Create a factory for the given class, and generate instances."""
+ return make_factory(klass, **kwargs).generate_batch(strategy, size)
+
+
+def simple_generate(klass, create, **kwargs):
+ """Create a factory for the given class, and simple_generate an instance."""
+ return make_factory(klass, **kwargs).simple_generate(create)
+
+
+def simple_generate_batch(klass, create, size, **kwargs):
+ """Create a factory for the given class, and simple_generate instances."""
+ return make_factory(klass, **kwargs).simple_generate_batch(create, size)
diff --git a/tests/test_using.py b/tests/test_using.py
index 7ed94e2..4e69212 100644
--- a/tests/test_using.py
+++ b/tests/test_using.py
@@ -102,6 +102,91 @@ class SimpleBuildTestCase(unittest.TestCase):
self.assertEqual(obj.three, 3)
self.assertFalse(hasattr(obj, 'two'))
+ def test_stub_batch(self):
+ objs = factory.stub_batch(FakeDjangoModel, 4, foo='bar')
+
+ self.assertEqual(4, len(objs))
+ self.assertEqual(4, len(set(objs)))
+
+ for obj in objs:
+ self.assertFalse(hasattr(obj, 'id'))
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_generate_build(self):
+ obj = factory.generate(FakeDjangoModel, factory.BUILD_STRATEGY, foo='bar')
+ self.assertEqual(obj.id, None)
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_generate_create(self):
+ obj = factory.generate(FakeDjangoModel, factory.CREATE_STRATEGY, foo='bar')
+ self.assertEqual(obj.id, 1)
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_generate_stub(self):
+ obj = factory.generate(FakeDjangoModel, factory.STUB_STRATEGY, foo='bar')
+ self.assertFalse(hasattr(obj, 'id'))
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_generate_batch_build(self):
+ objs = factory.generate_batch(FakeDjangoModel, factory.BUILD_STRATEGY, 20, foo='bar')
+
+ self.assertEqual(20, len(objs))
+ self.assertEqual(20, len(set(objs)))
+
+ for obj in objs:
+ self.assertEqual(obj.id, None)
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_generate_batch_create(self):
+ objs = factory.generate_batch(FakeDjangoModel, factory.CREATE_STRATEGY, 20, foo='bar')
+
+ self.assertEqual(20, len(objs))
+ self.assertEqual(20, len(set(objs)))
+
+ for obj in objs:
+ self.assertEqual(obj.id, 1)
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_generate_batch_stub(self):
+ objs = factory.generate_batch(FakeDjangoModel, factory.STUB_STRATEGY, 20, foo='bar')
+
+ self.assertEqual(20, len(objs))
+ self.assertEqual(20, len(set(objs)))
+
+ for obj in objs:
+ self.assertFalse(hasattr(obj, 'id'))
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_simple_generate_build(self):
+ obj = factory.simple_generate(FakeDjangoModel, False, foo='bar')
+ self.assertEqual(obj.id, None)
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_simple_generate_create(self):
+ obj = factory.simple_generate(FakeDjangoModel, True, foo='bar')
+ self.assertEqual(obj.id, 1)
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_simple_generate_batch_build(self):
+ objs = factory.simple_generate_batch(FakeDjangoModel, False, 20, foo='bar')
+
+ self.assertEqual(20, len(objs))
+ self.assertEqual(20, len(set(objs)))
+
+ for obj in objs:
+ self.assertEqual(obj.id, None)
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_simple_generate_batch_create(self):
+ objs = factory.simple_generate_batch(FakeDjangoModel, True, 20, foo='bar')
+
+ self.assertEqual(20, len(objs))
+ self.assertEqual(20, len(set(objs)))
+
+ for obj in objs:
+ self.assertEqual(obj.id, 1)
+ self.assertEqual(obj.foo, 'bar')
+
def test_make_factory(self):
fact = factory.make_factory(TestObject, two=2, three=factory.LazyAttribute(lambda o: o.two + 1))
@@ -275,6 +360,116 @@ class FactoryTestCase(unittest.TestCase):
self.assertEqual(i, obj.two)
self.assertTrue(obj.id)
+ def test_generate_build(self):
+ class TestModelFactory(factory.Factory):
+ one = 'one'
+
+ test_model = TestModelFactory.generate(factory.BUILD_STRATEGY)
+ self.assertEqual(test_model.one, 'one')
+ self.assertFalse(test_model.id)
+
+ def test_generate_create(self):
+ class TestModelFactory(factory.Factory):
+ one = 'one'
+
+ test_model = TestModelFactory.generate(factory.CREATE_STRATEGY)
+ self.assertEqual(test_model.one, 'one')
+ self.assertTrue(test_model.id)
+
+ def test_generate_stub(self):
+ class TestModelFactory(factory.Factory):
+ one = 'one'
+
+ test_model = TestModelFactory.generate(factory.STUB_STRATEGY)
+ self.assertEqual(test_model.one, 'one')
+ self.assertFalse(hasattr(test_model, 'id'))
+
+ def test_generate_batch_build(self):
+ class TestModelFactory(factory.Factory):
+ one = 'one'
+
+ objs = TestModelFactory.generate_batch(factory.BUILD_STRATEGY, 20, two='two')
+
+ self.assertEqual(20, len(objs))
+ self.assertEqual(20, len(set(objs)))
+
+ for i, obj in enumerate(objs):
+ self.assertEqual('one', obj.one)
+ self.assertEqual('two', obj.two)
+ self.assertFalse(obj.id)
+
+ def test_generate_batch_create(self):
+ class TestModelFactory(factory.Factory):
+ one = 'one'
+
+ objs = TestModelFactory.generate_batch(factory.CREATE_STRATEGY, 20, two='two')
+
+ self.assertEqual(20, len(objs))
+ self.assertEqual(20, len(set(objs)))
+
+ for i, obj in enumerate(objs):
+ self.assertEqual('one', obj.one)
+ self.assertEqual('two', obj.two)
+ self.assertTrue(obj.id)
+
+ def test_generate_batch_stub(self):
+ class TestModelFactory(factory.Factory):
+ one = 'one'
+
+ objs = TestModelFactory.generate_batch(factory.STUB_STRATEGY, 20, two='two')
+
+ self.assertEqual(20, len(objs))
+ self.assertEqual(20, len(set(objs)))
+
+ for i, obj in enumerate(objs):
+ self.assertEqual('one', obj.one)
+ self.assertEqual('two', obj.two)
+ self.assertFalse(hasattr(obj, 'id'))
+
+ def test_simple_generate_build(self):
+ class TestModelFactory(factory.Factory):
+ one = 'one'
+
+ test_model = TestModelFactory.simple_generate(False)
+ self.assertEqual(test_model.one, 'one')
+ self.assertFalse(test_model.id)
+
+ def test_simple_generate_create(self):
+ class TestModelFactory(factory.Factory):
+ one = 'one'
+
+ test_model = TestModelFactory.simple_generate(True)
+ self.assertEqual(test_model.one, 'one')
+ self.assertTrue(test_model.id)
+
+ def test_simple_generate_batch_build(self):
+ class TestModelFactory(factory.Factory):
+ one = 'one'
+
+ objs = TestModelFactory.simple_generate_batch(False, 20, two='two')
+
+ self.assertEqual(20, len(objs))
+ self.assertEqual(20, len(set(objs)))
+
+ for i, obj in enumerate(objs):
+ self.assertEqual('one', obj.one)
+ self.assertEqual('two', obj.two)
+ self.assertFalse(obj.id)
+
+ def test_simple_generate_batch_create(self):
+ class TestModelFactory(factory.Factory):
+ one = 'one'
+
+ objs = TestModelFactory.simple_generate_batch(True, 20, two='two')
+
+ self.assertEqual(20, len(objs))
+ self.assertEqual(20, len(set(objs)))
+
+ for i, obj in enumerate(objs):
+ self.assertEqual('one', obj.one)
+ self.assertEqual('two', obj.two)
+ self.assertTrue(obj.id)
+
def test_stub_batch(self):
class TestObjectFactory(factory.Factory):
one = 'one'