/node_modules/mongodb/node_modules/bson/test/node/to_bson_test.js
JavaScript | 109 lines | 71 code | 13 blank | 25 comment | 6 complexity | 47f9a3337649a2f7c2f0ef548a83490b MD5 | raw file
Possible License(s): Apache-2.0, MIT
1var mongodb = process.env['TEST_NATIVE'] != null ? require('../../lib/bson').native() : require('../../lib/bson').pure(); 2 3var testCase = require('nodeunit').testCase, 4 mongoO = require('../../lib/bson').pure(), 5 Buffer = require('buffer').Buffer, 6 gleak = require('../../tools/gleak'), 7 fs = require('fs'), 8 BSON = mongoO.BSON, 9 Code = mongoO.Code, 10 Binary = mongoO.Binary, 11 Timestamp = mongoO.Timestamp, 12 Long = mongoO.Long, 13 MongoReply = mongoO.MongoReply, 14 ObjectID = mongoO.ObjectID, 15 Symbol = mongoO.Symbol, 16 DBRef = mongoO.DBRef, 17 Double = mongoO.Double, 18 MinKey = mongoO.MinKey, 19 MaxKey = mongoO.MaxKey, 20 BinaryParser = mongoO.BinaryParser; 21 22var BSONSE = mongodb, 23 BSONDE = mongodb; 24 25// for tests 26BSONDE.BSON_BINARY_SUBTYPE_DEFAULT = 0; 27BSONDE.BSON_BINARY_SUBTYPE_FUNCTION = 1; 28BSONDE.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2; 29BSONDE.BSON_BINARY_SUBTYPE_UUID = 3; 30BSONDE.BSON_BINARY_SUBTYPE_MD5 = 4; 31BSONDE.BSON_BINARY_SUBTYPE_USER_DEFINED = 128; 32 33BSONSE.BSON_BINARY_SUBTYPE_DEFAULT = 0; 34BSONSE.BSON_BINARY_SUBTYPE_FUNCTION = 1; 35BSONSE.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2; 36BSONSE.BSON_BINARY_SUBTYPE_UUID = 3; 37BSONSE.BSON_BINARY_SUBTYPE_MD5 = 4; 38BSONSE.BSON_BINARY_SUBTYPE_USER_DEFINED = 128; 39 40var hexStringToBinary = function(string) { 41 var numberofValues = string.length / 2; 42 var array = ""; 43 44 for(var i = 0; i < numberofValues; i++) { 45 array += String.fromCharCode(parseInt(string[i*2] + string[i*2 + 1], 16)); 46 } 47 return array; 48} 49 50var assertBuffersEqual = function(test, buffer1, buffer2) { 51 if(buffer1.length != buffer2.length) test.fail("Buffers do not have the same length", buffer1, buffer2); 52 53 for(var i = 0; i < buffer1.length; i++) { 54 test.equal(buffer1[i], buffer2[i]); 55 } 56} 57 58/** 59 * Retrieve the server information for the current 60 * instance of the db client 61 * 62 * @ignore 63 */ 64exports.setUp = function(callback) { 65 callback(); 66} 67 68/** 69 * Retrieve the server information for the current 70 * instance of the db client 71 * 72 * @ignore 73 */ 74exports.tearDown = function(callback) { 75 callback(); 76} 77 78/** 79 * @ignore 80 */ 81exports['Should correctly handle toBson function for an object'] = function(test) { 82 // Test object 83 var doc = { 84 hello: new ObjectID(), 85 a:1 86 }; 87 // Add a toBson method to the object 88 doc.toBSON = function() { 89 return {b:1}; 90 } 91 92 // Serialize the data 93 var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); 94 var deserialized_doc = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); 95 test.deepEqual({b:1}, deserialized_doc); 96 test.done(); 97} 98 99/** 100 * Retrieve the server information for the current 101 * instance of the db client 102 * 103 * @ignore 104 */ 105exports.noGlobalsLeaked = function(test) { 106 var leaks = gleak.detectNew(); 107 test.equal(0, leaks.length, "global var leak detected: " + leaks.join(', ')); 108 test.done(); 109}