diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2016-04-02 16:14:06 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2016-04-02 17:11:46 +0200 |
commit | 03c40fd80707ad4837523a07cdf3f82564ab0259 (patch) | |
tree | 257ad8b19f1c4280006bd410ecc453e4d93c4323 /factory | |
parent | c77962de7dd7206ccab85b44da173832acbf5921 (diff) | |
download | factory-boy-03c40fd80707ad4837523a07cdf3f82564ab0259.tar factory-boy-03c40fd80707ad4837523a07cdf3f82564ab0259.tar.gz |
Add Traits (Closes #251).
Based on a boolean flag, those will alter the definitions of the current
factory, taking precedence over pre-defined behavior but overridden by
callsite-level arguments.
Diffstat (limited to 'factory')
-rw-r--r-- | factory/__init__.py | 1 | ||||
-rw-r--r-- | factory/declarations.py | 16 |
2 files changed, 17 insertions, 0 deletions
diff --git a/factory/__init__.py b/factory/__init__.py index ccd71cd..ad9da80 100644 --- a/factory/__init__.py +++ b/factory/__init__.py @@ -52,6 +52,7 @@ from .declarations import ( Sequence, LazyAttributeSequence, SelfAttribute, + Trait, ContainerAttribute, SubFactory, Dict, diff --git a/factory/declarations.py b/factory/declarations.py index ad1f72f..895f2ac 100644 --- a/factory/declarations.py +++ b/factory/declarations.py @@ -469,6 +469,22 @@ class ComplexParameter(object): return [] +class Trait(ComplexParameter): + """The simplest complex parameter, it enables a bunch of new declarations based on a boolean flag.""" + def __init__(self, **overrides): + self.overrides = overrides + + def compute(self, field_name, declarations): + if declarations.get(field_name): + return self.overrides + else: + return {} + + def get_revdeps(self, parameters): + """This might alter fields it's injecting.""" + return [param for param in parameters if param in self.overrides] + + # Post-generation # =============== |