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 /docs | |
parent | e7a9a87320c78ec05a5d548516fe17c258e6d4c7 (diff) | |
download | factory-boy-2b661e6eae3187c05c4eb8e1c3790cee6a9e3032.tar factory-boy-2b661e6eae3187c05c4eb8e1c3790cee6a9e3032.tar.gz |
Add Dict/List declarations (Closes #18).
Diffstat (limited to 'docs')
-rw-r--r-- | docs/changelog.rst | 1 | ||||
-rw-r--r-- | docs/reference.rst | 75 |
2 files changed, 76 insertions, 0 deletions
diff --git a/docs/changelog.rst b/docs/changelog.rst index 80074ae..af43ea5 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -15,6 +15,7 @@ ChangeLog through :attr:`~factory.DjangoModelFactory.FACTORY_DJANGO_GET_OR_CREATE`. - Add support for :mod:`~factory.fuzzy` attribute definitions. - The :class:`Sequence` counter can be overridden when calling a generating function + - Add :class:`~factory.Dict` and :class:`~factory.List` declarations (Closes #18). *Removed:* diff --git a/docs/reference.rst b/docs/reference.rst index 13220b0..81aa645 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -879,6 +879,81 @@ use the :func:`iterator` decorator: yield line +Dict and List +""""""""""""" + +When a factory expects lists or dicts as arguments, such values can be generated +through the whole range of factory_boy declarations, +with the :class:`Dict` and :class:`List` attributes: + +.. class:: Dict(params[, dict_factory=factory.DictFactory]) + + The :class:`Dict` class is used for dict-like attributes. + It receives as non-keyword argument a dictionary of fields to define, whose + value may be any factory-enabled declarations: + + .. code-block:: python + + class UserFactory(factory.Factory): + FACTORY_FOR = User + + is_superuser = False + roles = factory.Dict({ + 'role1': True, + 'role2': False, + 'role3': factory.Iterator([True, False]), + 'admin': factory.SelfAttribute('..is_superuser'), + }) + + .. note:: Declarations used as a :class:`Dict` values are evaluated within + that :class:`Dict`'s context; this means that you must use + the ``..foo`` syntax to access fields defined at the factory level. + + On the other hand, the :class:`Sequence` counter is aligned on the + containing factory's one. + + + The :class:`Dict` behaviour can be tuned through the following parameters: + + .. attribute:: dict_factory + + The actual factory to use for generating the dict can be set as a keyword + argument, if an exotic dictionary-like object (SortedDict, ...) is required. + + +.. class:: List(items[, list_factory=factory.ListFactory]) + + The :class:`List` can be used for list-like attributes. + + Internally, the fields are converted into a ``index=value`` dict, which + makes it possible to override some values at use time: + + .. code-block:: python + + class UserFactory(factory.Factory): + FACTORY_FOR = User + + flags = factory.List([ + 'user', + 'active', + 'admin', + ]) + + .. code-block:: pycon + + >>> u = UserFactory(flags__2='superadmin') + >>> u.flags + ['user', 'active', 'superadmin'] + + + The :class:`List` behaviour can be tuned through the following parameters: + + .. attribute:: list_factory + + The actual factory to use for generating the list can be set as a keyword + argument, if another type (tuple, set, ...) is required. + + Post-generation hooks """"""""""""""""""""" |