26
import simplejson as json
31
should_test_uuid = True
33
should_test_uuid = False
35
22
from nose.plugins.skip import SkipTest
37
24
sys.path[0:0] = [""]
27
from bson.py3compat import b
28
from bson import json_util
29
from bson.binary import Binary, MD5_SUBTYPE, USER_DEFINED_SUBTYPE
30
from bson.code import Code
39
31
from bson.dbref import DBRef
40
from bson.json_util import default, object_hook
32
from bson.max_key import MaxKey
41
33
from bson.min_key import MinKey
42
from bson.max_key import MaxKey
43
34
from bson.objectid import ObjectId
44
35
from bson.timestamp import Timestamp
45
36
from bson.tz_util import utc
38
from test.test_connection import get_connection
47
40
PY3 = sys.version_info[0] == 3
50
43
class TestJsonUtil(unittest.TestCase):
46
if not json_util.json_lib:
47
raise SkipTest("No json or simplejson module")
49
self.db = get_connection().pymongo_test
56
51
def round_tripped(self, doc):
57
return json.loads(json.dumps(doc, default=default),
58
object_hook=object_hook)
52
return json_util.loads(json_util.dumps(doc))
60
54
def round_trip(self, doc):
61
55
self.assertEqual(doc, self.round_tripped(doc))
69
63
def test_dbref(self):
70
64
self.round_trip({"ref": DBRef("foo", 5)})
71
65
self.round_trip({"ref": DBRef("foo", 5, "db")})
73
# TODO this is broken when using cjson. See:
74
# http://jira.mongodb.org/browse/PYTHON-153
75
# http://bugs.python.org/issue6105
77
# self.assertEqual("{\"ref\": {\"$ref\": \"foo\", \"$id\": 5}}",
78
# json.dumps({"ref": DBRef("foo", 5)},
80
# self.assertEqual("{\"ref\": {\"$ref\": \"foo\",
81
# \"$id\": 5, \"$db\": \"bar\"}}",
82
# json.dumps({"ref": DBRef("foo", 5, "bar")},
66
self.round_trip({"ref": DBRef("foo", ObjectId())})
67
self.round_trip({"ref": DBRef("foo", ObjectId(), "db")})
85
69
def test_datetime(self):
86
70
# only millis, not micros
100
84
self.round_trip({"m": MinKey()})
102
86
def test_maxkey(self):
103
self.round_trip({"m": MinKey()})
87
self.round_trip({"m": MaxKey()})
105
89
def test_timestamp(self):
106
res = json.dumps({"ts": Timestamp(4, 13)}, default=default)
107
dct = json.loads(res)
90
res = json_util.json.dumps({"ts": Timestamp(4, 13)},
91
default=json_util.default)
92
dct = json_util.json.loads(res)
108
93
self.assertEqual(dct['ts']['t'], 4)
109
94
self.assertEqual(dct['ts']['i'], 13)
111
96
def test_uuid(self):
112
if not should_test_uuid:
97
if not bson.has_uuid():
98
raise SkipTest("No uuid module")
115
{'uuid': uuid.UUID('f47ac10b-58cc-4372-a567-0e02b2c3d479')})
100
{'uuid': bson.uuid.UUID(
101
'f47ac10b-58cc-4372-a567-0e02b2c3d479')})
103
def test_binary(self):
104
bin_type_dict = {"bin": Binary(b("\x00\x01\x02\x03\x04"))}
106
"md5": Binary(b(' n7\x18\xaf\t/\xd1\xd1/\x80\xca\xe7q\xcc\xac'),
108
custom_type_dict = {"custom": Binary(b("hello"), USER_DEFINED_SUBTYPE)}
110
self.round_trip(bin_type_dict)
111
self.round_trip(md5_type_dict)
112
self.round_trip(custom_type_dict)
114
# PYTHON-443 ensure old type formats are supported
115
json_bin_dump = json_util.dumps(bin_type_dict)
116
self.assertTrue('"$type": "00"' in json_bin_dump)
117
self.assertEqual(bin_type_dict,
118
json_util.loads('{"bin": {"$type": 0, "$binary": "AAECAwQ="}}'))
120
json_bin_dump = json_util.dumps(md5_type_dict)
121
self.assertTrue('"$type": "05"' in json_bin_dump)
122
self.assertEqual(md5_type_dict,
123
json_util.loads('{"md5": {"$type": 5, "$binary":'
124
' "IG43GK8JL9HRL4DK53HMrA=="}}'))
126
json_bin_dump = json_util.dumps(custom_type_dict)
127
self.assertTrue('"$type": "80"' in json_bin_dump)
128
self.assertEqual(custom_type_dict,
129
json_util.loads('{"custom": {"$type": 128, "$binary":'
132
# Handle mongoexport where subtype >= 128
133
self.assertEqual(128,
134
json_util.loads('{"custom": {"$type": "ffffff80", "$binary":'
135
' "aGVsbG8="}}')['custom'].subtype)
137
self.assertEqual(255,
138
json_util.loads('{"custom": {"$type": "ffffffff", "$binary":'
139
' "aGVsbG8="}}')['custom'].subtype)
142
self.round_trip({"code": Code("function x() { return 1; }")})
143
self.round_trip({"code": Code("function y() { return z; }", z=2)})
145
def test_cursor(self):
148
db.drop_collection("test")
151
{'bar': {'hello': 'world'}},
152
{'code': Code("function x() { return 1; }")},
153
{'bin': Binary(b("\x00\x01\x02\x03\x04"))},
154
{'dbref': {'_ref': DBRef('simple',
155
ObjectId('509b8db456c02c5ab7e63c34'))}}
159
reloaded_docs = json_util.loads(json_util.dumps(db.test.find()))
161
self.assertTrue(doc in reloaded_docs)
117
163
if __name__ == "__main__":