PYTHON-700 - Support subclassing of son manipulators
This commit is contained in:
parent
bfbfd3a866
commit
fd880545bf
@ -32,6 +32,7 @@ from pymongo.errors import (CollectionInvalid,
|
||||
from pymongo.read_preferences import (make_read_preference,
|
||||
ReadPreference,
|
||||
SECONDARY_OK_COMMANDS)
|
||||
from pymongo.son_manipulator import SONManipulator
|
||||
|
||||
|
||||
def _check_name(name):
|
||||
@ -102,9 +103,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).__func__ != getattr(base, method).__func__)
|
||||
|
||||
if manipulator.will_copy():
|
||||
if method_overwritten(manipulator, "transform_incoming"):
|
||||
|
||||
@ -960,6 +960,30 @@ class TestDatabase(IntegrationTest):
|
||||
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__":
|
||||
|
||||
Loading…
Reference in New Issue
Block a user