-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
objectid.test.js
238 lines (204 loc) · 8.17 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
'use strict';
var test = require('./shared').assert;
const { expect } = require('chai');
var setupDatabase = require('./shared').setupDatabase;
const { ObjectId } = require('../../src');
const { setTimeout, setInterval } = require('timers');
describe('ObjectId', function () {
before(function () {
return setupDatabase(this.configuration);
});
it('shouldCorrectlyGenerateObjectId', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
},
test: function (done) {
var configuration = this.configuration;
var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
client.connect(function (err, client) {
var db = client.db(configuration.db);
var number_of_tests_done = 0;
var collection = db.collection('test_object_id_generation.data');
// Insert test documents (creates collections and test fetch by query)
collection.insert({ name: 'Fred', age: 42 }, { writeConcern: { w: 1 } }, function (err, r) {
expect(r).property('insertedCount').to.equal(1);
const id = r.insertedIds[0];
expect(id.toHexString().length).to.equal(24);
// Locate the first document inserted
collection.findOne({ name: 'Fred' }, function (err, document) {
expect(err).to.not.exist;
expect(id.toHexString()).to.equal(document._id.toHexString());
number_of_tests_done++;
});
});
// Insert another test document and collect using ObjectId
collection.insert({ name: 'Pat', age: 21 }, { writeConcern: { w: 1 } }, function (err, r) {
expect(r).property('insertedCount').to.equal(1);
const id = r.insertedIds[0];
expect(id.toHexString().length).to.equal(24);
// Locate the first document inserted
collection.findOne(id, function (err, document) {
expect(err).to.not.exist;
expect(id.toHexString()).to.equal(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 },
{ writeConcern: { w: 1 } },
function (err, r) {
expect(err).to.not.exist;
expect(r).property('insertedCount').to.equal(1);
const id = r.insertedIds[0];
expect(id.toHexString().length).to.equal(24);
expect(id.toHexString()).to.equal(objectId.toHexString());
// Locate the first document inserted
collection.findOne(id, function (err, document) {
expect(err).to.not.exist;
expect(id.toHexString()).to.equal(document._id.toHexString());
expect(objectId.toHexString()).to.equal(document._id.toHexString());
number_of_tests_done++;
});
}
);
var intervalId = setInterval(function () {
if (number_of_tests_done === 3) {
clearInterval(intervalId);
client.close(done);
}
}, 100);
});
}
});
it('shouldCorrectlyRetrieve24CharacterHexStringFromToString', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
},
test: function (done) {
// Create a new ObjectId
var objectId = new ObjectId();
// Verify that the hex string is 24 characters long
test.equal(24, objectId.toString().length);
done();
}
});
it('shouldCorrectlyRetrieve24CharacterHexStringFromToJSON', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
},
test: function (done) {
// Create a new ObjectId
var objectId = new ObjectId();
// Verify that the hex string is 24 characters long
test.equal(24, objectId.toJSON().length);
done();
}
});
it('shouldCorrectlyCreateOIDNotUsingObjectId', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
},
test: function (done) {
var configuration = this.configuration;
var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
client.connect(function (err, client) {
var db = client.db(configuration.db);
var collection = db.collection('test_non_oid_id');
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 }, { writeConcern: { w: 1 } }, function (err) {
expect(err).to.not.exist;
collection.find({ _id: date }).toArray(function (err, items) {
test.equal('' + date, '' + items[0]._id);
// Let's close the db
client.close(done);
});
});
});
}
});
it('shouldCorrectlyGenerateObjectIdFromTimestamp', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
},
test: function (done) {
var timestamp = Math.floor(new Date().getTime() / 1000);
var objectID = new ObjectId(timestamp);
var time2 = objectID.generationTime;
test.equal(timestamp, time2);
done();
}
});
it('shouldCorrectlyCreateAnObjectIdAndOverrideTheTimestamp', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
},
test: function (done) {
var timestamp = 1000;
var objectID = new ObjectId();
var id1 = objectID.id;
// Override the timestamp
objectID.generationTime = timestamp;
var id2 = objectID.id;
// Check the timestamp
if (id1 instanceof Buffer && id2 instanceof Buffer) {
test.deepEqual(id1.slice(0, 4), id2.slice(0, 4));
} else {
test.equal(id1.substr(4), id2.substr(4));
}
done();
}
});
it('shouldCorrectlyInsertWithObjectId', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
},
test: function (done) {
var configuration = this.configuration;
var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
client.connect(function (err, client) {
expect(err).to.not.exist;
var db = client.db(configuration.db);
var collection = db.collection('shouldCorrectlyInsertWithObjectId');
collection.insert({}, { writeConcern: { w: 1 } }, function (err) {
expect(err).to.not.exist;
const firstCompareDate = new Date();
setTimeout(function () {
collection.insert({}, { writeConcern: { w: 1 } }, function (err) {
expect(err).to.not.exist;
const secondCompareDate = new Date();
collection.find().toArray(function (err, items) {
expect(err).to.not.exist;
// 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(firstCompareDate.getFullYear(), date1.getFullYear());
test.equal(firstCompareDate.getDate(), date1.getDate());
test.equal(firstCompareDate.getMonth(), date1.getMonth());
test.equal(firstCompareDate.getHours(), date1.getHours());
test.equal(secondCompareDate.getFullYear(), date2.getFullYear());
test.equal(secondCompareDate.getDate(), date2.getDate());
test.equal(secondCompareDate.getMonth(), date2.getMonth());
test.equal(secondCompareDate.getHours(), date2.getHours());
// Let's close the db
client.close(done);
});
});
}, 2000);
});
});
}
});
});