diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2016-04-02 16:13:34 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2016-04-02 17:11:46 +0200 |
commit | c77962de7dd7206ccab85b44da173832acbf5921 (patch) | |
tree | 0913b772d5181f654d5ce824753186a2252e9691 /factory/declarations.py | |
parent | eea28cce1544021f3d152782c9932a20402d6240 (diff) | |
download | factory-boy-c77962de7dd7206ccab85b44da173832acbf5921.tar factory-boy-c77962de7dd7206ccab85b44da173832acbf5921.tar.gz |
Add a new Params section to factories.
This handles parameters that alter the declarations of a factory.
A few technical notes:
- A parameter's outcome may alter other parameters
- In order to fix that, we perform a (simple) cyclic definition
detection at class declaration time.
- Parameters may only be either naked values or ComplexParameter
subclasses
- Parameters are never passed to the underlying class
Diffstat (limited to 'factory/declarations.py')
-rw-r--r-- | factory/declarations.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/factory/declarations.py b/factory/declarations.py index 9ab7462..ad1f72f 100644 --- a/factory/declarations.py +++ b/factory/declarations.py @@ -440,6 +440,39 @@ class List(SubFactory): **params) +# Parameters +# ========== + + +class ComplexParameter(object): + """A complex parameter, to be used in a Factory.Params section. + + Must implement: + - A "compute" function, performing the actual declaration override + - Optionally, a get_revdeps() function (to compute other parameters it may alter) + """ + + def compute(self, field_name, declarations): + """Compute the overrides for this parameter. + + Args: + - field_name (str): the field this parameter is installed at + - declarations (dict): the global factory declarations + + Returns: + dict: the declarations to override + """ + raise NotImplementedError() + + def get_revdeps(self, parameters): + """Retrieve the list of other parameters modified by this one.""" + return [] + + +# Post-generation +# =============== + + class ExtractionContext(object): """Private class holding all required context from extraction to postgen.""" def __init__(self, value=None, did_extract=False, extra=None, for_field=''): |