diff options
Diffstat (limited to 'factory/base.py')
-rw-r--r-- | factory/base.py | 54 |
1 files changed, 54 insertions, 0 deletions
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) + |