-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
unit_db_list_collections.test.js
76 lines (67 loc) · 2.13 KB
/
unit_db_list_collections.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
'use strict';
const mock = require('../tools/mock');
const expect = require('chai').expect;
describe('db.listCollections', function () {
const testHarness = {};
afterEach(() => mock.cleanup());
beforeEach(() => {
return mock.createServer().then(server => {
server.setMessageHandler(request => {
const doc = request.document;
if (doc.ismaster || doc.hello) {
return request.reply(Object.assign({}, mock.DEFAULT_ISMASTER_36));
}
if (doc.listCollections) {
return request.reply({
ok: 1,
cursor: {
id: 0,
ns: 'test.$cmd.listCollections',
firstBatch: [{ name: 'test', type: 'collection' }]
}
});
}
});
testHarness.server = server;
});
});
[
{
description: 'should always send nameOnly option, defaulting to false',
command: db => db.listCollections().toArray(() => {}),
listCollectionsValue: false
},
{
description: 'should propagate the nameOnly option',
command: db => db.listCollections({}, { nameOnly: true }).toArray(() => {}),
listCollectionsValue: true
},
{
description: 'should send nameOnly: true for db.collections',
command: db => db.collections(() => {}),
listCollectionsValue: true
}
].forEach(config => {
function testFn(done) {
const configuration = this.configuration;
const client = configuration.newClient(`mongodb://${testHarness.server.uri()}/test`, {
monitorCommands: true
});
client.connect((err, client) => {
const db = client.db('foo');
client.on('commandStarted', e => {
if (e.commandName === 'listCollections') {
try {
expect(e).to.have.nested.property('command.nameOnly', config.listCollectionsValue);
client.close(done);
} catch (err) {
client.close(() => done(err));
}
}
});
config.command(db);
});
}
it(config.description, { test: testFn, metadata: { requires: { mongodb: '>=2.7.6' } } });
});
});