summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--factory/__init__.py1
-rw-r--r--factory/base.py12
-rw-r--r--tests/test_base.py22
3 files changed, 31 insertions, 4 deletions
diff --git a/factory/__init__.py b/factory/__init__.py
index f2652f2..b673162 100644
--- a/factory/__init__.py
+++ b/factory/__init__.py
@@ -31,6 +31,7 @@ from base import (
build,
create,
stub,
+ make_factory,
BUILD_STRATEGY,
CREATE_STRATEGY,
diff --git a/factory/base.py b/factory/base.py
index 80c49ca..19d441b 100644
--- a/factory/base.py
+++ b/factory/base.py
@@ -442,7 +442,8 @@ class DjangoModelFactory(Factory):
return 1
-def _make_factory(klass, **kwargs):
+def make_factory(klass, **kwargs):
+ """Create a new, simple factory for the given class."""
factory_name = '%sFactory' % klass.__name__
kwargs[FACTORY_CLASS_DECLARATION] = klass
factory_class = type(Factory).__new__(type(Factory), factory_name, (Factory,), kwargs)
@@ -452,10 +453,13 @@ def _make_factory(klass, **kwargs):
def build(klass, **kwargs):
- return _make_factory(klass, **kwargs).build()
+ """Create a factory for the given class, and build an instance."""
+ return make_factory(klass, **kwargs).build()
def create(klass, **kwargs):
- return _make_factory(klass, **kwargs).create()
+ """Create a factory for the given class, and create an instance."""
+ return make_factory(klass, **kwargs).create()
def stub(klass, **kwargs):
- return _make_factory(klass, **kwargs).stub()
+ """Create a factory for the given class, and stub an instance."""
+ return make_factory(klass, **kwargs).stub()
diff --git a/tests/test_base.py b/tests/test_base.py
index 5855682..b445cb3 100644
--- a/tests/test_base.py
+++ b/tests/test_base.py
@@ -65,6 +65,13 @@ class SimpleBuildTestCase(unittest.TestCase):
self.assertEqual(obj.three, None)
self.assertEqual(obj.four, None)
+ def test_complex(self):
+ obj = base.build(TestObject, two=2, three=declarations.LazyAttribute(lambda o: o.two + 1))
+ self.assertEqual(obj.one, None)
+ self.assertEqual(obj.two, 2)
+ self.assertEqual(obj.three, 3)
+ self.assertEqual(obj.four, None)
+
def test_create(self):
obj = base.create(FakeDjangoModel, foo='bar')
self.assertEqual(obj.id, 1)
@@ -75,6 +82,21 @@ class SimpleBuildTestCase(unittest.TestCase):
self.assertEqual(obj.three, 3)
self.assertFalse(hasattr(obj, 'two'))
+ def test_make_factory(self):
+ fact = base.make_factory(TestObject, two=2, three=declarations.LazyAttribute(lambda o: o.two + 1))
+
+ obj = fact.build()
+ self.assertEqual(obj.one, None)
+ self.assertEqual(obj.two, 2)
+ self.assertEqual(obj.three, 3)
+ self.assertEqual(obj.four, None)
+
+ obj = fact.build(two=4)
+ self.assertEqual(obj.one, None)
+ self.assertEqual(obj.two, 4)
+ self.assertEqual(obj.three, 5)
+ self.assertEqual(obj.four, None)
+
class FactoryTestCase(unittest.TestCase):
def testAttribute(self):