utils.test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var utils = require('../lib/utils');
  2. var assert = require('assert');
  3. var mongo;
  4. try {
  5. mongo = new require('mongodb');
  6. } catch (e) {}
  7. describe('lib/utils', function() {
  8. describe('clone', function() {
  9. it('clones constructors named ObjectId', function(done) {
  10. function ObjectId (id) {
  11. this.id = id;
  12. }
  13. var o1 = new ObjectId('1234');
  14. var o2 = utils.clone(o1);
  15. assert.ok(o2 instanceof ObjectId);
  16. done();
  17. });
  18. it('clones constructors named ObjectID', function(done) {
  19. function ObjectID (id) {
  20. this.id = id;
  21. }
  22. var o1 = new ObjectID('1234');
  23. var o2 = utils.clone(o1);
  24. assert.ok(o2 instanceof ObjectID);
  25. done();
  26. });
  27. it('does not clone constructors named ObjectIdd', function(done) {
  28. function ObjectIdd (id) {
  29. this.id = id;
  30. }
  31. var o1 = new ObjectIdd('1234');
  32. var o2 = utils.clone(o1);
  33. assert.ok(!(o2 instanceof ObjectIdd));
  34. done();
  35. });
  36. it('optionally clones ObjectId constructors using its clone method', function(done) {
  37. function ObjectID (id) {
  38. this.id = id;
  39. this.cloned = false;
  40. }
  41. ObjectID.prototype.clone = function () {
  42. var ret = new ObjectID(this.id);
  43. ret.cloned = true;
  44. return ret;
  45. }
  46. var id = 1234;
  47. var o1 = new ObjectID(id);
  48. assert.equal(id, o1.id);
  49. assert.equal(false, o1.cloned);
  50. var o2 = utils.clone(o1);
  51. assert.ok(o2 instanceof ObjectID);
  52. assert.equal(id, o2.id);
  53. assert.ok(o2.cloned);
  54. done();
  55. });
  56. it('clones mongodb.ReadPreferences', function (done) {
  57. if (!mongo) return done();
  58. var tags = [
  59. {dc: 'tag1'}
  60. ];
  61. var prefs = [
  62. new mongo.ReadPreference("primary"),
  63. new mongo.ReadPreference(mongo.ReadPreference.PRIMARY_PREFERRED),
  64. new mongo.ReadPreference("primary", tags),
  65. mongo.ReadPreference("primary", tags)
  66. ];
  67. var prefsCloned = utils.clone(prefs);
  68. for (var i = 0; i < prefsCloned.length; i++) {
  69. assert.notEqual(prefs[i], prefsCloned[i]);
  70. assert.ok(prefsCloned[i] instanceof mongo.ReadPreference);
  71. assert.ok(prefsCloned[i].isValid());
  72. if (prefs[i].tags) {
  73. assert.ok(prefsCloned[i].tags);
  74. assert.notEqual(prefs[i].tags, prefsCloned[i].tags);
  75. assert.notEqual(prefs[i].tags[0], prefsCloned[i].tags[0]);
  76. } else {
  77. assert.equal(prefsCloned[i].tags, null);
  78. }
  79. }
  80. done();
  81. });
  82. it('clones mongodb.Binary', function(done){
  83. if (!mongo) return done();
  84. var buf = new Buffer('hi');
  85. var binary= new mongo.Binary(buf, 2);
  86. var clone = utils.clone(binary);
  87. assert.equal(binary.sub_type, clone.sub_type);
  88. assert.equal(String(binary.buffer), String(buf));
  89. assert.ok(binary !== clone);
  90. done();
  91. })
  92. it('handles objects with no constructor', function(done) {
  93. var name ='335';
  94. var o = Object.create(null);
  95. o.name = name;
  96. var clone;
  97. assert.doesNotThrow(function() {
  98. clone = utils.clone(o);
  99. });
  100. assert.equal(name, clone.name);
  101. assert.ok(o != clone);
  102. done();
  103. });
  104. it('handles buffers', function(done){
  105. var buff = new Buffer(10);
  106. buff.fill(1);
  107. var clone = utils.clone(buff);
  108. for (var i = 0; i < buff.length; i++) {
  109. assert.equal(buff[i], clone[i]);
  110. }
  111. done();
  112. });
  113. });
  114. });