forked from christkv/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjectid_test.js
349 lines (317 loc) · 10.3 KB
/
objectid_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
var mongodb = process.env['TEST_NATIVE'] != null ? require('../lib/mongodb').native() : require('../lib/mongodb').pure();
var useSSL = process.env['USE_SSL'] != null ? true : false;
var testCase = require('nodeunit').testCase,
debug = require('util').debug,
inspect = require('util').inspect,
nodeunit = require('nodeunit'),
gleak = require('../dev/tools/gleak'),
ObjectID = mongodb.ObjectID,
Db = mongodb.Db,
Cursor = mongodb.Cursor,
Collection = mongodb.Collection,
Server = mongodb.Server;
var MONGODB = 'integration_tests';
var client = null;
/**
* Retrieve the server information for the current
* instance of the db client
*
* @ignore
*/
exports.setUp = function(callback) {
var self = exports;
client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL}), {native_parser: (process.env['TEST_NATIVE'] != null)});
client.open(function(err, db_p) {
if(numberOfTestsRun == (Object.keys(self).length)) {
// If first test drop the db
client.dropDatabase(function(err, done) {
callback();
});
} else {
return callback();
}
});
}
/**
* Retrieve the server information for the current
* instance of the db client
*
* @ignore
*/
exports.tearDown = function(callback) {
var self = this;
numberOfTestsRun = numberOfTestsRun - 1;
// Close connection
client.close();
callback();
}
/**
* @ignore
*/
exports.shouldCorrectlyGenerateObjectID = function(test) {
var number_of_tests_done = 0;
client.collection('test_object_id_generation.data', function(err, collection) {
// Insert test documents (creates collections and test fetch by query)
collection.insert({name:"Fred", age:42}, {safe:true}, function(err, ids) {
test.equal(1, ids.length);
test.ok(ids[0]['_id'].toHexString().length == 24);
// Locate the first document inserted
collection.findOne({name:"Fred"}, function(err, document) {
test.equal(ids[0]['_id'].toHexString(), document._id.toHexString());
number_of_tests_done++;
});
});
// Insert another test document and collect using ObjectId
collection.insert({name:"Pat", age:21}, {safe:true}, function(err, ids) {
test.equal(1, ids.length);
test.ok(ids[0]['_id'].toHexString().length == 24);
// Locate the first document inserted
collection.findOne(ids[0]['_id'], function(err, document) {
test.equal(ids[0]['_id'].toHexString(), document._id.toHexString());
number_of_tests_done++;
});
});
// Manually created id
var objectId = new ObjectID(null);
// Insert a manually created document with generated oid
collection.insert({"_id":objectId, name:"Donald", age:95}, {safe:true}, function(err, ids) {
test.equal(1, ids.length);
test.ok(ids[0]['_id'].toHexString().length == 24);
test.equal(objectId.toHexString(), ids[0]['_id'].toHexString());
// Locate the first document inserted
collection.findOne(ids[0]['_id'], function(err, document) {
test.equal(ids[0]['_id'].toHexString(), document._id.toHexString());
test.equal(objectId.toHexString(), document._id.toHexString());
number_of_tests_done++;
});
});
});
var intervalId = setInterval(function() {
if(number_of_tests_done == 3) {
clearInterval(intervalId);
test.done();
}
}, 100);
}
/**
* Generate 12 byte binary string representation using a second based timestamp or
* default value
*
* @_class objectid
* @_function getTimestamp
* @ignore
*/
exports.shouldCorrectlyGenerate12ByteStringFromTimestamp = function(test) {
// Get a timestamp in seconds
var timestamp = Math.floor(new Date().getTime()/1000);
// Create a date with the timestamp
var timestampDate = new Date(timestamp*1000);
// Create a new ObjectID with a specific timestamp
var objectId = new ObjectID(timestamp);
// Get the timestamp and validate correctness
test.equal(timestampDate.toString(), objectId.getTimestamp().toString());
test.done();
}
/**
* Generate a 24 character hex string representation of the ObjectID
*
* @_class objectid
* @_function toHexString
* @ignore
*/
exports.shouldCorrectlyRetrieve24CharacterHexStringFromToString = function(test) {
// Create a new ObjectID
var objectId = new ObjectID();
// Verify that the hex string is 24 characters long
test.equal(24, objectId.toHexString().length);
test.done();
}
/**
* Get and set the generation time for an ObjectID
*
* @_class objectid
* @_function generationTime
* @ignore
*/
exports.shouldCorrectlyGetAndSetObjectIDUsingGenerationTimeProperty = function(test) {
// Create a new ObjectID
var objectId = new ObjectID();
// Get the generation time
var generationTime = objectId.generationTime;
// Add 1000 miliseconds to the generation time
objectId.generationTime = generationTime + 1000;
// Create a timestamp
var timestampDate = new Date();
timestampDate.setTime((generationTime + 1000) * 1000);
// Get the timestamp and validate correctness
test.equal(timestampDate.toString(), objectId.getTimestamp().toString());
test.done();
}
/**
* @ignore
*/
exports.shouldCorrectlyRetrieve24CharacterHexStringFromToString = function(test) {
// Create a new ObjectID
var objectId = new ObjectID();
// Verify that the hex string is 24 characters long
test.equal(24, objectId.toString().length);
test.done();
}
/**
* @ignore
*/
exports.shouldCorrectlyRetrieve24CharacterHexStringFromToString = function(test) {
// Create a new ObjectID
var objectId = new ObjectID();
// Verify that the hex string is 24 characters long
test.equal(24, objectId.toJSON().length);
test.done();
}
/**
* Convert a ObjectID into a hex string representation and then back to an ObjectID
*
* @_class objectid
* @_function ObjectID.createFromHexString
* @ignore
*/
exports.shouldCorrectlyTransformObjectIDToAndFromHexString = function(test) {
// Create a new ObjectID
var objectId = new ObjectID();
// Convert the object id to a hex string
var originalHex = objectId.toHexString();
// Create a new ObjectID using the createFromHexString function
var newObjectId = new ObjectID.createFromHexString(originalHex)
// Convert the new ObjectID back into a hex string using the toHexString function
var newHex = newObjectId.toHexString();
// Compare the two hex strings
test.equal(originalHex, newHex);
test.done();
}
/**
* Compare two different ObjectID's using the equals method
*
* @_class objectid
* @_function equals
* @ignore
*/
exports.shouldCorrectlyTransformObjectIDToAndFromHexString = function(test) {
// Create a new ObjectID
var objectId = new ObjectID();
// Create a new ObjectID Based on the first ObjectID
var objectId2 = new ObjectID(objectId.id);
// Create another ObjectID
var objectId3 = new ObjectID();
// objectId and objectId2 should be the same
test.ok(objectId.equals(objectId2));
// objectId and objectId2 should be different
test.ok(!objectId.equals(objectId3));
test.done();
}
/**
* @ignore
*/
exports.shouldCorrectlyCreateOIDNotUsingObjectID = function(test) {
client.createCollection('test_non_oid_id', function(err, collection) {
var date = new Date();
date.setUTCDate(12);
date.setUTCFullYear(2009);
date.setUTCMonth(11 - 1);
date.setUTCHours(12);
date.setUTCMinutes(0);
date.setUTCSeconds(30);
collection.insert({'_id':date}, {safe:true}, function(err, ids) {
collection.find({'_id':date}).toArray(function(err, items) {
test.equal(("" + date), ("" + items[0]._id));
// Let's close the db
test.done();
});
});
});
}
/**
* @ignore
*/
exports.shouldCorrectlyGenerateObjectIDFromTimestamp = function(test) {
var timestamp = Math.floor(new Date().getTime()/1000);
var objectID = new ObjectID(timestamp);
var time2 = objectID.generationTime;
test.equal(timestamp, time2);
test.done();
}
/**
* @ignore
*/
exports.shouldCorrectlyCreateAnObjectIDAndOverrideTheTimestamp = function(test) {
var timestamp = 1000;
var objectID = new ObjectID();
var id1 = objectID.id;
// Override the timestamp
objectID.generationTime = timestamp
var id2 = objectID.id;
// Check the strings
test.equal(id1.substr(4), id2.substr(4));
test.done();
}
/**
* @ignore
*/
exports.shouldCorrectlyInsertWithObjectId = function(test) {
client.createCollection('shouldCorrectlyInsertWithObjectId', function(err, collection) {
collection.insert({}, {safe:true}, function(err, ids) {
setTimeout(function() {
collection.insert({}, {safe:true}, function(err, ids) {
collection.find().toArray(function(err, items) {
var compareDate = new Date();
// Date 1
var date1 = new Date();
date1.setTime(items[0]._id.generationTime * 1000);
// Date 2
var date2 = new Date();
date2.setTime(items[1]._id.generationTime * 1000);
// Compare
test.equal(compareDate.getFullYear(), date1.getFullYear());
test.equal(compareDate.getDate(), date1.getDate());
test.equal(compareDate.getMonth(), date1.getMonth());
test.equal(compareDate.getHours(), date1.getHours());
test.equal(compareDate.getFullYear(), date2.getFullYear());
test.equal(compareDate.getDate(), date2.getDate());
test.equal(compareDate.getMonth(), date2.getMonth());
test.equal(compareDate.getHours(), date2.getHours());
// Let's close the db
test.done();
});
});
}, 2000);
});
});
}
/**
* Show the usage of the Objectid createFromTime function
*
* @_class objectid
* @_function ObjectID.createFromTime
* @ignore
*/
exports.shouldCorrectlyTransformObjectIDToAndFromHexString = function(test) {
var objectId = ObjectID.createFromTime(1);
test.equal("000000010000000000000000", objectId.toHexString());
test.done();
}
/**
* Retrieve the server information for the current
* instance of the db client
*
* @ignore
*/
exports.noGlobalsLeaked = function(test) {
var leaks = gleak.detectNew();
test.equal(0, leaks.length, "global var leak detected: " + leaks.join(', '));
test.done();
}
/**
* Retrieve the server information for the current
* instance of the db client
*
* @ignore
*/
var numberOfTestsRun = Object.keys(this).length - 2;