summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/validate_timeseries_id_timestamp.js
blob: 55e4f5a090934fe8ea793c3ddae7c7727d38bd45 (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
/**
 * Tests that the validate command checks for the equivalence of the timestamp embedded in the
 * time-series bucket document's '_id' field and the timestamp in the document's 'control.min.time'
 * field.
 *
 * @tags: [
 * requires_fcv_62
 * ]
 */

(function() {
"use strict";
let testCount = 0;
const collNamePrefix = "validate_timeseries_id_timestamp";
const bucketNamePrefix = "system.buckets.validate_timeseries_id_timestamp";
let collName = collNamePrefix + testCount;
let bucketName = bucketNamePrefix + testCount;
let coll = null;
let bucket = null;

jsTestLog(
    "Running the validate command to check time-series bucket OID timestamp and min timestamp equivalence.");
testCount += 1;
collName = collNamePrefix + testCount;
bucketName = bucketNamePrefix + testCount;
db.getCollection(collName).drop();
assert.commandWorked(db.createCollection(
    collName, {timeseries: {timeField: "timestamp", metaField: "metadata", granularity: "hours"}}));
coll = db.getCollection(collName);
bucket = db.getCollection(bucketName);

// Inserts documents into a bucket. Checks no issues are found.
coll.insertMany(
    [...Array(10).keys()].map(i => ({
                                  "metadata": {"sensorId": testCount, "type": "temperature"},
                                  "timestamp": ISODate(),
                                  "temp": i
                              })),
    {ordered: false});
let res = coll.validate();
assert(res.valid, tojson(res));
assert.eq(res.nNonCompliantDocuments, 0);
assert.eq(res.warnings.length, 0);

// Inserts documents into another bucket but manually changes the min timestamp. Expects
// warnings from validation.
testCount += 1;
collName = collNamePrefix + testCount;
bucketName = bucketNamePrefix + testCount;
db.getCollection(collName).drop();
assert.commandWorked(db.createCollection(
    collName, {timeseries: {timeField: "timestamp", metaField: "metadata", granularity: "hours"}}));
coll = db.getCollection(collName);
bucket = db.getCollection(bucketName);
coll.insertMany(
    [...Array(10).keys()].map(i => ({
                                  "metadata": {"sensorId": testCount, "type": "temperature"},
                                  "timestamp": ISODate(),
                                  "temp": i
                              })),
    {ordered: false});
bucket.updateOne({"meta.sensorId": testCount}, {"$set": {"control.min.timestamp": ISODate()}});
res = coll.validate();
assert(res.valid, tojson(res));
assert.eq(res.nNonCompliantDocuments, 1);
assert.eq(res.warnings.length, 1);
})();