Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-5102): fix listDatabases nameOnly setting is sent as NaN #3742

Merged
merged 8 commits into from
Jul 3, 2023
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
61 changes: 61 additions & 0 deletions test/integration/enumerate_databases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,67 @@ describe('listDatabases()', function () {
);
});

describe('nameOnly option', function () {
let client: MongoClient;
const DBS = 10;
const nameOnlyOptions = [true, false, undefined];

beforeEach(async function () {
client = await this.configuration.newClient().connect();
for (let i = 0; i < DBS; i++) {
const db = client.db(`testDb_${i}`);
await db.collection('test').insertOne({ a: 1 });
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
}
});

afterEach(async function () {
if (client) {
for (let i = 0; i < DBS; i++) {
await client.db(`testDb_${i}`).dropDatabase();
}

await client.close();
}
});

for (const nameOnly of nameOnlyOptions) {
context(`when options.nameOnly is ${nameOnly ?? 'not defined'}`, function () {
it(`returns ${optionToExpecation(nameOnly)}`, async function () {
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
const response = await client.db().admin().listDatabases({ nameOnly });

expect(response).to.have.property('databases');
expect(response.databases).to.have.length.gte(DBS);
switch (nameOnly) {
case true:
for (const db of response.databases) {
expect(db).property('name').to.exist;
expect(db).to.not.have.property('sizeOnDisk');
expect(db).to.not.have.property('empty');
}
break;
case false:
case undefined:
for (const db of response.databases) {
expect(db.name).to.exist;
expect(db).property('sizeOnDisk').to.exist;
expect(db).property('empty').to.exist;
}
break;
}
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
});
});
}

function optionToExpecation(nameOnly?: boolean): string {
switch (nameOnly) {
case true:
return 'list of only database names';
case false:
case undefined:
return 'list of entire listDatabases responses';
}
}
});
UnifiedTestSuiteBuilder.describe('comment option')
.createEntities(UnifiedTestSuiteBuilder.defaultEntities)
.initialData({
Expand Down
42 changes: 42 additions & 0 deletions test/unit/operations/list_databases.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 () {
const operation = new ListDatabasesOperation(mockDB, { nameOnly: true });
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
it('sets nameOnly to true', function () {
expect(operation.options).to.have.property('nameOnly', true);
});
});

context('when nameOnly is false', function () {
const operation = new ListDatabasesOperation(mockDB, { nameOnly: false });

it('sets nameOnly to false', function () {
expect(operation.options).to.have.property('nameOnly', false);
});
});
});

context('when nameOnly is not specified', function () {
const operation = new ListDatabasesOperation(mockDB, {});

it('nameOnly is undefined', function () {
expect(operation.options).not.to.have.property('nameOnly');
});
});
});
});