summaryrefslogtreecommitdiff
path: root/factory/declarations.py
diff options
context:
space:
mode:
Diffstat (limited to 'factory/declarations.py')
-rw-r--r--factory/declarations.py107
1 files changed, 19 insertions, 88 deletions
diff --git a/factory/declarations.py b/factory/declarations.py
index 77000f2..83c32ab 100644
--- a/factory/declarations.py
+++ b/factory/declarations.py
@@ -208,115 +208,46 @@ class ContainerAttribute(OrderedDeclaration):
return self.function(obj, containers)
-class ParameteredAttribute(OrderedDeclaration):
- """Base class for attributes expecting parameters.
+class SubFactory(OrderedDeclaration):
+ """Base class for attributes based upon a sub-factory.
Attributes:
- defaults (dict): Default values for the paramters.
- May be overridden by call-time parameters.
-
- Class attributes:
- CONTAINERS_FIELD (str): name of the field, if any, where container
- information (e.g for SubFactory) should be stored. If empty,
- containers data isn't merged into generate() parameters.
+ defaults (dict): Overrides to the defaults defined in the wrapped
+ factory
+ factory (base.Factory): the wrapped factory
"""
- CONTAINERS_FIELD = '__containers'
-
- def __init__(self, **kwargs):
- super(ParameteredAttribute, self).__init__()
+ def __init__(self, factory, **kwargs):
+ super(SubFactory, self).__init__()
self.defaults = kwargs
+ self.factory = factory
def evaluate(self, create, extra, containers):
"""Evaluate the current definition and fill its attributes.
Uses attributes definition in the following order:
- - values defined when defining the ParameteredAttribute
- - additional values defined when instantiating the containing factory
+ - attributes defined in the wrapped factory class
+ - values defined when defining the SubFactory
+ - additional values defined in attributes
Args:
- create (bool): whether the parent factory is being 'built' or
- 'created'
+ create (bool): whether the subfactory should call 'build' or
+ 'create'
extra (containers.DeclarationDict): extra values that should
- override the defaults
+ override the wrapped factory's defaults
containers (list of LazyStub): List of LazyStub for the chain of
factories being evaluated, the calling stub being first.
"""
+
defaults = dict(self.defaults)
if extra:
defaults.update(extra)
- if self.CONTAINERS_FIELD:
- defaults[self.CONTAINERS_FIELD] = containers
+ defaults['__containers'] = containers
- return self.generate(create, defaults)
-
- def generate(self, create, params): # pragma: no cover
- """Actually generate the related attribute.
-
- Args:
- create (bool): whether the calling factory was in 'create' or
- 'build' mode
- params (dict): parameters inherited from init and evaluation-time
- overrides.
-
- Returns:
- Computed value for the current declaration.
- """
- raise NotImplementedError()
-
-
-class SubFactory(ParameteredAttribute):
- """Base class for attributes based upon a sub-factory.
-
- Attributes:
- defaults (dict): Overrides to the defaults defined in the wrapped
- factory
- factory (base.Factory): the wrapped factory
- """
-
- def __init__(self, factory, **kwargs):
- super(SubFactory, self).__init__(**kwargs)
- self.factory = factory
-
- def get_factory(self):
- """Retrieve the wrapped factory.Factory subclass."""
- return self.factory
-
- def generate(self, create, params):
- """Evaluate the current definition and fill its attributes.
-
- Args:
- create (bool): whether the subfactory should call 'build' or
- 'create'
- params (containers.DeclarationDict): extra values that should
- override the wrapped factory's defaults
- """
- subfactory = self.get_factory()
if create:
- return subfactory.create(**params)
+ return self.factory.create(**defaults)
else:
- return subfactory.build(**params)
-
-
-class CircularSubFactory(SubFactory):
- """Use to solve circular dependencies issues."""
- def __init__(self, module_name, factory_name, **kwargs):
- super(CircularSubFactory, self).__init__(None, **kwargs)
- self.module_name = module_name
- self.factory_name = factory_name
-
- def get_factory(self):
- """Retrieve the factory.Factory subclass.
-
- Its value is cached in the 'factory' attribute, and retrieved through
- the factory_getter callable.
- """
- if self.factory is None:
- factory_class = utils.import_object(
- self.module_name, self.factory_name)
-
- self.factory = factory_class
- return self.factory
+ return self.factory.build(**defaults)
class PostGenerationDeclaration(object):
@@ -352,7 +283,7 @@ class PostGenerationDeclaration(object):
kwargs = utils.extract_dict(extract_prefix, attrs)
return extracted, kwargs
- def call(self, obj, create, extracted=None, **kwargs): # pragma: no cover
+ def call(self, obj, create, extracted=None, **kwargs):
"""Call this hook; no return value is expected.
Args: