Recurse when dumping DBRefs PYTHON-433

This commit is contained in:
Ross Lawley 2012-11-09 08:56:23 +00:00
parent 95fe4f02ae
commit d1dc11242e
2 changed files with 6 additions and 14 deletions

View File

@ -163,7 +163,7 @@ def default(obj):
if isinstance(obj, ObjectId):
return {"$oid": str(obj)}
if isinstance(obj, DBRef):
return obj.as_doc()
return _json_convert(obj.as_doc())
if isinstance(obj, datetime.datetime):
# TODO share this code w/ bson.py?
if obj.utcoffset() is not None:

View File

@ -63,18 +63,8 @@ class TestJsonUtil(unittest.TestCase):
def test_dbref(self):
self.round_trip({"ref": DBRef("foo", 5)})
self.round_trip({"ref": DBRef("foo", 5, "db")})
# TODO this is broken when using cjson. See:
# http://jira.mongodb.org/browse/PYTHON-153
# http://bugs.python.org/issue6105
#
# self.assertEqual("{\"ref\": {\"$ref\": \"foo\", \"$id\": 5}}",
# json.dumps({"ref": DBRef("foo", 5)},
# default=json_util.default))
# self.assertEqual("{\"ref\": {\"$ref\": \"foo\",
# \"$id\": 5, \"$db\": \"bar\"}}",
# json.dumps({"ref": DBRef("foo", 5, "bar")},
# default=json_util.default))
self.round_trip({"ref": DBRef("foo", ObjectId())})
self.round_trip({"ref": DBRef("foo", ObjectId(), "db")})
def test_datetime(self):
# only millis, not micros
@ -128,7 +118,9 @@ class TestJsonUtil(unittest.TestCase):
{'foo': [1, 2]},
{'bar': {'hello': 'world'}},
{'code': Code("function x() { return 1; }")},
{'bin': Binary(b("\x00\x01\x02\x03\x04"))}
{'bin': Binary(b("\x00\x01\x02\x03\x04"))},
{'dbref': {'_ref': DBRef('simple',
ObjectId('509b8db456c02c5ab7e63c34'))}}
]
db.test.insert(docs)