summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-03-05 23:12:10 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-03-05 23:12:10 +0100
commit114ac649e448e97a210cf8dccc6ba50278645ce6 (patch)
tree705da01544a66d5602624d4d3813ac7ffef45382
parentd01f5e6c041395bc579f58e12e7034a08afe3f14 (diff)
downloadfactory-boy-114ac649e448e97a210cf8dccc6ba50278645ce6.tar
factory-boy-114ac649e448e97a210cf8dccc6ba50278645ce6.tar.gz
Stop calling Foo.objects.create() when it doesn't break (Closes #23).
This will be properly fixed in v2.0.0; the current heuristic is: - If the user defined a custom _create method, use it - If he didn't, but the associated class has a objects attribute, use TheClass.objects.create(*args, **kwargs) - Otherwise, simply call TheClass(*args, **kwargs). Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
-rw-r--r--docs/reference.rst9
-rw-r--r--factory/base.py3
-rw-r--r--tests/test_using.py15
3 files changed, 26 insertions, 1 deletions
diff --git a/docs/reference.rst b/docs/reference.rst
index 6d01e5d..03023fc 100644
--- a/docs/reference.rst
+++ b/docs/reference.rst
@@ -235,6 +235,15 @@ factory_boy supports two main strategies for generating instances, plus stubs.
.. OHAI_VIM*
+ .. warning:: For backward compatibility reasons, the default behaviour of
+ factory_boy is to call ``MyClass.objects.create(*args, **kwargs)``
+ when using the ``create`` strategy.
+
+ That policy will be used if the
+ :attr:`associated class <Factory.FACTORY_FOR>` has an ``objects``
+ attribute *and* the :meth:`~Factory._create` classmethod of the
+ :class:`Factory` wasn't overridden.
+
.. function:: use_strategy(strategy)
diff --git a/factory/base.py b/factory/base.py
index 3ebc746..44d58a7 100644
--- a/factory/base.py
+++ b/factory/base.py
@@ -527,7 +527,8 @@ class Factory(BaseFactory):
creation_function = getattr(cls, '_creation_function', ())
if creation_function and creation_function[0]:
return creation_function[0]
- elif cls._create.__func__ == Factory._create.__func__:
+ elif cls._create.__func__ == Factory._create.__func__ and \
+ hasattr(cls._associated_class, 'objects'):
# Backwards compatibility.
# Default creation_function and default _create() behavior.
# The best "Vanilla" _create detection algorithm I found is relying
diff --git a/tests/test_using.py b/tests/test_using.py
index 9bc466e..8f43813 100644
--- a/tests/test_using.py
+++ b/tests/test_using.py
@@ -314,6 +314,21 @@ class UsingFactoryTestCase(unittest.TestCase):
self.assertEqual(1, obj.id)
self.assertTrue(obj.properly_created)
+ def test_non_django_create(self):
+ class NonDjango(object):
+ def __init__(self, x, y=2):
+ self.x = x
+ self.y = y
+
+ class NonDjangoFactory(factory.Factory):
+ FACTORY_FOR = NonDjango
+
+ x = 3
+
+ obj = NonDjangoFactory.create()
+ self.assertEqual(3, obj.x)
+ self.assertEqual(2, obj.y)
+
def test_sequence_batch(self):
class TestObjectFactory(factory.Factory):
FACTORY_FOR = TestObject