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