summaryrefslogtreecommitdiff
path: root/jstests/core/api_version_new_50_language_features.js
blob: 367b20deaeaefc634539e4d87e544573d1886549 (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
/**
 * Tests that language features introduced in version 4.9 or 5.0 are not included in API Version 1
 * yet. This test should be updated or removed in a future release when we have more confidence that
 * the behavior and syntax is stable.
 *
 *  TODO: SERVER-58962 remove requires_fcv_51 from this file or change the test code.
 *
 * @tags: [
 *   requires_fcv_51,
 *   uses_api_parameters,
 * ]
 */

(function() {
"use strict";

const collName = "api_version_new_50_language_features";
const coll = db[collName];
coll.drop();
assert.commandWorked(coll.insert({a: 1, date: new ISODate()}));

const unstablePipelines = [
    [{$set: {x: {$dateTrunc: {date: "$date", unit: "second", binSize: 5}}}}],
    [{$set: {x: {$dateAdd: {startDate: "$date", unit: "day", amount: 1}}}}],
    [{$set: {x: {$dateSubtract: {startDate: "$date", unit: "day", amount: 1}}}}],
    [{$set: {x: {$dateDiff: {startDate: "$date", endDate: "$date", unit: "day"}}}}],
    [{$set: {x: {$getField: {input: "$$ROOT", field: "x"}}}}],
    [{$set: {x: {$setField: {input: "$$ROOT", field: "x", value: "foo"}}}}],
    [{$set: {x: {$tsSecond: new Timestamp(0, 0)}}}],
    [{$set: {x: {$tsIncrement: new Timestamp(0, 0)}}}],
];

function assertAggregateFailsWithAPIStrict(pipeline, errorCodes) {
    assert.commandFailedWithCode(db.runCommand({
        aggregate: collName,
        pipeline: pipeline,
        cursor: {},
        apiStrict: true,
        apiVersion: "1"
    }),
                                 errorCodes,
                                 pipeline);
}

function assertViewFailsWithAPIStrict(pipeline) {
    assert.commandFailedWithCode(db.runCommand({
        create: 'new_50_feature_view',
        viewOn: collName,
        pipeline: pipeline,
        apiStrict: true,
        apiVersion: "1"
    }),
                                 ErrorCodes.APIStrictError,
                                 pipeline);
}

for (let pipeline of unstablePipelines) {
    // Assert error thrown when running a pipeline with stages not in API Version 1.
    assertAggregateFailsWithAPIStrict(pipeline, ErrorCodes.APIStrictError);

    // Assert error thrown when creating a view on a pipeline with stages not in API Version 1.
    assertViewFailsWithAPIStrict(pipeline);

    // Assert error is not thrown when running without apiStrict=true.
    assert.commandWorked(db.runCommand({
        aggregate: coll.getName(),
        pipeline: pipeline,
        apiVersion: "1",
        cursor: {},
    }));
}

// $setWindowFields is not supported in transactions or with read concern snapshot. Test separately
// and check for all the error codes that can occur depending on what passthrough we are in.
const setWindowFieldsPipeline = [{
    $setWindowFields: {
        sortBy: {_id: 1},
        output: {runningCount: {$sum: 1, window: {documents: ["unbounded", "current"]}}}
    }
}];
assertAggregateFailsWithAPIStrict(setWindowFieldsPipeline, [
    ErrorCodes.APIStrictError,
    ErrorCodes.InvalidOptions,
    ErrorCodes.OperationNotSupportedInTransaction
]);

assertViewFailsWithAPIStrict(setWindowFieldsPipeline);

// Creating a collection with the unstable validator is not allowed with apiStrict:true.
assert.commandFailedWithCode(db.runCommand({
    create: 'new_50_features_validator',
    validator: {$expr: {$eq: [{$getField: {input: "$$ROOT", field: "dotted.path"}}, 2]}},
    apiVersion: "1",
    apiStrict: true
}),
                             ErrorCodes.APIStrictError);
})();