summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/validate_timeseries_data_indexes.js
blob: 4328a7fdf777bb380b657b1621450b2adff259fd (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
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
/**
 * Tests that the validate command now checks the indexes in the time-series buckets data fields.
 *
 * @tags: [featureFlagExtendValidateCommand]
 */

(function() {
"use strict";

const collPrefix = "validate_timeseries_data_indexes";
const bucketPrefix = "system.buckets.validate_timeseries_data_indexes";
let collName = collPrefix;
let bucketName = bucketPrefix;
let testCount = 0;

const weatherData = [
    {
        "metadata": {"sensorId": 5578, "type": "temperature"},
        "timestamp": ISODate("2021-05-18T12:00:00.000Z"),
        "temp": 12
    },
    {
        "metadata": {"sensorId": 5578, "type": "temperature"},
        "timestamp": ISODate("2021-05-18T16:00:00.000Z"),
        "temp": 16
    },
    {
        "metadata": {"sensorId": 5578, "type": "temperature"},
        "timestamp": ISODate("2021-05-18T20:00:00.000Z"),
        "humidity": 15
    },
];

// Drops collection and creates it again with new data, checking that collection is valid before
// faulty data is inserted.
function setUpCollection(data) {
    testCount += 1;
    collName = collPrefix + testCount;
    bucketName = bucketPrefix + testCount;
    db.getCollection(collName).drop();
    assert.commandWorked(db.createCollection(
        collName,
        {timeseries: {timeField: "timestamp", metaField: "metadata", granularity: "hours"}}));
    const collection = db.getCollection(collName);
    assert.commandWorked(collection.insertMany(data));
    const result = assert.commandWorked(collection.validate());
    assert(result.valid, tojson(result));
    assert(result.warnings.length == 0, tojson(result));
    assert(result.nNonCompliantDocuments == 0, tojson(result));
}

// Non-numerical index.
setUpCollection(weatherData);
jsTestLog("Running validate on bucket with non-numerical data field indexes on collection " +
          collName);
let coll = db.getCollection(collName);
let bucket = db.getCollection(bucketName);
assert.commandWorked(bucket.update({}, {"$rename": {"data.temp.0": "data.temp.a"}}));
let res = assert.commandWorked(coll.validate());
assert(res.valid, tojson(res));
assert(res.warnings.length == 1, tojson(res));
assert(res.nNonCompliantDocuments == 1, tojson(res));

// Non-increasing index.
setUpCollection(weatherData);
jsTestLog("Running validate on bucket with non-increasing data field indexes on collection " +
          collName);
coll = db.getCollection(collName);
bucket = db.getCollection(bucketName);
// This keeps indexes from being reordered by Javascript.
assert.commandWorked(
    bucket.update({}, {"$rename": {"data.temp.0": "data.temp.a", "data.temp.1": "data.temp.b"}}));
assert.commandWorked(bucket.update({}, {"$rename": {"data.temp.a": "data.temp.1"}}));
assert.commandWorked(bucket.update({}, {"$rename": {"data.temp.b": "data.temp.0"}}));
printjson(bucket.find().toArray());
res = assert.commandWorked(coll.validate());
assert(res.valid, tojson(res));
assert(res.warnings.length == 1, tojson(res));
assert(res.nNonCompliantDocuments == 1, tojson(res));

// Out-of-range index.
setUpCollection(weatherData);
jsTestLog("Running validate on bucket with out-of-range data field indexes on collection " +
          collName);
coll = db.getCollection(collName);
bucket = db.getCollection(bucketName);
assert.commandWorked(bucket.update({}, {"$rename": {"data.temp.1": "data.temp.10"}}));
printjson(bucket.find().toArray());
res = assert.commandWorked(coll.validate());
assert(res.valid, tojson(res));
assert(res.warnings.length == 1, tojson(res));
assert(res.nNonCompliantDocuments == 1, tojson(res));

// Negative index.
setUpCollection(weatherData);
jsTestLog("Running validate on bucket with negative data field indexes on collection " + collName);
coll = db.getCollection(collName);
bucket = db.getCollection(bucketName);
assert.commandWorked(bucket.update({}, {"$rename": {"data.temp.0": "data.temp.-1"}}));
printjson(bucket.find().toArray());
res = assert.commandWorked(coll.validate());
assert(res.valid, tojson(res));
assert(res.warnings.length == 1, tojson(res));
assert(res.nNonCompliantDocuments == 1, tojson(res));

// Missing time field index.
setUpCollection(weatherData);
jsTestLog("Running validate on bucket with missing time field indexes on collection " + collName);
coll = db.getCollection(collName);
bucket = db.getCollection(bucketName);
assert.commandWorked(bucket.update({}, {"$unset": {"data.timestamp.1": ""}}));
printjson(bucket.find().toArray());
res = assert.commandWorked(coll.validate());
assert(res.valid, tojson(res));
assert(res.warnings.length == 1, tojson(res));
assert(res.nNonCompliantDocuments == 1, tojson(res));
})();