|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { expect } = require('chai'); |
| 4 | +const { mergeBatchResults } = require('../../../src/bulk/common'); |
| 5 | +const { Timestamp, Long } = require('../../../src/bson'); |
| 6 | + |
| 7 | +describe('bulk/common', function () { |
| 8 | + describe('#mergeBatchResults', function () { |
| 9 | + let opTime; |
| 10 | + let lastOp; |
| 11 | + const bulkResult = { |
| 12 | + ok: 1, |
| 13 | + writeErrors: [], |
| 14 | + writeConcernErrors: [], |
| 15 | + insertedIds: [], |
| 16 | + nInserted: 0, |
| 17 | + nUpserted: 0, |
| 18 | + nMatched: 0, |
| 19 | + nModified: 0, |
| 20 | + nRemoved: 1, |
| 21 | + upserted: [] |
| 22 | + }; |
| 23 | + const result = { |
| 24 | + n: 8, |
| 25 | + nModified: 8, |
| 26 | + electionId: '7fffffff0000000000000028', |
| 27 | + ok: 1, |
| 28 | + $clusterTime: { |
| 29 | + clusterTime: '7020546605669417498', |
| 30 | + signature: { |
| 31 | + hash: 'AAAAAAAAAAAAAAAAAAAAAAAAAAA=', |
| 32 | + keyId: 0 |
| 33 | + } |
| 34 | + }, |
| 35 | + operationTime: '7020546605669417498' |
| 36 | + }; |
| 37 | + const batch = []; |
| 38 | + |
| 39 | + context('when lastOp is an object', function () { |
| 40 | + context('when the opTime is a Timestamp', function () { |
| 41 | + before(function () { |
| 42 | + lastOp = { ts: 7020546605669417496, t: 10 }; |
| 43 | + opTime = Timestamp.fromNumber(8020546605669417496); |
| 44 | + bulkResult.opTime = lastOp; |
| 45 | + result.opTime = opTime; |
| 46 | + }); |
| 47 | + |
| 48 | + it('replaces the opTime with the properly formatted object', function () { |
| 49 | + mergeBatchResults(batch, bulkResult, null, result); |
| 50 | + expect(bulkResult.opTime).to.deep.equal({ ts: opTime, t: Long.ZERO }); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + context('when the opTime is an object', function () { |
| 55 | + context('when the ts is greater', function () { |
| 56 | + before(function () { |
| 57 | + lastOp = { ts: 7020546605669417496, t: 10 }; |
| 58 | + opTime = { ts: 7020546605669417497, t: 10 }; |
| 59 | + bulkResult.opTime = lastOp; |
| 60 | + result.opTime = opTime; |
| 61 | + }); |
| 62 | + |
| 63 | + it('replaces the opTime with the new opTime', function () { |
| 64 | + mergeBatchResults(batch, bulkResult, null, result); |
| 65 | + expect(bulkResult.opTime).to.deep.equal(opTime); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + context('when the ts is equal', function () { |
| 70 | + context('when the t is greater', function () { |
| 71 | + before(function () { |
| 72 | + lastOp = { ts: 7020546605669417496, t: 10 }; |
| 73 | + opTime = { ts: 7020546605669417496, t: 20 }; |
| 74 | + bulkResult.opTime = lastOp; |
| 75 | + result.opTime = opTime; |
| 76 | + }); |
| 77 | + |
| 78 | + it('replaces the opTime with the new opTime', function () { |
| 79 | + mergeBatchResults(batch, bulkResult, null, result); |
| 80 | + expect(bulkResult.opTime).to.deep.equal(opTime); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + context('when the t is equal', function () { |
| 85 | + before(function () { |
| 86 | + lastOp = { ts: 7020546605669417496, t: 10 }; |
| 87 | + opTime = { ts: 7020546605669417496, t: 10 }; |
| 88 | + bulkResult.opTime = lastOp; |
| 89 | + result.opTime = opTime; |
| 90 | + }); |
| 91 | + |
| 92 | + it('does not replace the opTime with the new opTime', function () { |
| 93 | + mergeBatchResults(batch, bulkResult, null, result); |
| 94 | + expect(bulkResult.opTime).to.deep.equal(lastOp); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + context('when the t is less', function () { |
| 99 | + before(function () { |
| 100 | + lastOp = { ts: 7020546605669417496, t: 10 }; |
| 101 | + opTime = { ts: 7020546605669417496, t: 5 }; |
| 102 | + bulkResult.opTime = lastOp; |
| 103 | + result.opTime = opTime; |
| 104 | + }); |
| 105 | + |
| 106 | + it('does not replace the opTime with the new opTime', function () { |
| 107 | + mergeBatchResults(batch, bulkResult, null, result); |
| 108 | + expect(bulkResult.opTime).to.deep.equal(lastOp); |
| 109 | + }); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + context('when the ts is less', function () { |
| 114 | + before(function () { |
| 115 | + lastOp = { ts: 7020546605669417496, t: 10 }; |
| 116 | + opTime = { ts: 7020546605669417495, t: 10 }; |
| 117 | + bulkResult.opTime = lastOp; |
| 118 | + result.opTime = opTime; |
| 119 | + }); |
| 120 | + |
| 121 | + it('does not replace the opTime with the new opTime', function () { |
| 122 | + mergeBatchResults(batch, bulkResult, null, result); |
| 123 | + expect(bulkResult.opTime).to.deep.equal(lastOp); |
| 124 | + }); |
| 125 | + }); |
| 126 | + }); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments