PYTHON-700 - Support subclassing of son manipulators
This commit is contained in:
parent
9dda1346dd
commit
0fb2fcfac0
@ -29,6 +29,7 @@ from pymongo.errors import (CollectionInvalid,
|
||||
from pymongo.read_preferences import (modes,
|
||||
secondary_ok_commands,
|
||||
ReadPreference)
|
||||
from pymongo.son_manipulator import SONManipulator
|
||||
|
||||
|
||||
def _check_name(name):
|
||||
@ -94,9 +95,10 @@ class Database(common.BaseObject):
|
||||
:Parameters:
|
||||
- `manipulator`: the manipulator to add
|
||||
"""
|
||||
base = SONManipulator()
|
||||
def method_overwritten(instance, method):
|
||||
return getattr(instance, method) != \
|
||||
getattr(super(instance.__class__, instance), method)
|
||||
return (getattr(
|
||||
instance, method).im_func != getattr(base, method).im_func)
|
||||
|
||||
if manipulator.will_copy():
|
||||
if method_overwritten(manipulator, "transform_incoming"):
|
||||
|
||||
@ -707,6 +707,30 @@ class TestDatabase(unittest.TestCase):
|
||||
out = db.test.find_one()
|
||||
self.assertEqual('value', out.get('value'))
|
||||
|
||||
def test_son_manipulator_inheritance(self):
|
||||
class Thing(object):
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
class ThingTransformer(SONManipulator):
|
||||
def transform_incoming(self, thing, collection):
|
||||
return {'value': thing.value}
|
||||
|
||||
def transform_outgoing(self, son, collection):
|
||||
return Thing(son['value'])
|
||||
|
||||
class Child(ThingTransformer):
|
||||
pass
|
||||
|
||||
db = self.client.foo
|
||||
db.add_son_manipulator(Child())
|
||||
t = Thing('value')
|
||||
|
||||
db.test.remove()
|
||||
db.test.insert([t])
|
||||
out = db.test.find_one()
|
||||
self.assertTrue(isinstance(out, Thing))
|
||||
self.assertEqual('value', out.value)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user