/node_modules/mongodb/node_modules/bson/test/node/to_bson_test.js

https://bitbucket.org/gagginaspinnata/todo-app-with-angularjs · JavaScript · 109 lines · 71 code · 13 blank · 25 comment · 6 complexity · 47f9a3337649a2f7c2f0ef548a83490b MD5 · raw file

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