-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(listCollections): add support for nameOnly option
Fixes NODE-1471
- Loading branch information
1 parent
552c50a
commit d2d0367
Showing
2 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
'use strict'; | ||
|
||
const mock = require('mongodb-mock-server'); | ||
const expect = require('chai').expect; | ||
const MongoClient = require('../../lib/mongo_client'); | ||
|
||
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) { | ||
return request.reply(Object.assign({}, mock.DEFAULT_ISMASTER)); | ||
} | ||
|
||
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.createCollection', | ||
command: db => db.createCollection('foo', () => {}), | ||
listCollectionsValue: true | ||
}, | ||
{ | ||
description: 'should send nameOnly: true for db.collections', | ||
command: db => db.collections(() => {}), | ||
listCollectionsValue: true | ||
}, | ||
{ | ||
description: 'should send nameOnly: true for db.collection', | ||
command: db => db.collection('foo', { strict: true }, () => {}), | ||
listCollectionsValue: true | ||
} | ||
].forEach(config => { | ||
function testFn(done) { | ||
const client = new MongoClient(`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' } } }); | ||
}); | ||
}); |