Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change tracking when marshalling many mappers #123

Merged
merged 2 commits into from
Oct 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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