def test_binary(self):
bin_type_dict = {"bin": Binary(b"\x00\x01\x02\x03\x04")}
md5_type_dict = {
"md5": Binary(b" n7\x18\xaf\t/\xd1\xd1/\x80\xca\xe7q\xcc\xac",
MD5_SUBTYPE)
}
custom_type_dict = {"custom": Binary(b"hello", USER_DEFINED_SUBTYPE)}
self.round_trip(bin_type_dict)
self.round_trip(md5_type_dict)
self.round_trip(custom_type_dict)
json_bin_dump = bsonjs_dumps(md5_type_dict)
# Order should be $binary then $type.
self.assertEqual(
('{ "md5" : { "$binary" : "IG43GK8JL9HRL4DK53HMrA==", '
'"$type" : "05" } }'),
json_bin_dump)
json_bin_dump = bsonjs_dumps(custom_type_dict)
self.assertTrue('"$type" : "80"' in json_bin_dump)
# Check loading invalid binary
self.assertRaises(ValueError, bsonjs.loads,
'{"a": {"$binary": "invalid", "$type": "80"}}')
评论列表
文章目录