Skip to content

Commit

Permalink
Merge pull request #123 from mikeywaites/feature/memoize
Browse files Browse the repository at this point in the history
Change tracking when marshalling many mappers
  • Loading branch information
jackqu7 authored Oct 13, 2016
2 parents 4d3c16d + 60ecd46 commit 1b5dc29
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0-rc6
1.0.0-rc7
11 changes: 10 additions & 1 deletion kim/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ def __init__(self, mapper, **mapper_params):

self.mapper = mapper
self.mapper_params = mapper_params
self._changes = []

def get_mapper(self, data=None, obj=None):
"""return a new instance of the provided mapper.
Expand Down Expand Up @@ -776,6 +777,14 @@ def marshal(self, data, role='__default__'):

output = [] # TODO should this be user defined?
for datum in data:
output.append(self.get_mapper(data=datum).marshal(role=role))
mapper = self.get_mapper(data=datum)
output.append(mapper.marshal(role=role))
self._changes.append(mapper.get_changes())

return output

def get_changes(self):
"""return the list of changes from each mapper
"""

return self._changes
24 changes: 24 additions & 0 deletions tests/test_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,30 @@ class MapperBase(Mapper):
assert (res2.name, res2.id) == ('bob', 2)


def test_mapper_marshal_many_mapper_changes():

class MapperBase(Mapper):

__type__ = TestType

id = Integer()
name = String()

data = [{'name': 'mike', 'id': 1}, {'name': 'bob', 'id': 2}]

mapper = MapperBase.many()
mapper.marshal(data)

assert mapper.get_changes() == [
{
'id': {'old_value': None, 'new_value': 1},
'name': {'old_value': None, 'new_value': 'mike'}},
{
'id': {'old_value': None, 'new_value': 2},
'name': {'old_value': None, 'new_value': 'bob'}},
]


def test_mapper_marshal_many_with_role():

class MapperBase(Mapper):
Expand Down

0 comments on commit 1b5dc29

Please sign in to comment.