diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-04-03 00:42:05 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-04-03 00:42:05 +0200 |
commit | 4c0d0650610e154499511d27268ed7c1d32b60db (patch) | |
tree | 547ad7ec93438b2513d403138be3f0de0e9fdaf7 /factory/containers.py | |
parent | 6532f25058a13e81b1365bb353848510821f571f (diff) | |
download | factory-boy-4c0d0650610e154499511d27268ed7c1d32b60db.tar factory-boy-4c0d0650610e154499511d27268ed7c1d32b60db.tar.gz |
internal: merge OrderedDeclaration.evaluate() variants.
Diffstat (limited to 'factory/containers.py')
-rw-r--r-- | factory/containers.py | 51 |
1 files changed, 19 insertions, 32 deletions
diff --git a/factory/containers.py b/factory/containers.py index 0859a10..dc3a457 100644 --- a/factory/containers.py +++ b/factory/containers.py @@ -172,30 +172,6 @@ class LazyValue(object): raise NotImplementedError("This is an abstract method.") -class SubFactoryWrapper(LazyValue): - """Lazy wrapper around a SubFactory. - - Attributes: - subfactory (declarations.SubFactory): the SubFactory being wrapped - subfields (DeclarationDict): Default values to override when evaluating - the SubFactory - create (bool): whether to 'create' or 'build' the SubFactory. - """ - - def __init__(self, subfactory, subfields, create, *args, **kwargs): - super(SubFactoryWrapper, self).__init__(*args, **kwargs) - self.subfactory = subfactory - self.subfields = subfields - self.create = create - - def evaluate(self, obj, containers=()): - expanded_containers = (obj,) - if containers: - expanded_containers += tuple(containers) - return self.subfactory.evaluate(self.create, self.subfields, - expanded_containers) - - class OrderedDeclarationWrapper(LazyValue): """Lazy wrapper around an OrderedDeclaration. @@ -206,10 +182,12 @@ class OrderedDeclarationWrapper(LazyValue): declaration """ - def __init__(self, declaration, sequence, *args, **kwargs): - super(OrderedDeclarationWrapper, self).__init__(*args, **kwargs) + def __init__(self, declaration, sequence, create, extra=None, **kwargs): + super(OrderedDeclarationWrapper, self).__init__(**kwargs) self.declaration = declaration self.sequence = sequence + self.create = create + self.extra = extra def evaluate(self, obj, containers=()): """Lazily evaluate the attached OrderedDeclaration. @@ -219,7 +197,14 @@ class OrderedDeclarationWrapper(LazyValue): containers (object list): the chain of containers of the object being built, its immediate holder being first. """ - return self.declaration.evaluate(self.sequence, obj, containers) + return self.declaration.evaluate(self.sequence, obj, + create=self.create, + extra=self.extra, + containers=containers, + ) + + def __repr__(self): + return '<%s for %r>' % (self.__class__.__name__, self.declaration) class AttributeBuilder(object): @@ -240,7 +225,7 @@ class AttributeBuilder(object): extra = {} self.factory = factory - self._containers = extra.pop('__containers', None) + self._containers = extra.pop('__containers', ()) self._attrs = factory.declarations(extra) attrs_with_subfields = [k for k, v in self._attrs.items() if self.has_subfields(v)] @@ -263,10 +248,12 @@ class AttributeBuilder(object): # OrderedDeclaration. wrapped_attrs = {} for k, v in self._attrs.items(): - if isinstance(v, declarations.SubFactory): - v = SubFactoryWrapper(v, self._subfields.get(k, {}), create) - elif isinstance(v, declarations.OrderedDeclaration): - v = OrderedDeclarationWrapper(v, self.factory.sequence) + if isinstance(v, declarations.OrderedDeclaration): + v = OrderedDeclarationWrapper(v, + sequence=self.factory.sequence, + create=create, + extra=self._subfields.get(k, {}), + ) wrapped_attrs[k] = v stub = LazyStub(wrapped_attrs, containers=self._containers, |