|
| 1 | +import { expect } from 'chai'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import { promisify } from 'util'; |
| 4 | + |
| 5 | +import { FindOperation } from '../../../src/operations/find'; |
| 6 | +import { Server } from '../../../src/sdam/server'; |
| 7 | +import { ServerDescription } from '../../../src/sdam/server_description'; |
| 8 | +import { Topology } from '../../../src/sdam/topology'; |
| 9 | +import { ns } from '../../../src/utils'; |
| 10 | + |
| 11 | +describe('FindOperation', function () { |
| 12 | + const namespace = ns('db.coll'); |
| 13 | + const options = { |
| 14 | + batchSize: 100 |
| 15 | + }; |
| 16 | + const filter = { |
| 17 | + ts: { $gt: new Date() } |
| 18 | + }; |
| 19 | + |
| 20 | + afterEach(function () { |
| 21 | + sinon.restore(); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('#constructor', function () { |
| 25 | + const operation = new FindOperation(undefined, namespace, filter, options); |
| 26 | + |
| 27 | + it('sets the namespace', function () { |
| 28 | + expect(operation.ns).to.equal(namespace); |
| 29 | + }); |
| 30 | + |
| 31 | + it('sets options', function () { |
| 32 | + expect(operation.options).to.equal(options); |
| 33 | + }); |
| 34 | + |
| 35 | + it('sets filter', function () { |
| 36 | + expect(operation.filter).to.equal(filter); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('#execute', function () { |
| 41 | + context('command construction', () => { |
| 42 | + const namespace = ns('db.collection'); |
| 43 | + const server = new Server(new Topology([], {} as any), new ServerDescription(''), {} as any); |
| 44 | + |
| 45 | + it('should build basic find command with filter', async () => { |
| 46 | + const findOperation = new FindOperation(undefined, namespace, filter); |
| 47 | + const stub = sinon.stub(server, 'command').yieldsRight(); |
| 48 | + await promisify(findOperation.execute.bind(findOperation))(server, undefined); |
| 49 | + expect(stub).to.have.been.calledOnceWith(namespace, { |
| 50 | + find: namespace.collection, |
| 51 | + filter |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should build find command with oplogReplay', async () => { |
| 56 | + const options = { |
| 57 | + oplogReplay: true |
| 58 | + }; |
| 59 | + const findOperation = new FindOperation(undefined, namespace, {}, options); |
| 60 | + const stub = sinon.stub(server, 'command').yieldsRight(); |
| 61 | + await promisify(findOperation.execute.bind(findOperation))(server, undefined); |
| 62 | + expect(stub).to.have.been.calledOnceWith( |
| 63 | + namespace, |
| 64 | + sinon.match.has('oplogReplay', options.oplogReplay) |
| 65 | + ); |
| 66 | + }); |
| 67 | + }); |
| 68 | + }); |
| 69 | +}); |
0 commit comments