diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2011-09-05 21:40:10 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2011-09-05 21:40:10 +0200 |
commit | 89631460a87d84790bc88766433e42f881937bf7 (patch) | |
tree | 00d00c1987a63ee1c74a8f5864c954d7cad5ff65 /factory/containers.py | |
parent | 0d97937d994d4ec11f77661985be971a61daa6e3 (diff) | |
download | factory-boy-89631460a87d84790bc88766433e42f881937bf7.tar factory-boy-89631460a87d84790bc88766433e42f881937bf7.tar.gz |
Handle nested SubFactory with extra attributes.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
Diffstat (limited to 'factory/containers.py')
-rw-r--r-- | factory/containers.py | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/factory/containers.py b/factory/containers.py index b46a19f..d3fbc5b 100644 --- a/factory/containers.py +++ b/factory/containers.py @@ -134,6 +134,21 @@ class DeclarationsHolder(object): def items(self): return list(self.iteritems()) + def _extract_sub_fields(self, base): + """Extract all subfields declaration from a given dict-like object. + + Will compare with attributes declared in the current object, and + will pop() values from the given base. + """ + sub_fields = dict() + + for key in list(base): + if ATTR_SPLITTER in key: + cls_name, attr_name = key.split(ATTR_SPLITTER, 1) + if cls_name in self: + sub_fields.setdefault(cls_name, {})[attr_name] = base.pop(key) + return sub_fields + def build_attributes(self, factory, create=False, extra=None): """Build the list of attributes based on class attributes.""" if not extra: @@ -143,11 +158,8 @@ class DeclarationsHolder(object): attributes = {} sub_fields = {} - for key in list(extra.keys()): - if ATTR_SPLITTER in key: - cls_name, attr_name = key.split(ATTR_SPLITTER, 1) - if cls_name in self: - sub_fields.setdefault(cls_name, {})[attr_name] = extra.pop(key) + for base in (self._unordered, self._ordered, extra): + sub_fields.update(self._extract_sub_fields(base)) # For fields in _unordered, use the value from extra if any; otherwise, # use the default value. |