Skip to content

Commit eaaa54b

Browse files
committed
PYTHON-893 - Fix application of SON manipulators in CommandCursor.
1 parent eda1e77 commit eaaa54b

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

pymongo/command_cursor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def next(self):
169169
"""
170170
if len(self.__data) or self._refresh():
171171
coll = self.__collection
172-
return coll.database._fix_incoming(self.__data.popleft(), coll)
172+
return coll.database._fix_outgoing(self.__data.popleft(), coll)
173173
else:
174174
raise StopIteration
175175

test/test_database.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,33 @@ def transform_incoming(self, thing, collection):
759759
out = db.test.find_one()
760760
self.assertEqual('value', out.get('value'))
761761

762+
def test_son_manipulator_outgoing(self):
763+
class Thing(object):
764+
def __init__(self, value):
765+
self.value = value
766+
767+
class ThingTransformer(SONManipulator):
768+
def transform_outgoing(self, doc, collection):
769+
# We don't want this applied to the command return
770+
# value in pymongo.cursor.Cursor.
771+
if 'value' in doc:
772+
return Thing(doc['value'])
773+
return doc
774+
775+
db = self.client.foo
776+
db.add_son_manipulator(ThingTransformer())
777+
778+
db.test.remove()
779+
db.test.insert({'value': 'value'})
780+
out = db.test.find_one()
781+
self.assertTrue(isinstance(out, Thing))
782+
self.assertEqual('value', out.value)
783+
784+
if version.at_least(self.client, (2, 6)):
785+
out = db.test.aggregate([], cursor={}).next()
786+
self.assertTrue(isinstance(out, Thing))
787+
self.assertEqual('value', out.value)
788+
762789
def test_son_manipulator_inheritance(self):
763790
class Thing(object):
764791
def __init__(self, value):

0 commit comments

Comments
 (0)