Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 71 additions & 31 deletions packages/shell-api/src/explainable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,46 +110,86 @@ describe('Explainable', function () {
});

describe('find', function () {
let cursorStub;
let explainResult;
beforeEach(async function () {
explainResult = { ok: 1 };
context('without options', function () {
let cursorStub;
let explainResult;

const cursorSpy = {
explain: sinon.spy(() => explainResult),
} as unknown;
collection.find = sinon.spy(() => Promise.resolve(cursorSpy as Cursor));
beforeEach(async function () {
explainResult = { ok: 1 };

cursorStub = await explainable.find({ query: 1 }, { projection: 1 });
});
const cursorSpy = {
explain: sinon.spy(() => explainResult),
} as unknown;
collection.find = sinon.spy(() =>
Promise.resolve(cursorSpy as Cursor)
);

it('calls collection.find with arguments', function () {
expect(collection.find).to.have.been.calledOnceWithExactly(
{ query: 1 },
{ projection: 1 }
);
});
cursorStub = await explainable.find({ query: 1 }, { projection: 1 });
});

it('calls collection.find with arguments', function () {
expect(collection.find).to.have.been.calledOnceWithExactly(
{ query: 1 },
{ projection: 1 },
{}
);
});

it('returns an cursor that has toShellResult when evaluated', async function () {
expect((await toShellResult(cursorStub)).type).to.equal(
'ExplainableCursor'
);
});

context(
'when calling toShellResult().printable on the result',
function () {
it('calls explain with verbosity', function () {
expect(cursorStub._verbosity).to.equal('queryPlanner');
});

it('returns an cursor that has toShellResult when evaluated', async function () {
expect((await toShellResult(cursorStub)).type).to.equal(
'ExplainableCursor'
it('returns the explain result', async function () {
expect((await toShellResult(cursorStub)).printable).to.equal(
explainResult
);
});
}
);
});

context(
'when calling toShellResult().printable on the result',
function () {
it('calls explain with verbosity', function () {
expect(cursorStub._verbosity).to.equal('queryPlanner');
});
context('with options', function () {
let cursorStub;
let explainResult;

beforeEach(async function () {
explainResult = { ok: 1 };

const cursorSpy = {
explain: sinon.spy(() => explainResult),
} as unknown;
collection.find = sinon.spy(() =>
Promise.resolve(cursorSpy as Cursor)
);

it('returns the explain result', async function () {
expect((await toShellResult(cursorStub)).printable).to.equal(
explainResult
);
cursorStub = await explainable.find({}, undefined, {
collation: { locale: 'simple' },
});
}
);
});

it('calls collection.find with arguments', function () {
expect(collection.find).to.have.been.calledOnceWithExactly(
{},
undefined,
{ collation: { locale: 'simple' } }
);
});

it('returns an cursor that has toShellResult when evaluated', async function () {
expect((await toShellResult(cursorStub)).type).to.equal(
'ExplainableCursor'
);
});
});
});

describe('aggregate', function () {
Expand Down
6 changes: 4 additions & 2 deletions packages/shell-api/src/explainable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
FindOneAndDeleteOptions,
FindOneAndReplaceOptions,
FindOneAndUpdateOptions,
FindOptions,
} from '@mongosh/service-provider-core';

@shellApiClassDefault
Expand Down Expand Up @@ -97,11 +98,12 @@ export default class Explainable extends ShellApiWithMongoClass {
@returnsPromise
async find(
query?: Document,
projection?: Document
projection?: Document,
options: FindOptions = {}
): Promise<ExplainableCursor> {
this._emitExplainableApiCall('find', { query, projection });

const cursor = await this._collection.find(query, projection);
const cursor = await this._collection.find(query, projection, options);
return new ExplainableCursor(this._mongo, cursor, this._verbosity);
}

Expand Down
13 changes: 13 additions & 0 deletions packages/shell-api/src/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,19 @@ describe('Shell API (integration)', function () {
'serverInfo',
]);
});

describe('after server 4.4', function () {
skipIfServerVersion(testServer, '<= 4.4');
it('the explainable cursor reflects collation', async function () {
const cursor = await explainable.find({}, undefined, {
collation: { locale: 'simple' },
});
const result = await toShellResult(cursor);
expect(result.printable.command.collation.locale).to.be.equal(
'simple'
);
});
});
});

describe('aggregate', function () {
Expand Down