summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2012-01-06 23:43:52 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2012-01-06 23:43:52 +0100
commitbcc1ad6fdce458ae3b24270789ab60ddd2425c4a (patch)
treeba73ba9cc16e6bffed9f843500a23c1ddb3013c7
parent8563a98022b8b3e855bf036a57aad4fd86319eff (diff)
downloadfactory-boy-bcc1ad6fdce458ae3b24270789ab60ddd2425c4a.tar
factory-boy-bcc1ad6fdce458ae3b24270789ab60ddd2425c4a.tar.gz
Publish the make_factory method, and make sure that complex declarations work with it.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
-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):