diff options
Diffstat (limited to 'factory/base.py')
-rw-r--r-- | factory/base.py | 88 |
1 files changed, 7 insertions, 81 deletions
diff --git a/factory/base.py b/factory/base.py index a3d91b0..c1dbd98 100644 --- a/factory/base.py +++ b/factory/base.py @@ -32,12 +32,7 @@ CREATE_STRATEGY = 'create' STUB_STRATEGY = 'stub' # Creation functions. Use Factory.set_creation_function() to set a creation function appropriate for your ORM. -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) +DJANGO_CREATION = lambda class_to_create, **kwargs: class_to_create.objects.create(**kwargs) # Building functions. Use Factory.set_building_function() to set a building functions appropriate for your ORM. NAIVE_BUILD = lambda class_to_build, **kwargs: class_to_build(**kwargs) @@ -187,7 +182,7 @@ class FactoryMetaClass(BaseFactoryMetaClass): FACTORY_CLASS_DECLARATION, associated_name, class_name, - ), DeprecationWarning, 3) + ), PendingDeprecationWarning) return getattr(factory_module, associated_name) @@ -266,9 +261,6 @@ class BaseFactory(object): # class. _base_factory = None - # List of arguments that should be passed as *args instead of **kwargs - FACTORY_ARG_PARAMETERS = () - @classmethod def _setup_next_sequence(cls): """Set up an initial sequence value for Sequence attributes. @@ -325,36 +317,6 @@ class BaseFactory(object): return getattr(cls, CLASS_ATTRIBUTE_DECLARATIONS).copy(extra_defs) @classmethod - def _build(cls, target_class, *args, **kwargs): - """Actually build an instance of the target_class. - - Customization point, will be called once the full set of args and kwargs - has been computed. - - Args: - target_class (type): the class for which an instance should be - built - args (tuple): arguments to use when building the class - kwargs (dict): keyword arguments to use when building the class - """ - return target_class(*args, **kwargs) - - @classmethod - def _create(cls, target_class, *args, **kwargs): - """Actually create an instance of the target_class. - - Customization point, will be called once the full set of args and kwargs - has been computed. - - Args: - target_class (type): the class for which an instance should be - created - args (tuple): arguments to use when creating the class - kwargs (dict): keyword arguments to use when creating the class - """ - return target_class(*args, **kwargs) - - @classmethod def build(cls, **kwargs): """Build an instance of the associated class, with overriden attrs.""" raise cls.UnsupportedStrategy() @@ -500,7 +462,7 @@ class Factory(BaseFactory): # Customizing 'create' strategy, using a tuple to keep the creation function # from turning it into an instance method. - _creation_function = (None,) + _creation_function = (DJANGO_CREATION,) @classmethod def set_creation_function(cls, creation_function): @@ -512,10 +474,6 @@ class Factory(BaseFactory): 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) cls._creation_function = (creation_function,) @classmethod @@ -527,22 +485,11 @@ class Factory(BaseFactory): an instance will be created, and keyword arguments for the value of the fields of the instance. """ - creation_function = cls._creation_function[0] - if creation_function: - return creation_function - elif cls._create.__func__ == Factory._create.__func__: - # 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 - else: - return creation_function + return cls._creation_function[0] # Customizing 'build' strategy, using a tuple to keep the creation function # from turning it into an instance method. - _building_function = (None,) + _building_function = (NAIVE_BUILD,) @classmethod def set_building_function(cls, building_function): @@ -554,10 +501,6 @@ class Factory(BaseFactory): 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) cls._building_function = (building_function,) @classmethod @@ -579,23 +522,10 @@ class Factory(BaseFactory): create: bool, whether to create or to build the object **kwargs: arguments to pass to the creation function """ - target_class = getattr(cls, CLASS_ATTRIBUTE_ASSOCIATED_CLASS) - - # Extract *args from **kwargs - 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) + return cls.get_creation_function()(getattr(cls, CLASS_ATTRIBUTE_ASSOCIATED_CLASS), **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) + return cls.get_building_function()(getattr(cls, CLASS_ATTRIBUTE_ASSOCIATED_CLASS), **kwargs) @classmethod def _generate(cls, create, attrs): @@ -651,10 +581,6 @@ class DjangoModelFactory(Factory): except IndexError: return 1 - def _create(cls, target_class, *args, **kwargs): - """Create an instance of the model, and save it to the database.""" - return target_class._default_manager.create(*args, **kwargs) - def make_factory(klass, **kwargs): """Create a new, simple factory for the given class.""" |