|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const expect = require('chai').expect; |
| 4 | +const makeSinonSandbox = require('./utils').makeSinonSandbox; |
| 5 | +const executeWrite = require('../../../../../lib/wireprotocol/3_6_support/execute_write'); |
| 6 | +const Pool = require('../../../../../lib/connection/pool'); |
| 7 | +const BSON = require('bson'); |
| 8 | +const MongoError = require('../../../../../lib/error').MongoError; |
| 9 | + |
| 10 | +[ |
| 11 | + { type: 'insert', opsField: 'documents' }, |
| 12 | + { type: 'update', opsField: 'updates' }, |
| 13 | + { type: 'delete', opsField: 'deletes' } |
| 14 | +].forEach(opMeta => { |
| 15 | + const TYPE = opMeta.type; |
| 16 | + const OPSFIELD = opMeta.opsField; |
| 17 | + |
| 18 | + describe(`Wire Protocol 3.6 ${TYPE}`, function() { |
| 19 | + const sinon = makeSinonSandbox(); |
| 20 | + let bson, pool, callback; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + bson = sinon.createStubInstance(BSON); |
| 24 | + pool = sinon.createStubInstance(Pool); |
| 25 | + callback = sinon.stub(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should throw if no documents are provided', function() { |
| 29 | + const badValues = [ |
| 30 | + 0, |
| 31 | + 1, |
| 32 | + -1, |
| 33 | + 'darmok', |
| 34 | + 'jalad', |
| 35 | + { delete: 'collection' }, |
| 36 | + null, |
| 37 | + undefined, |
| 38 | + [] |
| 39 | + ]; |
| 40 | + badValues.forEach(badValue => { |
| 41 | + const failingFunction = () => |
| 42 | + executeWrite(bson, pool, TYPE, OPSFIELD, 'darmok.jalad', badValue, {}, callback); |
| 43 | + const stringOfBadValue = JSON.stringify(badValue); |
| 44 | + expect( |
| 45 | + failingFunction, |
| 46 | + `Expected executeWrite to fail when ops === ${stringOfBadValue}, but it succeeded` |
| 47 | + ) |
| 48 | + .to.throw(MongoError) |
| 49 | + .with.property('message') |
| 50 | + .that.equals('write operation must contain at least one document'); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + it(`should properly split namespace and operations across $db, ${TYPE} and ${OPSFIELD}`, function() { |
| 55 | + const ops = [ |
| 56 | + { temba: 'his arms wide' }, |
| 57 | + { temba: 'at rest' }, |
| 58 | + { shaka: 'when the walls fell' } |
| 59 | + ]; |
| 60 | + |
| 61 | + executeWrite(pool, bson, TYPE, OPSFIELD, 'darmok.jalad', ops, callback); |
| 62 | + |
| 63 | + expect(pool.write).to.have.been.calledOnce.and.to.have.been.calledWith( |
| 64 | + sinon.match({ query: sinon.match.any }), |
| 65 | + sinon.match.any, |
| 66 | + callback |
| 67 | + ); |
| 68 | + |
| 69 | + const msg = pool.write.firstCall.args[0]; |
| 70 | + |
| 71 | + expect(msg) |
| 72 | + .to.have.property('query') |
| 73 | + .with.lengthOf(1) |
| 74 | + .that.has.property(0) |
| 75 | + .that.includes({ |
| 76 | + $db: 'darmok', |
| 77 | + [TYPE]: 'jalad', |
| 78 | + [OPSFIELD]: ops |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + // TODO: do we need to actually test this? |
| 83 | + describe.skip('option tests', function() { |
| 84 | + it('ordered'); |
| 85 | + it('writeConcern'); |
| 86 | + it('collation'); |
| 87 | + it('bypassDocumentValidation'); |
| 88 | + it('txnNumber'); |
| 89 | + it('session'); |
| 90 | + it('chckKeys'); |
| 91 | + it('serializeFunctions'); |
| 92 | + it('ignoreUndefined'); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments