diff --git a/src/operations/list_databases.ts b/src/operations/list_databases.ts index 9046c2963e..6979f566e3 100644 --- a/src/operations/list_databases.ts +++ b/src/operations/list_databases.ts @@ -40,8 +40,9 @@ export class ListDatabasesOperation extends CommandOperation ): void { const cmd: Document = { listDatabases: 1 }; - if (this.options.nameOnly) { - cmd.nameOnly = Number(cmd.nameOnly); + + if (typeof this.options.nameOnly === 'boolean') { + cmd.nameOnly = this.options.nameOnly; } if (this.options.filter) { diff --git a/test/integration/enumerate_databases.test.ts b/test/integration/enumerate_databases.test.ts index 9234973eb6..decb0ac892 100644 --- a/test/integration/enumerate_databases.test.ts +++ b/test/integration/enumerate_databases.test.ts @@ -1,4 +1,5 @@ import { expect } from 'chai'; +import { once } from 'events'; import { type AddUserOptions, type MongoClient, MongoServerError } from '../mongodb'; import { TestBuilder, UnifiedTestSuiteBuilder } from '../tools/utils'; @@ -141,6 +142,53 @@ describe('listDatabases()', function () { ); }); + describe('nameOnly option', function () { + let client: MongoClient; + const nameOnlyOptions = [true, false, undefined]; + const optionToExpectation = { + true: 'with nameOnly = true', + false: 'with nameOnly = false', + undefined: 'without nameOnly field' + }; + + beforeEach(async function () { + client = await this.configuration.newClient({}, { monitorCommands: true }).connect(); + await client.db('test').createCollection('test'); + }); + + afterEach(async function () { + if (client) { + await client.db(`test`).dropDatabase(); + await client.close(); + } + }); + + for (const nameOnly of nameOnlyOptions) { + context(`when options.nameOnly is ${nameOnly ?? 'not defined'}`, function () { + it(`sends command ${optionToExpectation[String(nameOnly)]}`, async function () { + const promise = once(client, 'commandStarted'); + await client.db().admin().listDatabases({ nameOnly }); + + const commandStarted = (await promise)[0]; + expect(commandStarted.command).to.haveOwnProperty('listDatabases', 1); + + switch (nameOnly) { + case true: + expect(commandStarted.command).to.have.property('nameOnly', true); + break; + case false: + expect(commandStarted.command).to.have.property('nameOnly', false); + break; + case undefined: + expect(commandStarted.command).to.not.have.property('nameOnly'); + break; + default: + expect.fail(`Unrecognized nameOnly value: ${nameOnly}`); + } + }); + }); + } + }); UnifiedTestSuiteBuilder.describe('comment option') .createEntities(UnifiedTestSuiteBuilder.defaultEntities) .initialData({ diff --git a/test/unit/operations/list_databases.test.ts b/test/unit/operations/list_databases.test.ts new file mode 100644 index 0000000000..b5f49f4b0a --- /dev/null +++ b/test/unit/operations/list_databases.test.ts @@ -0,0 +1,40 @@ +import { expect } from 'chai'; + +import { ListDatabasesOperation, MongoDBNamespace } from '../../mongodb'; + +const mockDB = { + s: { + namespace: { + withCollection() { + return new MongoDBNamespace('test', 'test'); + } + } + } +}; + +describe('ListDatabasesOperation', function () { + describe('#constructor', function () { + context('when nameOnly is provided', function () { + context('when nameOnly is true', function () { + it('sets nameOnly to true', function () { + const operation = new ListDatabasesOperation(mockDB, { nameOnly: true }); + expect(operation.options).to.have.property('nameOnly', true); + }); + }); + + context('when nameOnly is false', function () { + it('sets nameOnly to false', function () { + const operation = new ListDatabasesOperation(mockDB, { nameOnly: false }); + expect(operation.options).to.have.property('nameOnly', false); + }); + }); + }); + + context('when nameOnly is not specified', function () { + it('nameOnly is undefined', function () { + const operation = new ListDatabasesOperation(mockDB, {}); + expect(operation.options).not.to.have.property('nameOnly'); + }); + }); + }); +});