summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/timeseries_insert_ordered_true.js
blob: d444981cbb48a132c19a38e874391f142212fc01 (plain)
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
/**
 * Tests that time-series inserts respect {ordered: true}.
 */
(function() {
'use strict';

load('jstests/core/timeseries/libs/timeseries.js');
load('jstests/libs/fail_point_util.js');

const conn = MongoRunner.runMongod();

if (!TimeseriesTest.timeseriesCollectionsEnabled(conn)) {
    jsTestLog('Skipping test because the time-series collection feature flag is disabled');
    MongoRunner.stopMongod(conn);
    return;
}

const testDB = conn.getDB(jsTestName());

const coll = testDB.getCollection('t');
const bucketsColl = testDB.getCollection('system.buckets.' + coll.getName());

const timeFieldName = 'time';
const metaFieldName = 'meta';

const resetColl = function() {
    coll.drop();
    assert.commandWorked(testDB.createCollection(
        coll.getName(), {timeseries: {timeField: timeFieldName, metaField: metaFieldName}}));
    assert.contains(bucketsColl.getName(), testDB.getCollectionNames());
};
resetColl();

configureFailPoint(conn, 'failTimeseriesInsert', {metadata: 'fail'});

const docs = [
    {_id: 0, [timeFieldName]: ISODate()},
    {_id: 1, [timeFieldName]: ISODate()},
    {_id: 2, [timeFieldName]: ISODate()},
];

let res = assert.commandWorked(coll.insert(docs, {ordered: true}));
assert.eq(res.nInserted, 3, 'Invalid insert result: ' + tojson(res));
assert.docEq(coll.find().sort({_id: 1}).toArray(), docs);
resetColl();

docs[1][metaFieldName] = 'fail';
res = assert.commandFailed(coll.insert(docs, {ordered: true}));
jsTestLog('Checking insert result: ' + tojson(res));
assert.eq(res.nInserted, 1);
assert.eq(res.getWriteErrors().length, 1);
assert.eq(res.getWriteErrors()[0].index, 1);
assert.docEq(res.getWriteErrors()[0].getOperation(), docs[1]);
assert.docEq(coll.find().toArray(), [docs[0]]);

MongoRunner.stopMongod(conn);
})();