Skip to content

Commit

Permalink
test: success and fail with server side errors, check cursor state
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Oct 18, 2023
1 parent 9835b6d commit 241f1c5
Showing 1 changed file with 71 additions and 4 deletions.
75 changes: 71 additions & 4 deletions test/integration/crud/crud_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import * as sinon from 'sinon';

import {
AbstractCursor,
Collection,
CommandFailedEvent,
CommandSucceededEvent,
type MongoClient,
MongoError,
MongoServerError,
ObjectId,
ReturnDocument
} from '../../mongodb';
import { type FailPoint } from '../../tools/utils';
import { assert as test } from '../shared';

// instanceof cannot be use reliably to detect the new models in js due to scoping and new
Expand Down Expand Up @@ -70,10 +75,72 @@ describe('CRUD API', function () {
await client.close();
});

it('findOne calls cursor.close()', async function () {
const spy = sinon.spy(AbstractCursor.prototype, 'close');
await client.db().collection('t').findOne({});
expect(spy).to.be.calledOnce;
describe('findOne()', () => {
let client: MongoClient;
let events;
let collection: Collection<{ _id: number }>;

beforeEach(async function () {
client = this.configuration.newClient({ monitorCommands: true });
events = [];
client.on('commandSucceeded', commandSucceeded =>
commandSucceeded.commandName === 'find' ? events.push(commandSucceeded) : null
);
client.on('commandFailed', commandFailed =>
commandFailed.commandName === 'find' ? events.push(commandFailed) : null
);

collection = client.db('findOne').collection('findOne');
await collection.drop().catch(() => null);
await collection.insertMany([{ _id: 1 }, { _id: 2 }]);
});

afterEach(async () => {
await collection.drop().catch(() => null);
await client.close();
});

describe('when the operation succeeds', () => {
it('the cursor for findOne is closed', async function () {
const spy = sinon.spy(Collection.prototype, 'find');
const result = await collection.findOne({});
expect(result).to.deep.equal({ _id: 1 });
expect(events.at(0)).to.be.instanceOf(CommandSucceededEvent);
expect(spy.returnValues.at(0)).to.have.property('closed', true);
});
});

describe('when the find operation fails', () => {
beforeEach(async () => {
const failPoint: FailPoint = {
configureFailPoint: 'failCommand',
mode: 'alwaysOn',
data: {
failCommands: ['find'],
// 1 == InternalError, but this value not important to the test
errorCode: 1
}
};
await client.db().admin().command(failPoint);
});

afterEach(async () => {
const failPoint: FailPoint = {
configureFailPoint: 'failCommand',
mode: 'off',
data: { failCommands: ['find'] }
};
await client.db().admin().command(failPoint);
});

it('the cursor for findOne is closed', async function () {
const spy = sinon.spy(Collection.prototype, 'find');
const error = await collection.findOne({}).catch(error => error);
expect(error).to.be.instanceOf(MongoServerError);
expect(events.at(0)).to.be.instanceOf(CommandFailedEvent);
expect(spy.returnValues.at(0)).to.have.property('closed', true);
});
});
});

context('when creating a cursor with find', () => {
Expand Down

0 comments on commit 241f1c5

Please sign in to comment.