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 | |
parent | e7a9a87320c78ec05a5d548516fe17c258e6d4c7 (diff) | |
download | factory-boy-2b661e6eae3187c05c4eb8e1c3790cee6a9e3032.tar factory-boy-2b661e6eae3187c05c4eb8e1c3790cee6a9e3032.tar.gz |
Add Dict/List declarations (Closes #18).
Diffstat (limited to 'factory')
-rw-r--r-- | factory/__init__.py | 7 | ||||
-rw-r--r-- | factory/base.py | 42 | ||||
-rw-r--r-- | factory/declarations.py | 35 |
3 files changed, 81 insertions, 3 deletions
diff --git a/factory/__init__.py b/factory/__init__.py index beb422e..9843fa1 100644 --- a/factory/__init__.py +++ b/factory/__init__.py @@ -25,6 +25,11 @@ __author__ = 'Raphaël Barrois <raphael.barrois+fboy@polytechnique.org>' from .base import ( Factory, + BaseDictFactory, + DictFactory, + BaseListFactory, + ListFactory, + MogoFactory, StubFactory, DjangoModelFactory, @@ -42,6 +47,8 @@ from .declarations import ( SelfAttribute, ContainerAttribute, SubFactory, + Dict, + List, PostGeneration, PostGenerationMethodCall, RelatedFactory, diff --git a/factory/base.py b/factory/base.py index 25f4714..928ea7a 100644 --- a/factory/base.py +++ b/factory/base.py @@ -621,6 +621,48 @@ class MogoFactory(Factory): return target_class.new(*args, **kwargs) +class BaseDictFactory(Factory): + """Factory for dictionary-like classes.""" + ABSTRACT_FACTORY = True + + @classmethod + def _build(cls, target_class, *args, **kwargs): + if args: + raise ValueError( + "DictFactory %r does not support FACTORY_ARG_PARAMETERS.", cls) + return target_class(**kwargs) + + @classmethod + def _create(cls, target_class, *args, **kwargs): + return cls._build(target_class, *args, **kwargs) + + +class DictFactory(BaseDictFactory): + FACTORY_FOR = dict + + +class BaseListFactory(Factory): + """Factory for list-like classes.""" + ABSTRACT_FACTORY = True + + @classmethod + def _build(cls, target_class, *args, **kwargs): + if args: + raise ValueError( + "ListFactory %r does not support FACTORY_ARG_PARAMETERS.", cls) + + values = [v for k, v in sorted(kwargs.items())] + return target_class(values) + + @classmethod + def _create(cls, target_class, *args, **kwargs): + return cls._build(target_class, *args, **kwargs) + + +class ListFactory(BaseListFactory): + FACTORY_FOR = list + + def use_strategy(new_strategy): """Force the use of a different strategy. 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.""" |