Skip to content

Commit

Permalink
fix(NODE-3648): unit test get more execute
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Nov 8, 2021
1 parent 7d57268 commit 6dd5d5f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/operations/get_more.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ export interface GetMoreOptions<TSchema extends Document = Document> extends Ope
export class GetMoreOperation extends AbstractOperation {
cursorId: Long;
options: GetMoreOptions;
server: Server;

constructor(ns: MongoDBNamespace, cursorId: Long, options: GetMoreOptions = {}) {
constructor(ns: MongoDBNamespace, cursorId: Long, server: Server, options: GetMoreOptions = {}) {
super(options);
this.options = options;
this.ns = ns;
this.cursorId = cursorId;
this.server = server;
}

/**
* Although there is a server already associated with the get more operation, the signature
* for execute passes a server so we will just use that one.
*/
execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
this.server = server;

server.getMore(
this.ns,
this.cursorId,
Expand Down
38 changes: 34 additions & 4 deletions test/unit/operations/get_more.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
'use strict';

const sinon = require('sinon');
const { expect } = require('chai');
const { Long } = require('../../../src/bson');
const { GetMoreOperation } = require('../../../src/operations/get_more');
const { Server } = require('../../../src/sdam/server');
const { ClientSession } = require('../../../src/sessions');

describe('GetMoreOperation', function () {
const ns = 'db.coll';
const cursorId = Long.fromNumber(1);
const options = { batchSize: 100, comment: 'test', maxTimeMS: 500 };

describe('#constructor', function () {
const ns = 'db.coll';
const cursorId = Long.fromNumber(1);
const options = { batchSize: 100, comment: 'test', maxTimeMS: 500 };
const operation = new GetMoreOperation(ns, cursorId, options);
const server = sinon.createStubInstance(Server, {});
const operation = new GetMoreOperation(ns, cursorId, server, options);

it('sets the namespace', function () {
expect(operation.ns).to.equal(ns);
Expand All @@ -19,8 +24,33 @@ describe('GetMoreOperation', function () {
expect(operation.cursorId).to.equal(cursorId);
});

it('sets the server', function () {
expect(operation.server).to.equal(server);
});

it('sets the options', function () {
expect(operation.options).to.deep.equal(options);
});
});

describe('#execute', function () {
const getMoreStub = sinon.stub().yields(undefined);
const server = sinon.createStubInstance(Server, {
getMore: getMoreStub
});
const session = sinon.createStubInstance(ClientSession);
const operation = new GetMoreOperation(ns, cursorId, server, options);

it('executes a getmore on the provided server', function (done) {
const callback = () => {
const call = getMoreStub.getCall(0);
expect(getMoreStub.calledOnce).to.be.true;
expect(call.args[0]).to.equal(ns);
expect(call.args[1]).to.equal(cursorId);
expect(call.args[2]).to.deep.equal({ ...options, session });
done();
};
operation.execute(server, session, callback);
});
});
});

0 comments on commit 6dd5d5f

Please sign in to comment.