Skip to content

Commit

Permalink
extract the attribute name from the closure when a modifier is used i…
Browse files Browse the repository at this point in the history
…n a 'direct' mapping
  • Loading branch information
guewen committed May 26, 2014
2 parents cf522fd + f9c6bf7 commit 8aedacd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
16 changes: 16 additions & 0 deletions connector/tests/test_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,19 @@ class ObjectMapper(ImportMapper):
'test': .5})]
}
self.assertEqual(map_record.values(for_create=True), expected)

def test_modifier_filter_field(self):
""" A direct mapping with a modifier must still be considered from the list of fields """
class MyMapper(ImportMapper):
direct = [('field', 'field2'),
('no_field', 'no_field2'),
(convert('name', int), 'out_name')]

env = mock.MagicMock()
record = {'name': '300', 'field': 'value', 'no_field': 'no_value'}
mapper = MyMapper(env)
map_record = mapper.map_record(record)
expected = {'out_name': 300, 'field2': 'value'}
self.assertEqual(map_record.values(fields=['field', 'name']), expected)
self.assertEqual(map_record.values(for_create=True,
fields=['field', 'name']), expected)
9 changes: 8 additions & 1 deletion connector/unit/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,14 @@ def _apply_with_options(self, map_record):
for_create = self.options.for_create
result = {}
for from_attr, to_attr in self.direct:
if (not fields or from_attr in fields):
if callable(from_attr): # in a modifier
# the name of the attribute is the first arg of the
# closure
attr_name = from_attr.__closure__[0].cell_contents
else:
attr_name = from_attr

if (not fields or attr_name in fields):
value = self._map_direct(map_record.source,
from_attr,
to_attr)
Expand Down

0 comments on commit 8aedacd

Please sign in to comment.