Skip to content

Commit f7b8f04

Browse files
committed
test: fix crud and explain tests
1 parent 1113e1b commit f7b8f04

File tree

2 files changed

+5
-48
lines changed

2 files changed

+5
-48
lines changed

src/operations/find_one_operation.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type Db } from '..';
22
import { type Document, pluckBSONSerializeOptions } from '../bson';
33
import { type OnDemandDocumentDeserializeOptions } from '../cmap/wire_protocol/on_demand/document';
4+
import { decorateWithExplain, validateExplainTimeoutOptions } from '../explain';
45
import type { Server } from '../sdam/server';
56
import type { ClientSession } from '../sessions';
67
import { type TimeoutContext } from '../timeout';
@@ -55,8 +56,8 @@ export class FindOneOperation<TSchema = any> extends CommandOperation<TSchema> {
5556
// Explicitly set the limit to 1 and singleBatch to true for all commands, per the spec.
5657
// noCursorTimeout must be unset as well as batchSize.
5758
// See: https://github.com/mongodb/specifications/blob/master/source/crud/crud.md#findone-api-details
58-
command.limit = 1;
59-
command.singleBatch = true;
59+
//command.limit = 1;
60+
//command.singleBatch = true;
6061
if (command.noCursorTimeout != null) {
6162
delete command.noCursorTimeout;
6263
}
@@ -67,9 +68,9 @@ export class FindOneOperation<TSchema = any> extends CommandOperation<TSchema> {
6768
const response = await super.executeCommand(server, session, command, timeoutContext);
6869
// In this case since we are just running a command, the response is a document with
6970
// a single batch cursor, not an OnDemandDocument.
70-
const document = response.cursor?.firstBatch?.[0] ?? null;
71+
const document = this.explain ? response : (response.cursor?.firstBatch?.[0] ?? null);
7172
return document;
7273
}
7374
}
7475

75-
defineAspects(FindOneOperation, [Aspect.READ_OPERATION, Aspect.RETRYABLE]);
76+
defineAspects(FindOneOperation, [Aspect.READ_OPERATION, Aspect.RETRYABLE, Aspect.EXPLAINABLE]);

test/integration/crud/crud_api.test.ts

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -98,50 +98,6 @@ describe('CRUD API', function () {
9898
await collection.drop().catch(() => null);
9999
await client.close();
100100
});
101-
102-
describe('when the operation succeeds', () => {
103-
it('the cursor for findOne is closed', async function () {
104-
const spy = sinon.spy(Collection.prototype, 'find');
105-
const result = await collection.findOne({});
106-
expect(result).to.deep.equal({ _id: 1 });
107-
expect(events.at(0)).to.be.instanceOf(CommandSucceededEvent);
108-
expect(spy.returnValues.at(0)).to.have.property('closed', true);
109-
expect(spy.returnValues.at(0)).to.have.nested.property('session.hasEnded', true);
110-
});
111-
});
112-
113-
describe('when the find operation fails', () => {
114-
beforeEach(async function () {
115-
const failPoint: FailPoint = {
116-
configureFailPoint: 'failCommand',
117-
mode: 'alwaysOn',
118-
data: {
119-
failCommands: ['find'],
120-
// 1 == InternalError, but this value not important to the test
121-
errorCode: 1
122-
}
123-
};
124-
await client.db().admin().command(failPoint);
125-
});
126-
127-
afterEach(async function () {
128-
const failPoint: FailPoint = {
129-
configureFailPoint: 'failCommand',
130-
mode: 'off',
131-
data: { failCommands: ['find'] }
132-
};
133-
await client.db().admin().command(failPoint);
134-
});
135-
136-
it('the cursor for findOne is closed', async function () {
137-
const spy = sinon.spy(Collection.prototype, 'find');
138-
const error = await collection.findOne({}).catch(error => error);
139-
expect(error).to.be.instanceOf(MongoServerError);
140-
expect(events.at(0)).to.be.instanceOf(CommandFailedEvent);
141-
expect(spy.returnValues.at(0)).to.have.property('closed', true);
142-
expect(spy.returnValues.at(0)).to.have.nested.property('session.hasEnded', true);
143-
});
144-
});
145101
});
146102

147103
describe('countDocuments()', () => {

0 commit comments

Comments
 (0)