diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-04-15 02:22:01 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-04-15 02:22:01 +0200 |
commit | 2b661e6eae3187c05c4eb8e1c3790cee6a9e3032 (patch) | |
tree | fb23ae16274eb071639b64792dff07f1d1302898 /factory/declarations.py | |
parent | e7a9a87320c78ec05a5d548516fe17c258e6d4c7 (diff) | |
download | factory-boy-2b661e6eae3187c05c4eb8e1c3790cee6a9e3032.tar factory-boy-2b661e6eae3187c05c4eb8e1c3790cee6a9e3032.tar.gz |
Add Dict/List declarations (Closes #18).
Diffstat (limited to 'factory/declarations.py')
-rw-r--r-- | factory/declarations.py | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/factory/declarations.py b/factory/declarations.py index 3d76960..974b4ac 100644 --- a/factory/declarations.py +++ b/factory/declarations.py @@ -281,12 +281,14 @@ class ParameteredAttribute(OrderedDeclaration): containers = self._prepare_containers(obj, containers) defaults[self.CONTAINERS_FIELD] = containers - return self.generate(create, defaults) + return self.generate(sequence, obj, create, defaults) - def generate(self, create, params): # pragma: no cover + def generate(self, sequence, obj, create, params): # pragma: no cover """Actually generate the related attribute. Args: + sequence (int): the current sequence number + obj (LazyStub): the object being constructed create (bool): whether the calling factory was in 'create' or 'build' mode params (dict): parameters inherited from init and evaluation-time @@ -332,7 +334,7 @@ class SubFactory(ParameteredAttribute): self.factory_module, self.factory_name) return self.factory - def generate(self, create, params): + def generate(self, sequence, obj, create, params): """Evaluate the current definition and fill its attributes. Args: @@ -345,6 +347,33 @@ class SubFactory(ParameteredAttribute): return subfactory.simple_generate(create, **params) +class Dict(SubFactory): + """Fill a dict with usual declarations.""" + + def __init__(self, params, dict_factory='factory.DictFactory'): + super(Dict, self).__init__(dict_factory, **dict(params)) + + def generate(self, sequence, obj, create, params): + dict_factory = self.get_factory() + return dict_factory.simple_generate(create, + __sequence=sequence, + **params) + + +class List(SubFactory): + """Fill a list with standard declarations.""" + + def __init__(self, params, list_factory='factory.ListFactory'): + params = dict((str(i), v) for i, v in enumerate(params)) + super(List, self).__init__(list_factory, **params) + + def generate(self, sequence, obj, create, params): + list_factory = self.get_factory() + return list_factory.simple_generate(create, + __sequence=sequence, + **params) + + class PostGenerationDeclaration(object): """Declarations to be called once the target object has been generated.""" |