summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--factory/__init__.py4
-rw-r--r--factory/base.py92
-rw-r--r--tests/test_using.py87
3 files changed, 55 insertions, 128 deletions
diff --git a/factory/__init__.py b/factory/__init__.py
index 6c56955..ab8005f 100644
--- a/factory/__init__.py
+++ b/factory/__init__.py
@@ -45,10 +45,6 @@ from .base import (
CREATE_STRATEGY,
STUB_STRATEGY,
use_strategy,
-
- DJANGO_CREATION,
- NAIVE_BUILD,
- MOGO_BUILD,
)
from .declarations import (
diff --git a/factory/base.py b/factory/base.py
index a6ab98e..06730ba 100644
--- a/factory/base.py
+++ b/factory/base.py
@@ -31,20 +31,6 @@ BUILD_STRATEGY = 'build'
CREATE_STRATEGY = 'create'
STUB_STRATEGY = 'stub'
-# Creation functions. Deprecated.
-# Override Factory._create instead.
-def DJANGO_CREATION(class_to_create, **kwargs):
- warnings.warn(
- "Factories defaulting to Django's Foo.objects.create() is deprecated, "
- "and will be removed in the future. Please inherit from "
- "factory.DjangoModelFactory instead.", PendingDeprecationWarning, 6)
- return class_to_create.objects.create(**kwargs)
-
-# Building functions. Deprecated.
-# Override Factory._build instead.
-NAIVE_BUILD = lambda class_to_build, **kwargs: class_to_build(**kwargs)
-MOGO_BUILD = lambda class_to_build, **kwargs: class_to_build.new(**kwargs)
-
# Special declarations
FACTORY_CLASS_DECLARATION = 'FACTORY_FOR'
@@ -498,76 +484,6 @@ class Factory(BaseFactory):
pass
@classmethod
- def set_creation_function(cls, creation_function):
- """Set the creation function for this class.
-
- Args:
- creation_function (function): the new creation function. That
- function should take one non-keyword argument, the 'class' for
- which an instance will be created. The value of the various
- fields are passed as keyword arguments.
- """
- warnings.warn(
- "Use of factory.set_creation_function is deprecated, and will be "
- "removed in the future. Please override Factory._create() instead.",
- PendingDeprecationWarning, 2)
- # Customizing 'create' strategy, using a tuple to keep the creation function
- # from turning it into an instance method.
- cls._creation_function = (creation_function,)
-
- @classmethod
- def get_creation_function(cls):
- """Retrieve the creation function for this class.
-
- Returns:
- function: A function that takes one parameter, the class for which
- an instance will be created, and keyword arguments for the value
- of the fields of the instance.
- """
- creation_function = getattr(cls, '_creation_function', ())
- if creation_function and creation_function[0]:
- return creation_function[0]
- 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
- # on actual method implementation (otherwise, make_factory isn't
- # detected as 'default').
- return DJANGO_CREATION
-
- @classmethod
- def set_building_function(cls, building_function):
- """Set the building function for this class.
-
- Args:
- building_function (function): the new building function. That
- function should take one non-keyword argument, the 'class' for
- which an instance will be built. The value of the various
- fields are passed as keyword arguments.
- """
- warnings.warn(
- "Use of factory.set_building_function is deprecated, and will be "
- "removed in the future. Please override Factory._build() instead.",
- PendingDeprecationWarning, 2)
- # Customizing 'build' strategy, using a tuple to keep the creation function
- # from turning it into an instance method.
- cls._building_function = (building_function,)
-
- @classmethod
- def get_building_function(cls):
- """Retrieve the building function for this class.
-
- Returns:
- function: A function that takes one parameter, the class for which
- an instance will be created, and keyword arguments for the value
- of the fields of the instance.
- """
- building_function = getattr(cls, '_building_function', ())
- if building_function and building_function[0]:
- return building_function[0]
-
- @classmethod
def _adjust_kwargs(cls, **kwargs):
"""Extension point for custom kwargs adjustment."""
return kwargs
@@ -587,16 +503,8 @@ class Factory(BaseFactory):
args = tuple(kwargs.pop(key) for key in cls.FACTORY_ARG_PARAMETERS)
if create:
- # Backwards compatibility
- creation_function = cls.get_creation_function()
- if creation_function:
- return creation_function(target_class, *args, **kwargs)
return cls._create(target_class, *args, **kwargs)
else:
- # Backwards compatibility
- building_function = cls.get_building_function()
- if building_function:
- return building_function(target_class, *args, **kwargs)
return cls._build(target_class, *args, **kwargs)
@classmethod
diff --git a/tests/test_using.py b/tests/test_using.py
index 8f43813..5287a6d 100644
--- a/tests/test_using.py
+++ b/tests/test_using.py
@@ -54,6 +54,12 @@ class FakeModel(object):
instance.id = 2
return instance
+ def values_list(self, *args, **kwargs):
+ return self
+
+ def order_by(self, *args, **kwargs):
+ return [1]
+
objects = FakeModelManager()
def __init__(self, **kwargs):
@@ -104,13 +110,16 @@ class SimpleBuildTestCase(unittest.TestCase):
self.assertEqual(obj.three, 3)
self.assertEqual(obj.four, None)
- @tools.disable_warnings
def test_create(self):
obj = factory.create(FakeModel, foo='bar')
+ self.assertEqual(obj.id, None)
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_create_custom_base(self):
+ obj = factory.create(FakeModel, foo='bar', FACTORY_CLASS=factory.DjangoModelFactory)
self.assertEqual(obj.id, 2)
self.assertEqual(obj.foo, 'bar')
- @tools.disable_warnings
def test_create_batch(self):
objs = factory.create_batch(FakeModel, 4, foo='bar')
@@ -118,6 +127,17 @@ class SimpleBuildTestCase(unittest.TestCase):
self.assertEqual(4, len(set(objs)))
for obj in objs:
+ self.assertEqual(obj.id, None)
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_create_batch_custom_base(self):
+ objs = factory.create_batch(FakeModel, 4, foo='bar',
+ FACTORY_CLASS=factory.DjangoModelFactory)
+
+ self.assertEqual(4, len(objs))
+ self.assertEqual(4, len(set(objs)))
+
+ for obj in objs:
self.assertEqual(obj.id, 2)
self.assertEqual(obj.foo, 'bar')
@@ -141,9 +161,14 @@ class SimpleBuildTestCase(unittest.TestCase):
self.assertEqual(obj.id, None)
self.assertEqual(obj.foo, 'bar')
- @tools.disable_warnings
def test_generate_create(self):
obj = factory.generate(FakeModel, factory.CREATE_STRATEGY, foo='bar')
+ self.assertEqual(obj.id, None)
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_generate_create_custom_base(self):
+ obj = factory.generate(FakeModel, factory.CREATE_STRATEGY, foo='bar',
+ FACTORY_CLASS=factory.DjangoModelFactory)
self.assertEqual(obj.id, 2)
self.assertEqual(obj.foo, 'bar')
@@ -162,7 +187,6 @@ class SimpleBuildTestCase(unittest.TestCase):
self.assertEqual(obj.id, None)
self.assertEqual(obj.foo, 'bar')
- @tools.disable_warnings
def test_generate_batch_create(self):
objs = factory.generate_batch(FakeModel, factory.CREATE_STRATEGY, 20, foo='bar')
@@ -170,6 +194,17 @@ class SimpleBuildTestCase(unittest.TestCase):
self.assertEqual(20, len(set(objs)))
for obj in objs:
+ self.assertEqual(obj.id, None)
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_generate_batch_create_custom_base(self):
+ objs = factory.generate_batch(FakeModel, factory.CREATE_STRATEGY, 20, foo='bar',
+ FACTORY_CLASS=factory.DjangoModelFactory)
+
+ self.assertEqual(20, len(objs))
+ self.assertEqual(20, len(set(objs)))
+
+ for obj in objs:
self.assertEqual(obj.id, 2)
self.assertEqual(obj.foo, 'bar')
@@ -188,9 +223,13 @@ class SimpleBuildTestCase(unittest.TestCase):
self.assertEqual(obj.id, None)
self.assertEqual(obj.foo, 'bar')
- @tools.disable_warnings
def test_simple_generate_create(self):
obj = factory.simple_generate(FakeModel, True, foo='bar')
+ self.assertEqual(obj.id, None)
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_simple_generate_create_custom_base(self):
+ obj = factory.simple_generate(FakeModel, True, foo='bar', FACTORY_CLASS=factory.DjangoModelFactory)
self.assertEqual(obj.id, 2)
self.assertEqual(obj.foo, 'bar')
@@ -204,7 +243,6 @@ class SimpleBuildTestCase(unittest.TestCase):
self.assertEqual(obj.id, None)
self.assertEqual(obj.foo, 'bar')
- @tools.disable_warnings
def test_simple_generate_batch_create(self):
objs = factory.simple_generate_batch(FakeModel, True, 20, foo='bar')
@@ -212,6 +250,17 @@ class SimpleBuildTestCase(unittest.TestCase):
self.assertEqual(20, len(set(objs)))
for obj in objs:
+ self.assertEqual(obj.id, None)
+ self.assertEqual(obj.foo, 'bar')
+
+ def test_simple_generate_batch_create_custom_base(self):
+ objs = factory.simple_generate_batch(FakeModel, True, 20, foo='bar',
+ FACTORY_CLASS=factory.DjangoModelFactory)
+
+ self.assertEqual(20, len(objs))
+ self.assertEqual(20, len(set(objs)))
+
+ for obj in objs:
self.assertEqual(obj.id, 2)
self.assertEqual(obj.foo, 'bar')
@@ -693,32 +742,6 @@ class UsingFactoryTestCase(unittest.TestCase):
self.assertEqual('three', obj.three)
self.assertEqual('four', obj.four)
- @tools.disable_warnings
- def test_set_building_function(self):
- def building_function(class_to_create, **kwargs):
- return "This doesn't even return an instance of {0}".format(class_to_create.__name__)
-
- class TestModelFactory(FakeModelFactory):
- FACTORY_FOR = TestModel
-
- TestModelFactory.set_building_function(building_function)
-
- test_object = TestModelFactory.build()
- self.assertEqual(test_object, "This doesn't even return an instance of TestModel")
-
- @tools.disable_warnings
- def testSetCreationFunction(self):
- def creation_function(class_to_create, **kwargs):
- return "This doesn't even return an instance of {0}".format(class_to_create.__name__)
-
- class TestModelFactory(FakeModelFactory):
- FACTORY_FOR = TestModel
-
- TestModelFactory.set_creation_function(creation_function)
-
- test_object = TestModelFactory.create()
- self.assertEqual(test_object, "This doesn't even return an instance of TestModel")
-
def testClassMethodAccessible(self):
class TestObjectFactory(factory.Factory):
FACTORY_FOR = TestObject