forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjectid_test.js
141 lines (125 loc) · 4.95 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
var mongodb = process.env['TEST_NATIVE'] != null ? require('../lib/mongodb').native() : require('../lib/mongodb').pure();
var testCase = require('../deps/nodeunit').testCase,
debug = require('util').debug,
inspect = require('util').inspect,
nodeunit = require('../deps/nodeunit'),
gleak = require('../tools/gleak'),
Db = mongodb.Db,
Cursor = mongodb.Cursor,
Collection = mongodb.Collection,
Server = mongodb.Server;
var MONGODB = 'integration_tests';
var client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4}), {native_parser: (process.env['TEST_NATIVE'] != null)});
// Define the tests, we want them to run as a nested test so we only clean up the
// db connection once
var tests = testCase({
setUp: function(callback) {
client.open(function(err, db_p) {
if(numberOfTestsRun == Object.keys(tests).length) {
// If first test drop the db
client.dropDatabase(function(err, done) {
callback();
});
} else {
return callback();
}
});
},
tearDown: function(callback) {
numberOfTestsRun = numberOfTestsRun - 1;
// Drop the database and close it
if(numberOfTestsRun <= 0) {
// client.dropDatabase(function(err, done) {
client.close();
callback();
// });
} else {
client.close();
callback();
}
},
// Test the generation of the object ids
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 client.bson_serializer.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);
},
shouldCorrectlyTransformObjectIDToAndFromHexString : function(test) {
var objectId = new client.bson_serializer.ObjectID(null);
var originalHex= objectId.toHexString();
var newObjectId= new client.bson_serializer.ObjectID.createFromHexString(originalHex)
var newHex= newObjectId.toHexString();
test.equal(originalHex, newHex);
test.done();
},
// Use some other id than the standard for inserts
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}, function(err, cursor) {
cursor.toArray(function(err, items) {
test.equal(("" + date), ("" + items[0]._id));
// Let's close the db
test.done();
});
});
});
});
},
noGlobalsLeaked : function(test) {
var leaks = gleak.detectNew();
test.equal(0, leaks.length, "global var leak detected: " + leaks.join(', '));
test.done();
}
})
// Stupid freaking workaround due to there being no way to run setup once for each suite
var numberOfTestsRun = Object.keys(tests).length;
// Assign out tests
module.exports = tests;