diff options
author | Raphaël Barrois <raphael.barrois@polyconseil.fr> | 2011-05-16 14:48:23 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polyconseil.fr> | 2011-05-16 14:48:23 +0200 |
commit | f7cb0a040cab507541405d64efbf44b874acf207 (patch) | |
tree | 54f7eb2603bb21ff5e5410a3b634e6261763cae0 /factory/declarations.py | |
parent | df8b1e10e06a6a6faa15808caa7167f3a8361c55 (diff) | |
download | factory-boy-f7cb0a040cab507541405d64efbf44b874acf207.tar factory-boy-f7cb0a040cab507541405d64efbf44b874acf207.tar.gz |
Add SubFactories, and full testing.
Signed-off-by: Raphaël Barrois <raphael.barrois@polyconseil.fr>
Diffstat (limited to 'factory/declarations.py')
-rw-r--r-- | factory/declarations.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/factory/declarations.py b/factory/declarations.py index 4c44e0c..a4a62af 100644 --- a/factory/declarations.py +++ b/factory/declarations.py @@ -75,6 +75,21 @@ class LazyAttributeSequence(Sequence): def evaluate(self, factory, attributes): return self.function(attributes, self.type(factory.sequence)) +class SubFactory(OrderedDeclaration): + """Base class for attributes based upon a sub-factory.""" + def __init__(self, factory, **kwargs): + super(SubFactory, self).__init__() + self.defaults = factory.declarations() + self.defaults.update_base(kwargs) + self.factory = factory + + def evaluate(self, factory, create, attributes): + attrs = self.defaults.build_attributes(self.factory, create, attributes) + if create: + return self.factory.create(**attrs) + else: + return self.factory.build(**attrs) + # Decorators... in case lambdas don't cut it def lazy_attribute(func): |