-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathconnection.test.js
1190 lines (1012 loc) · 34.4 KB
/
connection.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
/**
* Module dependencies.
*/
const Promise = require('bluebird');
const Q = require('q');
const assert = require('assert');
const co = require('co');
const server = require('./common').server;
const start = require('./common');
const mongoose = start.mongoose;
const Schema = mongoose.Schema;
const uri = 'mongodb://localhost:27017/mongoose_test';
/**
* Test.
*/
describe('connections:', function() {
this.timeout(10000);
describe('openUri (gh-5304)', function() {
it('with mongoose.createConnection()', function() {
const conn = mongoose.createConnection('mongodb://localhost/mongoosetest');
assert.equal(conn.constructor.name, 'NativeConnection');
const Test = conn.model('Test', new Schema({ name: String }));
assert.equal(Test.modelName, 'Test');
const findPromise = Test.findOne();
assert.equal(typeof conn.catch, 'function');
return conn.
then(function(conn) {
assert.equal(conn.constructor.name, 'NativeConnection');
assert.equal(conn.host, 'localhost');
assert.equal(conn.port, 27017);
assert.equal(conn.name, 'mongoosetest');
return findPromise;
}).
then(function() {
return conn.close();
});
});
it('with autoIndex (gh-5423)', function(done) {
const promise = mongoose.createConnection('mongodb://localhost:27017/mongoosetest', {
autoIndex: false,
useNewUrlParser: true
});
promise.then(function(conn) {
assert.strictEqual(conn.config.autoIndex, false);
done();
}).catch(done);
});
it('with autoCreate (gh-6489)', function() {
return co(function*() {
const conn = yield mongoose.createConnection(uri, {
autoCreate: true
});
const Model = conn.model('gh6489_Conn', new Schema({ name: String }, {
collation: { locale: 'en_US', strength: 1 },
collection: 'gh6489_Conn'
}));
yield Model.init();
// Will throw if collection was not created
yield conn.collection('gh6489_Conn').stats();
yield Model.create([{ name: 'alpha' }, { name: 'Zeta' }]);
// Ensure that the default collation is set. Mongoose will set the
// collation on the query itself (see gh-4839).
const res = yield conn.collection('gh6489_Conn').
find({}).sort({ name: 1 }).toArray();
assert.deepEqual(res.map(v => v.name), ['alpha', 'Zeta']);
});
});
it('autoCreate when collection already exists does not fail (gh-7122)', function() {
return co(function*() {
const conn = yield mongoose.createConnection(uri);
const schema = new mongoose.Schema({
name: {
type: String,
index: { unique: true }
}
}, { autoCreate: true });
yield conn.model('Actor', schema).init();
});
});
it('useCreateIndex (gh-6922)', function(done) {
const conn = mongoose.createConnection('mongodb://localhost:27017/mongoosetest', {
useCreateIndex: true,
useNewUrlParser: true
});
const M = conn.model('Test', new Schema({
name: { type: String, index: true }
}));
M.collection.ensureIndex = function() {
throw new Error('Fail');
};
conn.then(() => done(), err => done(err));
});
it('throws helpful error with legacy syntax (gh-6756)', function(done) {
assert.throws(function() {
mongoose.createConnection('localhost', 'dbname', 27017);
}, /mongoosejs\.com.*connections\.html/);
done();
});
it('throws helpful error with undefined uri (gh-6763)', function(done) {
assert.throws(function() {
mongoose.createConnection(void 0, { useNewUrlParser: true });
}, /string.*createConnection/);
done();
});
it('resolving with q (gh-5714)', function(done) {
const bootMongo = Q.defer();
const conn = mongoose.createConnection('mongodb://localhost:27017/mongoosetest',
{ useNewUrlParser: true });
conn.on('connected', function() {
bootMongo.resolve(this);
});
bootMongo.promise.then(function(_conn) {
assert.equal(_conn, conn);
done();
}).catch(done);
});
it('connection plugins (gh-7378)', function() {
const conn1 = mongoose.createConnection('mongodb://localhost:27017/mongoosetest',
{ useNewUrlParser: true });
const conn2 = mongoose.createConnection('mongodb://localhost:27017/mongoosetest',
{ useNewUrlParser: true });
const called = [];
conn1.plugin(schema => called.push(schema));
conn2.model('Test', new Schema({}));
assert.equal(called.length, 0);
const schema = new Schema({});
conn1.model('Test', schema);
assert.equal(called.length, 1);
assert.equal(called[0], schema);
});
describe('connection events', function() {
beforeEach(function() {
this.timeout(60000);
return server.start();
});
afterEach(function() {
this.timeout(60000);
return server.stop().
then(function() {
return server.purge();
});
});
it('disconnected (gh-5498) (gh-5524)', function(done) {
this.timeout(60000);
let numConnected = 0;
let numDisconnected = 0;
let numReconnected = 0;
let numReconnect = 0;
let numClose = 0;
const conn = mongoose.createConnection('mongodb://localhost:27000/mongoosetest', {
useNewUrlParser: true,
useUnifiedTopology: true
});
conn.on('connected', function() {
++numConnected;
});
conn.on('disconnected', function() {
++numDisconnected;
});
conn.on('reconnect', function() {
++numReconnect;
});
// Same as `reconnect`, just for backwards compat
conn.on('reconnected', function() {
++numReconnected;
});
conn.on('close', function() {
++numClose;
});
conn.
then(function() {
assert.equal(conn.readyState, conn.states.connected);
assert.equal(numConnected, 1);
return server.stop();
}).
then(function() {
return new Promise(function(resolve) {
setTimeout(function() { resolve(); }, 100);
});
}).
then(function() {
assert.equal(conn.readyState, conn.states.disconnected);
assert.equal(numDisconnected, 1);
assert.equal(numReconnected, 0);
assert.equal(numReconnect, 0);
}).
then(function() {
return server.start();
}).
then(function() {
return new Promise(function(resolve) {
setTimeout(function() { resolve(); }, 8000);
});
}).
then(function() {
assert.equal(conn.readyState, conn.states.connected);
assert.equal(numDisconnected, 1);
assert.equal(numReconnected, 1);
assert.equal(numReconnect, 1);
assert.equal(numClose, 0);
conn.close();
done();
}).
catch(done);
});
it('reconnectFailed (gh-4027)', function(done) {
this.timeout(60000);
let numReconnectFailed = 0;
let numConnected = 0;
let numDisconnected = 0;
let numReconnected = 0;
const conn = mongoose.createConnection('mongodb://localhost:27000/mongoosetest', {
reconnectTries: 3,
reconnectInterval: 100,
useNewUrlParser: true,
useUnifiedTopology: true
});
conn.on('connected', function() {
++numConnected;
});
conn.on('disconnected', function() {
++numDisconnected;
});
conn.on('reconnected', function() {
++numReconnected;
});
conn.on('reconnectFailed', function() {
++numReconnectFailed;
});
conn.
then(function() {
assert.equal(numConnected, 1);
return server.stop();
}).
then(function() {
return new Promise(function(resolve) {
setTimeout(function() { resolve(); }, 100);
});
}).
then(function() {
assert.equal(numDisconnected, 1);
assert.equal(numReconnected, 0);
assert.equal(numReconnectFailed, 0);
}).
then(function() {
return new Promise(function(resolve) {
setTimeout(function() { resolve(); }, 8000);
});
}).
then(function() {
assert.equal(numDisconnected, 1);
assert.equal(numReconnected, 0);
assert.equal(numReconnectFailed, 1);
}).
then(function() {
return server.start();
}).
then(function() {
return new Promise(function(resolve) {
setTimeout(function() { resolve(); }, 2000);
});
}).
then(function() {
assert.equal(numDisconnected, 1);
assert.equal(numReconnected, 0);
assert.equal(numReconnectFailed, 1);
conn.close();
done();
}).
catch(done);
});
it('timeout (gh-4513)', function(done) {
this.timeout(60000);
let numTimeout = 0;
let numDisconnected = 0;
const conn = mongoose.createConnection('mongodb://localhost:27000/mongoosetest', {
socketTimeoutMS: 100,
poolSize: 1,
useNewUrlParser: true
});
conn.on('timeout', function() {
++numTimeout;
});
conn.on('disconnected', function() {
++numDisconnected;
});
const Model = conn.model('gh4513', new Schema());
conn.
then(function() {
assert.equal(conn.readyState, conn.states.connected);
return Model.create({});
}).
then(function() {
return Model.find({ $where: 'sleep(250) || true' });
}).
then(function() {
done(new Error('expected timeout'));
}).
catch(function(error) {
assert.ok(error);
assert.ok(error.message.indexOf('timed out'), error.message);
// TODO: if autoReconnect is false, we might not actually be
// connected. See gh-5634
assert.equal(conn.readyState, conn.states.connected);
assert.equal(numTimeout, 1);
assert.equal(numDisconnected, 0);
conn.close();
done();
});
});
});
});
describe('helpers', function() {
let conn;
before(function() {
conn = mongoose.createConnection('mongodb://localhost:27017/mongoosetest_2');
return conn;
});
after(function() {
return conn.close();
});
it('dropDatabase()', function(done) {
conn.dropDatabase(function(error) {
assert.ifError(error);
done();
});
});
it('dropCollection()', function() {
return conn.db.collection('test').insertOne({ x: 1 }).
then(function() {
return conn.dropCollection('test');
}).
then(function() {
return conn.db.collection('test').findOne();
}).
then(function(doc) {
assert.ok(!doc);
});
});
it('createCollection()', function() {
return conn.dropDatabase().
then(function() {
return conn.createCollection('gh5712', {
capped: true,
size: 1024
});
}).
then(function() {
return conn.db.listCollections().toArray();
}).
then(function(collections) {
const names = collections.map(function(c) { return c.name; });
assert.ok(names.indexOf('gh5712') !== -1);
assert.ok(collections[names.indexOf('gh5712')].options.capped);
return conn.createCollection('gh5712_0');
}).
then(function() {
return conn.db.listCollections().toArray();
}).
then(function(collections) {
const names = collections.map(function(c) { return c.name; });
assert.ok(names.indexOf('gh5712') !== -1);
});
});
});
it('should allow closing a closed connection', function(done) {
const db = mongoose.createConnection();
assert.equal(db.readyState, 0);
db.close(done);
});
it('should accept mongodb://aaron:psw@localhost:27000/fake', function(done) {
const db = mongoose.createConnection('mongodb://aaron:psw@localhost:27000/fake', { useNewUrlParser: true }, () => {
db.close(done);
});
assert.equal(db.pass, 'psw');
assert.equal(db.user, 'aaron');
assert.equal(db.name, 'fake');
assert.equal(db.host, 'localhost');
assert.equal(db.port, 27000);
});
it('should accept unix domain sockets', function(done) {
const host = encodeURIComponent('/tmp/mongodb-27017.sock');
const db = mongoose.createConnection(`mongodb://aaron:psw@${host}/fake`, { useNewUrlParser: true });
db.catch(() => {});
assert.equal(db.name, 'fake');
assert.equal(db.host, '/tmp/mongodb-27017.sock');
assert.equal(db.pass, 'psw');
assert.equal(db.user, 'aaron');
db.close();
done();
});
describe('errors', function() {
it('.catch() means error does not get thrown (gh-5229)', function(done) {
const db = mongoose.createConnection();
db.openUri('fail connection').catch(function(error) {
assert.ok(error);
done();
});
});
it('promise is rejected even if there is an error event listener (gh-7850)', function(done) {
const db = mongoose.createConnection();
let called = 0;
db.on('error', () => ++called);
db.openUri('fail connection').catch(function(error) {
assert.ok(error);
setTimeout(() => {
assert.equal(called, 1);
done();
}, 0);
});
});
it('readyState is disconnected if initial connection fails (gh-6244)', function() {
const db = mongoose.createConnection();
return co(function*() {
let threw = false;
try {
yield db.openUri('fail connection');
} catch (err) {
assert.ok(err);
threw = true;
}
assert.ok(threw);
assert.strictEqual(db.readyState, 0);
});
});
});
describe('connect callbacks', function() {
it('execute with user:pwd connection strings', function(done) {
const db = mongoose.createConnection('mongodb://aaron:psw@localhost:27000/fake', { useNewUrlParser: true }, function() {
done();
});
db.catch(() => {});
db.on('error', function(err) {
assert.ok(err);
});
db.close();
});
it('execute without user:pwd connection strings', function(done) {
const db = mongoose.createConnection('mongodb://localhost/fake', { useNewUrlParser: true }, function() {
});
db.on('error', function(err) {
assert.ok(err);
});
assert.equal(typeof db.options, 'object');
assert.equal(db.user, undefined);
assert.equal(db.name, 'fake');
assert.equal(db.host, 'localhost');
assert.equal(db.port, 27017);
db.close();
setTimeout(done, 10);
});
it('should return an error if malformed uri passed', function(done) {
const db = mongoose.createConnection('mongodb:///fake', { useNewUrlParser: true }, function(err) {
assert.ok(/hostname/.test(err.message));
done();
});
db.close();
assert.ok(!db.options);
});
it('should use admin db if not specified and user/pass specified', function(done) {
const db = mongoose.createConnection('mongodb://u:p@localhost/admin', { useNewUrlParser: true }, function() {
done();
});
assert.equal(typeof db.options, 'object');
assert.equal(db.name, 'admin');
assert.equal(db.host, 'localhost');
assert.equal(db.port, 27017);
db.close();
});
});
describe('errors', function() {
it('event fires with one listener', function(done) {
this.timeout(1500);
const db = mongoose.createConnection('mongodb://bad.notadomain/fakeeee?connectTimeoutMS=100');
db.catch(() => {});
db.on('error', function() {
// this callback has no params which triggered the bug #759
db.close();
done();
});
});
it('should occur without hanging when password with special chars is used (gh-460)', function(done) {
mongoose.createConnection('mongodb://aaron:ps#w@localhost/fake?connectTimeoutMS=500', function(err) {
assert.ok(err);
done();
});
});
});
describe('.model()', function() {
let db;
before(function() {
db = start();
});
after(function(done) {
db.close(done);
});
it('allows passing a schema', function(done) {
const MyModel = db.model('MyModelasdf', new Schema({
name: String
}));
assert.ok(MyModel.schema instanceof Schema);
assert.ok(MyModel.prototype.schema instanceof Schema);
const m = new MyModel({name: 'aaron'});
assert.equal(m.name, 'aaron');
done();
});
it('should properly assign the db', function(done) {
const A = mongoose.model('testing853a', new Schema({x: String}), 'testing853-1');
const B = mongoose.model('testing853b', new Schema({x: String}), 'testing853-2');
const C = B.model('testing853a');
assert.ok(C === A);
done();
});
it('prevents overwriting pre-existing models', function(done) {
const name = 'gh-1209-a';
db.model(name, new Schema);
assert.throws(function() {
db.model(name, new Schema);
}, /Cannot overwrite `gh-1209-a` model/);
done();
});
it('allows passing identical name + schema args', function(done) {
const name = 'gh-1209-b';
const schema = new Schema;
const model = db.model(name, schema);
db.model(name, model.schema);
done();
});
it('throws on unknown model name', function(done) {
assert.throws(function() {
db.model('iDoNotExist!');
}, /Schema hasn't been registered/);
done();
});
it('uses the passed schema when global model exists with same name (gh-1209)', function(done) {
const s1 = new Schema({one: String});
const s2 = new Schema({two: Number});
const db = start();
const A = mongoose.model('gh-1209-a', s1);
const B = db.model('gh-1209-a', s2);
assert.ok(A.schema !== B.schema);
assert.ok(A.schema.paths.one);
assert.ok(B.schema.paths.two);
assert.ok(!B.schema.paths.one);
assert.ok(!A.schema.paths.two);
// reset
delete db.models['gh-1209-a'];
const C = db.model('gh-1209-a');
assert.ok(C.schema === A.schema);
db.close();
done();
});
describe('get existing model with not existing collection in db', function() {
it('must return exiting collection with all collection options', function(done) {
mongoose.model('some-th-1458', new Schema({test: String}, {capped: {size: 1000, max: 10}}));
const m = db.model('some-th-1458');
assert.equal(1000, m.collection.opts.capped.size);
assert.equal(10, m.collection.opts.capped.max);
done();
});
});
describe('passing collection name', function() {
describe('when model name already exists', function() {
it('returns a new uncached model', function(done) {
const s1 = new Schema({a: []});
const name = 'non-cached-collection-name';
const A = db.model(name, s1);
const B = db.model(name);
const C = db.model(name, 'alternate');
assert.ok(A.collection.name === B.collection.name);
assert.ok(A.collection.name !== C.collection.name);
assert.ok(db.models[name].collection.name !== C.collection.name);
assert.ok(db.models[name].collection.name === A.collection.name);
done();
});
});
});
describe('passing object literal schemas', function() {
it('works', function(done) {
const A = db.model('A', {n: [{age: 'number'}]});
const a = new A({n: [{age: '47'}]});
assert.strictEqual(47, a.n[0].age);
a.save(function(err) {
assert.ifError(err);
A.findById(a, function(err) {
assert.ifError(err);
assert.strictEqual(47, a.n[0].age);
done();
});
});
});
});
});
it('force close (gh-5664)', function(done) {
const opts = {};
const db = mongoose.createConnection('mongodb://localhost:27017/test', opts);
const coll = db.collection('Test');
db.then(function() {
setTimeout(function() {
coll.insertOne({x:1}, function(error) {
assert.ok(error);
done();
});
}, 100);
// Force close
db.close(true);
});
});
it('force close with connection created after close (gh-5664)', function(done) {
const opts = {};
const db = mongoose.createConnection('mongodb://localhost:27017/test', opts);
db.then(function() {
setTimeout(function() {
// TODO: enforce error.message, right now get a confusing error
/*db.collection('Test').insertOne({x:1}, function(error) {
assert.ok(error);
//assert.ok(error.message.indexOf('pool was destroyed') !== -1, error.message);
done();
});*/
let threw = false;
try {
db.collection('Test').insertOne({x:1});
} catch (error) {
threw = true;
assert.ok(error);
}
assert.ok(threw);
done();
}, 100);
// Force close
db.close(true);
});
});
it('bufferCommands (gh-5720)', function(done) {
let opts = { bufferCommands: false };
let db = mongoose.createConnection('mongodb://localhost:27017/test', opts);
let M = db.model('gh5720', new Schema({}));
assert.ok(!M.collection.buffer);
db.close();
opts = { bufferCommands: true };
db = mongoose.createConnection('mongodb://localhost:27017/test', opts);
M = db.model('gh5720', new Schema({}, { bufferCommands: false }));
assert.ok(!M.collection.buffer);
db.close();
opts = { bufferCommands: true };
db = mongoose.createConnection('mongodb://localhost:27017/test', opts);
M = db.model('gh5720', new Schema({}));
assert.ok(M.collection.buffer);
db.close(done);
});
it('dbName option (gh-6106)', function() {
const opts = { dbName: 'bacon' };
return mongoose.createConnection('mongodb://localhost:27017/test', opts).then(db => {
assert.equal(db.name, 'bacon');
db.close();
});
});
it('startSession() (gh-6653)', function() {
const conn = mongoose.createConnection('mongodb://localhost:27017/test');
let lastUse;
let session;
return conn.startSession().
then(_session => {
session = _session;
assert.ok(session);
lastUse = session.serverSession.lastUse;
return new Promise(resolve => setTimeout(resolve, 1));
}).then(() => {
return conn.model('Test', new Schema({})).findOne({}, null, { session });
}).
then(() => {
assert.ok(session.serverSession.lastUse > lastUse);
conn.close();
});
});
describe('modelNames()', function() {
it('returns names of all models registered on it', function(done) {
const m = new mongoose.Mongoose;
m.model('root', {x: String});
const another = m.model('another', {x: String});
another.discriminator('discriminated', new Schema({x: String}));
const db = m.createConnection();
db.model('something', {x: String});
let names = db.modelNames();
assert.ok(Array.isArray(names));
assert.equal(names.length, 1);
assert.equal(names[0], 'something');
names = m.modelNames();
assert.ok(Array.isArray(names));
assert.equal(names.length, 3);
assert.equal(names[0], 'root');
assert.equal(names[1], 'another');
assert.equal(names[2], 'discriminated');
db.close(done);
});
});
describe('connection pool sharing: ', function() {
it('works', function(done) {
const db = mongoose.createConnection('mongodb://localhost:27017/mongoose1');
const db2 = db.useDb('mongoose2');
assert.equal('mongoose2', db2.name);
assert.equal('mongoose1', db.name);
assert.equal(db2.port, db.port);
assert.equal(db2.replica, db.replica);
assert.equal(db2.hosts, db.hosts);
assert.equal(db2.host, db.host);
assert.equal(db2.port, db.port);
assert.equal(db2.user, db.user);
assert.equal(db2.pass, db.pass);
assert.deepEqual(db.options, db2.options);
db2.close(done);
});
it('saves correctly', function(done) {
const db = start();
const db2 = db.useDb('mongoose-test-2');
const schema = new Schema({
body: String,
thing: Number
});
const m1 = db.model('testMod', schema);
const m2 = db2.model('testMod', schema);
m1.create({body: 'this is some text', thing: 1}, function(err, i1) {
assert.ifError(err);
m2.create({body: 'this is another body', thing: 2}, function(err, i2) {
assert.ifError(err);
m1.findById(i1.id, function(err, item1) {
assert.ifError(err);
assert.equal('this is some text', item1.body);
assert.equal(1, item1.thing);
m2.findById(i2.id, function(err, item2) {
assert.ifError(err);
assert.equal('this is another body', item2.body);
assert.equal(2, item2.thing);
// validate the doc doesn't exist in the other db
m1.findById(i2.id, function(err, nothing) {
assert.ifError(err);
assert.strictEqual(null, nothing);
m2.findById(i1.id, function(err, nothing) {
assert.ifError(err);
assert.strictEqual(null, nothing);
db2.close(done);
});
});
});
});
});
});
});
it('emits connecting events on both', function(done) {
const db = mongoose.createConnection();
const db2 = db.useDb('mongoose-test-2');
let hit = false;
db2.on('connecting', function() {
hit && close();
hit = true;
});
db.on('connecting', function() {
hit && close();
hit = true;
});
db.openUri(start.uri);
function close() {
db.close(done);
}
});
it('emits connected events on both', function(done) {
const db = mongoose.createConnection();
const db2 = db.useDb('mongoose-test-2');
let hit = false;
db2.on('connected', function() {
hit && close();
hit = true;
});
db.on('connected', function() {
hit && close();
hit = true;
});
db.openUri(start.uri);
function close() {
db.close(done);
}
});
it('emits open events on both', function(done) {
const db = mongoose.createConnection();
const db2 = db.useDb('mongoose-test-2');
let hit = false;
db2.on('open', function() {
hit && close();
hit = true;
});
db.on('open', function() {
hit && close();
hit = true;
});
db.openUri(start.uri);
function close() {
db.close(done);
}
});
it('emits disconnecting events on both, closing initial db', function(done) {
const db = mongoose.createConnection();
const db2 = db.useDb('mongoose-test-2');
let hit = false;
db2.on('disconnecting', function() {
hit && done();
hit = true;
});
db.on('disconnecting', function() {
hit && done();
hit = true;
});
db.on('open', function() {
db.close();
});
db.openUri(start.uri);
});
it('emits disconnecting events on both, closing secondary db', function(done) {
const db = mongoose.createConnection();
const db2 = db.useDb('mongoose-test-2');
let hit = false;
db2.on('disconnecting', function() {
hit && done();
hit = true;
});
db.on('disconnecting', function() {
hit && done();
hit = true;
});
db.on('open', function() {
db2.close();
});
db.openUri(start.uri);
});
it('emits disconnected events on both, closing initial db', function(done) {
const db = mongoose.createConnection();
const db2 = db.useDb('mongoose-test-2');
let hit = false;
db2.on('disconnected', function() {
hit && done();
hit = true;
});
db.on('disconnected', function() {
hit && done();
hit = true;
});
db.on('open', function() {
db.close();
});
db.openUri(start.uri);
});