diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2012-02-24 09:49:49 +0100 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2012-02-24 09:49:49 +0100 |
commit | 1643e56a6e4ca0e90621b9c39928b2c6bfb63063 (patch) | |
tree | 5f84199f71d3a3b5f4b03165a17496e21a4244b5 /factory | |
parent | 59274330932d5eae3b708c5e944a7ae452cdddaf (diff) | |
download | factory-boy-1643e56a6e4ca0e90621b9c39928b2c6bfb63063.tar factory-boy-1643e56a6e4ca0e90621b9c39928b2c6bfb63063.tar.gz |
Add batches.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
Diffstat (limited to 'factory')
-rw-r--r-- | factory/__init__.py | 4 | ||||
-rw-r--r-- | factory/base.py | 54 |
2 files changed, 58 insertions, 0 deletions
diff --git a/factory/__init__.py b/factory/__init__.py index e58dc51..4acd380 100644 --- a/factory/__init__.py +++ b/factory/__init__.py @@ -33,6 +33,10 @@ from base import ( stub, make_factory, + build_batch, + create_batch, + stub_batch, + BUILD_STRATEGY, CREATE_STRATEGY, STUB_STRATEGY, diff --git a/factory/base.py b/factory/base.py index c17b7ce..99cf49d 100644 --- a/factory/base.py +++ b/factory/base.py @@ -309,11 +309,35 @@ class BaseFactory(object): raise cls.UnsupportedStrategy() @classmethod + def build_batch(cls, size, **kwargs): + """Build a batch of instances of the given class, with overriden attrs. + + Args: + size (int): the number of instances to build + + Returns: + object list: the built instances + """ + return [cls.build(**kwargs) for _ in xrange(size)] + + @classmethod def create(cls, **kwargs): """Create an instance of the associated class, with overriden attrs.""" raise cls.UnsupportedStrategy() @classmethod + def create_batch(cls, size, **kwargs): + """Create a batch of instances of the given class, with overriden attrs. + + Args: + size (int): the number of instances to create + + Returns: + object list: the created instances + """ + return [cls.create(**kwargs) for _ in xrange(size)] + + @classmethod def stub(cls, **kwargs): """Retrieve a stub of the associated class, with overriden attrs. @@ -325,6 +349,18 @@ class BaseFactory(object): setattr(stub_object, name, value) return stub_object + @classmethod + def stub_batch(cls, size, **kwargs): + """Stub a batch of instances of the given class, with overriden attrs. + + Args: + size (int): the number of instances to stub + + Returns: + object list: the stubbed instances + """ + return [cls.stub(**kwargs) for _ in xrange(size)] + class StubFactory(BaseFactory): __metaclass__ = BaseFactoryMetaClass @@ -468,10 +504,28 @@ def build(klass, **kwargs): """Create a factory for the given class, and build an instance.""" return make_factory(klass, **kwargs).build() + +def build_batch(klass, size, **kwargs): + """Create a factory for the given class, and build a batch of instances.""" + return make_factory(klass, **kwargs).build_batch(size) + + def create(klass, **kwargs): """Create a factory for the given class, and create an instance.""" return make_factory(klass, **kwargs).create() + +def create_batch(klass, size, **kwargs): + """Create a factory for the given class, and create a batch of instances.""" + return make_factory(klass, **kwargs).create_batch(size) + + def stub(klass, **kwargs): """Create a factory for the given class, and stub an instance.""" return make_factory(klass, **kwargs).stub() + + +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) + |