|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const sinon = require('sinon'); |
| 4 | +const { expect } = require('chai'); |
| 5 | +const { Long } = require('../../../src/bson'); |
| 6 | +const { GetMoreOperation } = require('../../../src/operations/get_more'); |
| 7 | +const { Server } = require('../../../src/sdam/server'); |
| 8 | +const { ClientSession } = require('../../../src/sessions'); |
| 9 | +const { ReadPreference } = require('../../../src/read_preference'); |
| 10 | +const { Aspect } = require('../../../src/operations/operation'); |
| 11 | +const { MongoRuntimeError } = require('../../../src/error'); |
| 12 | + |
| 13 | +describe('GetMoreOperation', function () { |
| 14 | + const ns = 'db.coll'; |
| 15 | + const cursorId = Object.freeze(Long.fromNumber(1)); |
| 16 | + const options = Object.freeze({ |
| 17 | + batchSize: 100, |
| 18 | + comment: 'test', |
| 19 | + maxTimeMS: 500, |
| 20 | + readPreference: ReadPreference.primary |
| 21 | + }); |
| 22 | + |
| 23 | + describe('#constructor', function () { |
| 24 | + const server = sinon.createStubInstance(Server, {}); |
| 25 | + const operation = new GetMoreOperation(ns, cursorId, server, options); |
| 26 | + |
| 27 | + it('sets the namespace', function () { |
| 28 | + expect(operation.ns).to.equal(ns); |
| 29 | + }); |
| 30 | + |
| 31 | + it('sets the cursorId', function () { |
| 32 | + expect(operation.cursorId).to.equal(cursorId); |
| 33 | + }); |
| 34 | + |
| 35 | + it('sets the server', function () { |
| 36 | + expect(operation.server).to.equal(server); |
| 37 | + }); |
| 38 | + |
| 39 | + it('sets the options', function () { |
| 40 | + expect(operation.options).to.deep.equal(options); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('#execute', function () { |
| 45 | + context('when the server is the same as the instance', function () { |
| 46 | + const getMoreStub = sinon.stub().yields(undefined); |
| 47 | + const server = sinon.createStubInstance(Server, { |
| 48 | + getMore: getMoreStub |
| 49 | + }); |
| 50 | + const session = sinon.createStubInstance(ClientSession); |
| 51 | + const opts = { ...options, session }; |
| 52 | + const operation = new GetMoreOperation(ns, cursorId, server, opts); |
| 53 | + |
| 54 | + it('executes a getmore on the provided server', function (done) { |
| 55 | + const callback = () => { |
| 56 | + const call = getMoreStub.getCall(0); |
| 57 | + expect(getMoreStub.calledOnce).to.be.true; |
| 58 | + expect(call.args[0]).to.equal(ns); |
| 59 | + expect(call.args[1]).to.equal(cursorId); |
| 60 | + expect(call.args[2]).to.deep.equal(opts); |
| 61 | + done(); |
| 62 | + }; |
| 63 | + operation.execute(server, session, callback); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + context('when the server is not the same as the instance', function () { |
| 68 | + const getMoreStub = sinon.stub().yields(undefined); |
| 69 | + const server = sinon.createStubInstance(Server, { |
| 70 | + getMore: getMoreStub |
| 71 | + }); |
| 72 | + const newServer = sinon.createStubInstance(Server, { |
| 73 | + getMore: getMoreStub |
| 74 | + }); |
| 75 | + const session = sinon.createStubInstance(ClientSession); |
| 76 | + const opts = { ...options, session }; |
| 77 | + const operation = new GetMoreOperation(ns, cursorId, server, opts); |
| 78 | + |
| 79 | + it('errors in the callback', function (done) { |
| 80 | + const callback = error => { |
| 81 | + expect(error).to.be.instanceOf(MongoRuntimeError); |
| 82 | + expect(error.message).to.equal('Getmore must run on the same server operation began on'); |
| 83 | + done(); |
| 84 | + }; |
| 85 | + operation.execute(newServer, session, callback); |
| 86 | + }); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('#hasAspect', function () { |
| 91 | + const server = sinon.createStubInstance(Server, {}); |
| 92 | + const operation = new GetMoreOperation(ns, cursorId, server, options); |
| 93 | + |
| 94 | + context('when the aspect is cursor iterating', function () { |
| 95 | + it('returns true', function () { |
| 96 | + expect(operation.hasAspect(Aspect.CURSOR_ITERATING)).to.be.true; |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + context('when the aspect is read', function () { |
| 101 | + it('returns true', function () { |
| 102 | + expect(operation.hasAspect(Aspect.READ_OPERATION)).to.be.true; |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + context('when the aspect is write', function () { |
| 107 | + it('returns false', function () { |
| 108 | + expect(operation.hasAspect(Aspect.WRITE_OPERATION)).to.be.false; |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + context('when the aspect is retryable', function () { |
| 113 | + it('returns false', function () { |
| 114 | + expect(operation.hasAspect(Aspect.RETRYABLE)).to.be.false; |
| 115 | + }); |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments