-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
collection.test.js
164 lines (128 loc) · 3.68 KB
/
collection.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
'use strict';
const start = require('./common');
const Collection = require('../lib/collection');
const assert = require('assert');
const mongoose = start.mongoose;
describe('collections:', function() {
let db = null;
afterEach(async function() {
if (db == null) {
return;
}
await db.close();
db = null;
});
it('should buffer commands until connection is established', async function() {
db = mongoose.createConnection();
const collection = db.collection('test-buffering-collection');
const op = collection.insertOne({ foo: 'bar' }, {});
const uri = start.uri;
await db.openUri(process.env.MONGOOSE_TEST_URI || uri);
const res = await op;
assert.ok(res.insertedId);
const doc = await collection.findOne({ _id: res.insertedId });
assert.strictEqual(doc.foo, 'bar');
await db.close();
});
it('returns a promise if buffering and no callback (gh-7676)', async function() {
const db = mongoose.createConnection();
const collection = db.collection('gh7676');
const promise = collection.insertOne({ foo: 'bar' }, {})
.then(result =>
collection.findOne({ _id: result.insertedId })
).then(doc => {
assert.strictEqual(doc.foo, 'bar');
});
await db.openUri(start.uri);
await promise;
await db.close();
});
it('returns a promise if buffering and callback with find() (gh-14184)', function(done) {
db = mongoose.createConnection();
const collection = db.collection('gh14184');
collection.opts.bufferTimeoutMS = 100;
collection.find({ foo: 'bar' }, {}, (err, docs) => {
assert.ok(err);
assert.ok(err.message.includes('buffering timed out after 100ms'));
assert.equal(docs, undefined);
done();
});
});
it('methods should that throw (unimplemented)', function() {
const collection = new Collection('test', mongoose.connection);
let thrown = false;
try {
collection.getIndexes();
} catch (e) {
assert.ok(/unimplemented/.test(e.message));
thrown = true;
}
assert.ok(thrown);
thrown = false;
try {
collection.updateOne();
} catch (e) {
assert.ok(/unimplemented/.test(e.message));
thrown = true;
}
assert.ok(thrown);
thrown = false;
try {
collection.save();
} catch (e) {
assert.ok(/unimplemented/.test(e.message));
thrown = true;
}
assert.ok(thrown);
thrown = false;
try {
collection.insertOne();
} catch (e) {
assert.ok(/unimplemented/.test(e.message), e.message);
thrown = true;
}
assert.ok(thrown);
thrown = false;
try {
collection.find();
} catch (e) {
assert.ok(/unimplemented/.test(e.message));
thrown = true;
}
assert.ok(thrown);
thrown = false;
try {
collection.findOne();
} catch (e) {
assert.ok(/unimplemented/.test(e.message));
thrown = true;
}
assert.ok(thrown);
thrown = false;
try {
collection.findAndModify();
} catch (e) {
assert.ok(/unimplemented/.test(e.message));
thrown = true;
}
assert.ok(thrown);
thrown = false;
try {
collection.ensureIndex();
} catch (e) {
assert.ok(/unimplemented/.test(e.message));
thrown = true;
}
assert.ok(thrown);
thrown = false;
});
it('buffers for sync methods (gh-10610)', async function() {
db = mongoose.createConnection();
const collection = db.collection('gh10610');
const promise = collection.find({}, {});
const uri = start.uri;
await db.openUri(process.env.MONGOOSE_TEST_URI || uri);
const res = await promise;
assert.equal(typeof res.toArray, 'function');
});
});