forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.test.js
413 lines (346 loc) · 11.8 KB
/
utils.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/**
* Module dependencies.
*/
var start = require('./common')
, mongoose = start.mongoose
, Schema = mongoose.Schema
, utils = require('../lib/utils')
, StateMachine = require('../lib/statemachine')
, ObjectId = require('../lib/types/objectid')
, MongooseBuffer = require('../lib/types/buffer')
, ReadPref = mongoose.mongo.ReadPreference
, assert = require('assert')
/**
* Setup.
*/
var ActiveRoster = StateMachine.ctor('require', 'init', 'modify');
/**
* Test.
*/
describe('utils', function(){
describe('ActiveRoster', function(){
it('should detect a path as required if it has been required', function (done) {
var ar = new ActiveRoster();
ar.require('hello');
assert.equal(ar.paths['hello'],'require');
done();
})
it('should detect a path as inited if it has been inited', function (done) {
var ar = new ActiveRoster();
ar.init('hello');
assert.equal(ar.paths['hello'],'init');
done();
})
it('should detect a path as modified', function (done) {
var ar = new ActiveRoster();
ar.modify('hello');
assert.equal(ar.paths['hello'],'modify');
done();
})
it('should remove a path from an old state upon a state change', function (done) {
var ar = new ActiveRoster();
ar.init('hello');
ar.modify('hello');
assert.ok(!ar.states.init.hasOwnProperty('hello'));
assert.ok(ar.states.modify.hasOwnProperty('hello'));
done();
})
it('forEach should be able to iterate through the paths belonging to one state', function (done) {
var ar = new ActiveRoster();
ar.init('hello');
ar.init('goodbye');
ar.modify('world');
ar.require('foo');
ar.forEach('init', function (path) {
assert.ok(~['hello', 'goodbye'].indexOf(path));
});
done();
})
it('forEach should be able to iterate through the paths in the union of two or more states', function (done) {
var ar = new ActiveRoster();
ar.init('hello');
ar.init('goodbye');
ar.modify('world');
ar.require('foo');
ar.forEach('modify', 'require', function (path) {
assert.ok(~['world', 'foo'].indexOf(path));
});
done();
})
it('forEach should iterate through all paths that have any state if given no state arguments', function (done) {
var ar = new ActiveRoster();
ar.init('hello');
ar.init('goodbye');
ar.modify('world');
ar.require('foo');
ar.forEach(function (path) {
assert.ok(~['hello', 'goodbye','world', 'foo'].indexOf(path));
});
done();
})
it('should be able to detect if at least one path exists in a set of states', function (done) {
var ar = new ActiveRoster();
ar.init('hello');
ar.modify('world');
assert.ok(ar.some('init'));
assert.ok(ar.some('modify'));
assert.ok(!ar.some('require'));
assert.ok(ar.some('init', 'modify'));
assert.ok(ar.some('init', 'require'));
assert.ok(ar.some('modify', 'require'));
done();
})
it('should be able to `map` over the set of paths in a given state', function (done) {
var ar = new ActiveRoster();
ar.init('hello');
ar.modify('world');
ar.require('iAmTheWalrus');
var suffixedPaths = ar.map('init', 'modify', function (path) {
return path + '-suffix';
});
assert.deepEqual(suffixedPaths,['hello-suffix', 'world-suffix']);
done();
})
it("should `map` over all states' paths if no states are specified in a `map` invocation", function (done) {
var ar = new ActiveRoster();
ar.init('hello');
ar.modify('world');
ar.require('iAmTheWalrus');
var suffixedPaths = ar.map(function (path) {
return path + '-suffix';
});
assert.deepEqual(suffixedPaths,['iAmTheWalrus-suffix', 'hello-suffix', 'world-suffix']);
done();
})
});
it('utils.options', function (done) {
var o = { a: 1, b: 2, c: 3, 0: 'zero1' };
var defaults = { b: 10, d: 20, 0: 'zero2' };
var result = utils.options(defaults, o);
assert.equal(1, result.a);
assert.equal(result.b,2);
assert.equal(result.c,3);
assert.equal(result.d,20);
assert.deepEqual(o.d,result.d);
assert.equal(result['0'],'zero1');
var result2 = utils.options(defaults);
assert.equal(result2.b, 10);
assert.equal(result2.d, 20);
assert.equal(result2['0'], 'zero2');
// same properties/vals
assert.deepEqual(defaults, result2);
// same object
assert.notEqual(defaults, result2);
done();
})
it('deepEquals on ObjectIds', function (done) {
var s = (new ObjectId).toString();
var a = new ObjectId(s)
, b = new ObjectId(s);
assert.ok(utils.deepEqual(a, b));
assert.ok(utils.deepEqual(a, a));
assert.ok(!utils.deepEqual(a, new ObjectId));
done();
})
it('deepEquals on MongooseDocumentArray works', function (done) {
var db = start()
, A = new Schema({ a: String })
, M = db.model('deepEqualsOnMongooseDocArray', new Schema({
a1: [A]
, a2: [A]
}));
db.close();
var m1 = new M({
a1: [{a: 'Hi'}, {a: 'Bye'}]
});
m1.a2 = m1.a1;
assert.ok(utils.deepEqual(m1.a1, m1.a2));
var m2 = new M;
m2.init(m1.toObject());
assert.ok(utils.deepEqual(m1.a1, m2.a1));
m2.set(m1.toObject());
assert.ok(utils.deepEqual(m1.a1, m2.a1))
done();
})
// gh-688
it('deepEquals with MongooseBuffer', function (done) {
var str = "this is the day";
var a = new MongooseBuffer(str);
var b = new MongooseBuffer(str);
var c = new Buffer(str);
var d = new Buffer("this is the way");
var e = new Buffer("other length");
assert.ok(utils.deepEqual(a, b))
assert.ok(utils.deepEqual(a, c))
assert.ok(!utils.deepEqual(a, d))
assert.ok(!utils.deepEqual(a, e))
assert.ok(!utils.deepEqual(a, []))
assert.ok(!utils.deepEqual([], a))
done();
})
it('#readPref', function(done){
var r = utils.readPref('p');
assert.equal('primary', r.mode);
var r = utils.readPref('primary');
assert.equal('primary', r.mode);
var r = utils.readPref('pp');
assert.ok(r.isValid());
assert.equal('primaryPreferred', r.mode);
var r = utils.readPref('primaryPreferred');
assert.ok(r.isValid());
assert.equal('primaryPreferred', r.mode);
var r = utils.readPref('s');
assert.equal('secondary', r.mode);
var r = utils.readPref('secondary');
assert.equal('secondary', r.mode);
var r = utils.readPref('sp');
assert.ok(r.isValid());
assert.equal('secondaryPreferred', r.mode);
var r = utils.readPref('secondaryPreferred');
assert.ok(r.isValid());
assert.equal('secondaryPreferred', r.mode);
var r = utils.readPref('n');
assert.equal('nearest', r.mode);
var r = utils.readPref('nearest');
assert.equal('nearest', r.mode);
var r = utils.readPref('explode');
assert.equal(false, r.isValid());
done();
})
describe('clone', function(){
it('retains RegExp options gh-1355', function(done){
var a = new RegExp('hello', 'igm');
assert.ok(a.global);
assert.ok(a.ignoreCase);
assert.ok(a.multiline);
var b = utils.clone(a);
assert.equal(b.source, a.source);
assert.equal(a.global, b.global);
assert.equal(a.ignoreCase, b.ignoreCase);
assert.equal(a.multiline, b.multiline);
done();
})
it('clones objects created with Object.create(null)', function(done){
var o = Object.create(null);
o.a = 0;
o.b = '0';
o.c = 1;
o.d = '1';
var out = utils.clone(o);
assert.strictEqual(0, out.a);
assert.strictEqual('0', out.b);
assert.strictEqual(1, out.c);
assert.strictEqual('1', out.d);
assert.equal(4, Object.keys(out).length);
done();
})
})
it('array.flatten', function(done){
var orig = [0,[1,2,[3,4,[5,[6]],7],8],9];
assert.deepEqual([0,1,2,3,4,5,6,7,8,9], utils.array.flatten(orig));
done();
})
describe('merge', function(){
it('merges two objects together without overriding properties & methods', function(done){
function To() {
this.name = 'to';
this.toProperty = true;
}
To.prototype.getName = function() {};
To.prototype.toMethod = function() {};
function From() {
this.name = 'from';
this.fromProperty = true;
}
From.prototype.getName = function() {};
From.prototype.fromMethod = function() {};
var to = new To();
var from = new From();
utils.merge(to, from);
assert.equal(to.name, 'to');
assert.equal(to.toProperty, true);
assert.equal(to.fromProperty, true);
assert.ok(to.getName === To.prototype.getName);
assert.ok(to.toMethod === To.prototype.toMethod);
assert.equal(to.fomMethod, From.prototype.fomMethod);
done();
})
})
describe('pluralize', function(){
it('should not pluralize _temp_ (gh-1703)', function(done){
var db = start();
var ASchema = new Schema ({
value: { type: Schema.Types.Mixed }
});
var collectionName = '_temp_';
var A = db.model(collectionName, ASchema);
assert.equal(A.collection.name, collectionName);
done();
});
it('should pluralize _temp (gh-1703)', function(done){
var db = start();
var ASchema = new Schema ({
value: { type: Schema.Types.Mixed }
});
var collectionName = '_temp';
var A = db.model(collectionName, ASchema);
assert.equal(A.collection.name, collectionName + 's');
done();
});
describe('option (gh-1707)', function(){
it('should pluralize by default', function(done){
var db = start();
var ASchema = new Schema ({value: String});
var collectionName = 'singular';
var A = db.model(collectionName, ASchema);
assert.equal(A.collection.name, collectionName + 's');
done();
});
it('should pluralize when global option set to true', function(done){
var db = start();
db.base.set('pluralization', true);
var ASchema = new Schema ({value: String});
var collectionName = 'singular';
var A = db.model(collectionName, ASchema);
assert.equal(A.collection.name, collectionName + 's');
done();
});
it('should not pluralize when global option set to false', function(done){
var db = start();
db.base.set('pluralization', false);
var ASchema = new Schema ({value: String});
var collectionName = 'singular';
var A = db.model(collectionName, ASchema);
assert.equal(A.collection.name, collectionName);
done();
});
it('should pluralize when local option set to true', function(done){
var db = start();
db.base.set('pluralization', false);
// override
var ASchema = new Schema ({value: String}, {pluralization: true});
var collectionName = 'singular';
var A = db.model(collectionName, ASchema);
assert.equal(A.collection.name, collectionName + 's');
done();
});
it('should not pluralize when local option set to false and global is true', function(done){
var db = start();
db.base.set('pluralization', true);
var ASchema = new Schema ({value: String}, {pluralization: false});
var collectionName = 'singular';
var A = db.model(collectionName, ASchema);
assert.equal(A.collection.name, collectionName);
done();
});
it('should not pluralize when local option set to false and global not set', function(done){
var db = start();
var ASchema = new Schema ({value: String}, {pluralization: false});
var collectionName = 'singular';
var A = db.model(collectionName, ASchema);
assert.equal(A.collection.name, collectionName);
done();
});
})
});
});