summaryrefslogtreecommitdiff
path: root/factory/base.py
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-04-15 02:22:01 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-04-15 02:22:01 +0200
commit2b661e6eae3187c05c4eb8e1c3790cee6a9e3032 (patch)
treefb23ae16274eb071639b64792dff07f1d1302898 /factory/base.py
parente7a9a87320c78ec05a5d548516fe17c258e6d4c7 (diff)
downloadfactory-boy-2b661e6eae3187c05c4eb8e1c3790cee6a9e3032.tar
factory-boy-2b661e6eae3187c05c4eb8e1c3790cee6a9e3032.tar.gz
Add Dict/List declarations (Closes #18).
Diffstat (limited to 'factory/base.py')
-rw-r--r--factory/base.py42
1 files changed, 42 insertions, 0 deletions
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.