@@ -380,5 +380,62 @@ def test_ordered_dict(self):
380
380
d = OrderedDict ([("one" , 1 ), ("two" , 2 ), ("three" , 3 ), ("four" , 4 )])
381
381
self .assertEqual (d , BSON .encode (d ).decode (as_class = OrderedDict ))
382
382
383
+ def test_loads_dumps (self ):
384
+
385
+ def _test_conversion (dictrep , bsonrep ):
386
+ self .assertEqual (bson .dumps (dictrep ), bsonrep )
387
+ self .assertEqual (bson .loads (bsonrep ), dictrep )
388
+ self .assertEqual (bson .loads (bson .dumps (dictrep )), dictrep )
389
+ self .assertEqual (bson .dumps (bson .loads (bsonrep )), bsonrep )
390
+
391
+ _test_conversion ({}, "\x05 \x00 \x00 \x00 \x00 " )
392
+ _test_conversion ({"test" : u"hello world" },
393
+ "\x1B \x00 \x00 \x00 \x02 \x74 \x65 \x73 \x74 \x00 \x0C \x00 \x00 "
394
+ "\x00 \x68 \x65 \x6C \x6C \x6F \x20 \x77 \x6F \x72 \x6C \x64 \x00 "
395
+ "\x00 " )
396
+ _test_conversion ({u"mike" : 100 },
397
+ "\x0F \x00 \x00 \x00 \x10 \x6D \x69 \x6B \x65 \x00 \x64 \x00 \x00 "
398
+ "\x00 \x00 " )
399
+ _test_conversion ({"hello" : 1.5 },
400
+ "\x14 \x00 \x00 \x00 \x01 \x68 \x65 \x6C \x6C \x6F \x00 \x00 \x00 "
401
+ "\x00 \x00 \x00 \x00 \xF8 \x3F \x00 " )
402
+ _test_conversion ({"true" : True },
403
+ "\x0C \x00 \x00 \x00 \x08 \x74 \x72 \x75 \x65 \x00 \x01 \x00 " )
404
+ _test_conversion ({"false" : False },
405
+ "\x0D \x00 \x00 \x00 \x08 \x66 \x61 \x6C \x73 \x65 \x00 \x00 "
406
+ "\x00 " )
407
+ _test_conversion ({"empty" : []},
408
+ "\x11 \x00 \x00 \x00 \x04 \x65 \x6D \x70 \x74 \x79 \x00 \x05 \x00 "
409
+ "\x00 \x00 \x00 \x00 " )
410
+ _test_conversion ({"none" : {}},
411
+ "\x10 \x00 \x00 \x00 \x03 \x6E \x6F \x6E \x65 \x00 \x05 \x00 \x00 "
412
+ "\x00 \x00 \x00 " )
413
+ _test_conversion ({"test" : Binary ("test" , 0 )},
414
+ "\x14 \x00 \x00 \x00 \x05 \x74 \x65 \x73 \x74 \x00 \x04 \x00 \x00 "
415
+ "\x00 \x00 \x74 \x65 \x73 \x74 \x00 " )
416
+ _test_conversion ({"test" : Binary ("test" )},
417
+ "\x18 \x00 \x00 \x00 \x05 \x74 \x65 \x73 \x74 \x00 \x08 \x00 \x00 "
418
+ "\x00 \x02 \x04 \x00 \x00 \x00 \x74 \x65 \x73 \x74 \x00 " )
419
+ _test_conversion ({"test" : Binary ("test" , 128 )},
420
+ "\x14 \x00 \x00 \x00 \x05 \x74 \x65 \x73 \x74 \x00 \x04 \x00 \x00 "
421
+ "\x00 \x80 \x74 \x65 \x73 \x74 \x00 " )
422
+ _test_conversion ({"test" : None },
423
+ "\x0B \x00 \x00 \x00 \x0A \x74 \x65 \x73 \x74 \x00 \x00 " )
424
+ _test_conversion ({"date" : datetime .datetime (2007 , 1 , 8 , 0 , 30 , 11 )},
425
+ "\x13 \x00 \x00 \x00 \x09 \x64 \x61 \x74 \x65 \x00 \x38 \xBE \x1C "
426
+ "\xFF \x0F \x01 \x00 \x00 \x00 " )
427
+ _test_conversion ({"$where" : Code ("test" )},
428
+ "\x1F \x00 \x00 \x00 \x0F \x24 \x77 \x68 \x65 \x72 \x65 \x00 \x12 "
429
+ "\x00 \x00 \x00 \x05 \x00 \x00 \x00 \x74 \x65 \x73 \x74 \x00 \x05 "
430
+ "\x00 \x00 \x00 \x00 \x00 " )
431
+ a = ObjectId ("\x00 \x01 \x02 \x03 \x04 \x05 \x06 \x07 \x08 \x09 \x0A \x0B " )
432
+ _test_conversion ({"oid" : a },
433
+ "\x16 \x00 \x00 \x00 \x07 \x6F \x69 \x64 \x00 \x00 \x01 \x02 \x03 "
434
+ "\x04 \x05 \x06 \x07 \x08 \x09 \x0A \x0B \x00 " )
435
+ _test_conversion ({"ref" : DBRef ("coll" , a )},
436
+ "\x2F \x00 \x00 \x00 \x03 ref\x00 \x25 \x00 \x00 \x00 \x02 $ref"
437
+ "\x00 \x05 \x00 \x00 \x00 coll\x00 \x07 $id\x00 \x00 \x01 \x02 "
438
+ "\x03 \x04 \x05 \x06 \x07 \x08 \x09 \x0A \x0B \x00 \x00 " )
439
+
383
440
if __name__ == "__main__" :
384
441
unittest .main ()
0 commit comments