Skip to content

Commit

Permalink
fix(NODE-5102): listDatabases nameOnly setting is sent as NaN (#3742)
Browse files Browse the repository at this point in the history
Co-authored-by: Warren James <warren.james@mongodb.com>
  • Loading branch information
redixhumayun and W-A-James authored Jul 3, 2023
1 parent a329748 commit b97132e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/operations/list_databases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ export class ListDatabasesOperation extends CommandOperation<ListDatabasesResult
callback: Callback<ListDatabasesResult>
): 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) {
Expand Down
48 changes: 48 additions & 0 deletions test/integration/enumerate_databases.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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({
Expand Down
40 changes: 40 additions & 0 deletions test/unit/operations/list_databases.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
});

0 comments on commit b97132e

Please sign in to comment.