|
2 | 2 |
|
3 | 3 | const expect = require('chai').expect;
|
4 | 4 | const mock = require('mongodb-mock-server');
|
5 |
| -const BulkWriteResult = require('../../lib/bulk/common').BulkWriteResult; |
| 5 | +const Long = require('../../lib/core').BSON.Long; |
| 6 | +const Timestamp = require('../../lib/core').BSON.Timestamp; |
| 7 | +const common = require('../../lib/bulk/common'); |
| 8 | +const BulkWriteResult = common.BulkWriteResult; |
| 9 | +const mergeBatchResults = common.mergeBatchResults; |
6 | 10 |
|
7 | 11 | describe('Bulk Writes', function() {
|
8 | 12 | const test = {};
|
@@ -131,4 +135,126 @@ describe('Bulk Writes', function() {
|
131 | 135 |
|
132 | 136 | expect(() => result.insertedIds).to.not.throw();
|
133 | 137 | });
|
| 138 | + |
| 139 | + describe('#mergeBatchResults', function() { |
| 140 | + let opTime; |
| 141 | + let lastOp; |
| 142 | + const bulkResult = { |
| 143 | + ok: 1, |
| 144 | + writeErrors: [], |
| 145 | + writeConcernErrors: [], |
| 146 | + insertedIds: [], |
| 147 | + nInserted: 0, |
| 148 | + nUpserted: 0, |
| 149 | + nMatched: 0, |
| 150 | + nModified: 0, |
| 151 | + nRemoved: 1, |
| 152 | + upserted: [] |
| 153 | + }; |
| 154 | + const result = { |
| 155 | + n: 8, |
| 156 | + nModified: 8, |
| 157 | + electionId: '7fffffff0000000000000028', |
| 158 | + ok: 1, |
| 159 | + $clusterTime: { |
| 160 | + clusterTime: '7020546605669417498', |
| 161 | + signature: { |
| 162 | + hash: 'AAAAAAAAAAAAAAAAAAAAAAAAAAA=', |
| 163 | + keyId: 0 |
| 164 | + } |
| 165 | + }, |
| 166 | + operationTime: '7020546605669417498' |
| 167 | + }; |
| 168 | + const batch = []; |
| 169 | + |
| 170 | + context('when lastOp is an object', function() { |
| 171 | + context('when the opTime is a Timestamp', function() { |
| 172 | + before(function() { |
| 173 | + lastOp = { ts: 7020546605669417496, t: 10 }; |
| 174 | + opTime = Timestamp.fromNumber(8020546605669417496); |
| 175 | + bulkResult.lastOp = lastOp; |
| 176 | + result.opTime = opTime; |
| 177 | + }); |
| 178 | + |
| 179 | + it('replaces the lastOp with the properly formatted object', function() { |
| 180 | + mergeBatchResults(batch, bulkResult, null, result); |
| 181 | + expect(bulkResult.lastOp).to.deep.equal({ ts: opTime, t: Long.ZERO }); |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + context('when the opTime is an object', function() { |
| 186 | + context('when the ts is greater', function() { |
| 187 | + before(function() { |
| 188 | + lastOp = { ts: 7020546605669417496, t: 10 }; |
| 189 | + opTime = { ts: 7020546605669417497, t: 10 }; |
| 190 | + bulkResult.lastOp = lastOp; |
| 191 | + result.opTime = opTime; |
| 192 | + }); |
| 193 | + |
| 194 | + it('replaces the lastOp with the new opTime', function() { |
| 195 | + mergeBatchResults(batch, bulkResult, null, result); |
| 196 | + expect(bulkResult.lastOp).to.deep.equal(opTime); |
| 197 | + }); |
| 198 | + }); |
| 199 | + |
| 200 | + context('when the ts is equal', function() { |
| 201 | + context('when the t is greater', function() { |
| 202 | + before(function() { |
| 203 | + lastOp = { ts: 7020546605669417496, t: 10 }; |
| 204 | + opTime = { ts: 7020546605669417496, t: 20 }; |
| 205 | + bulkResult.lastOp = lastOp; |
| 206 | + result.opTime = opTime; |
| 207 | + }); |
| 208 | + |
| 209 | + it('replaces the lastOp with the new opTime', function() { |
| 210 | + mergeBatchResults(batch, bulkResult, null, result); |
| 211 | + expect(bulkResult.lastOp).to.deep.equal(opTime); |
| 212 | + }); |
| 213 | + }); |
| 214 | + |
| 215 | + context('when the t is equal', function() { |
| 216 | + before(function() { |
| 217 | + lastOp = { ts: 7020546605669417496, t: 10 }; |
| 218 | + opTime = { ts: 7020546605669417496, t: 10 }; |
| 219 | + bulkResult.lastOp = lastOp; |
| 220 | + result.opTime = opTime; |
| 221 | + }); |
| 222 | + |
| 223 | + it('does not replace the lastOp with the new opTime', function() { |
| 224 | + mergeBatchResults(batch, bulkResult, null, result); |
| 225 | + expect(bulkResult.lastOp).to.deep.equal(lastOp); |
| 226 | + }); |
| 227 | + }); |
| 228 | + |
| 229 | + context('when the t is less', function() { |
| 230 | + before(function() { |
| 231 | + lastOp = { ts: 7020546605669417496, t: 10 }; |
| 232 | + opTime = { ts: 7020546605669417496, t: 5 }; |
| 233 | + bulkResult.lastOp = lastOp; |
| 234 | + result.opTime = opTime; |
| 235 | + }); |
| 236 | + |
| 237 | + it('does not replace the lastOp with the new opTime', function() { |
| 238 | + mergeBatchResults(batch, bulkResult, null, result); |
| 239 | + expect(bulkResult.lastOp).to.deep.equal(lastOp); |
| 240 | + }); |
| 241 | + }); |
| 242 | + }); |
| 243 | + |
| 244 | + context('when the ts is less', function() { |
| 245 | + before(function() { |
| 246 | + lastOp = { ts: 7020546605669417496, t: 10 }; |
| 247 | + opTime = { ts: 7020546605669417495, t: 10 }; |
| 248 | + bulkResult.lastOp = lastOp; |
| 249 | + result.opTime = opTime; |
| 250 | + }); |
| 251 | + |
| 252 | + it('does not replace the lastOp with the new opTime', function() { |
| 253 | + mergeBatchResults(batch, bulkResult, null, result); |
| 254 | + expect(bulkResult.lastOp).to.deep.equal(lastOp); |
| 255 | + }); |
| 256 | + }); |
| 257 | + }); |
| 258 | + }); |
| 259 | + }); |
134 | 260 | });
|
0 commit comments