summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/out/bypass_doc_validation.js
blob: 3b67647ef145b0f2208401509639e6d2db5a0454 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
 * Tests for $out with bypassDocumentValidation.
 *
 * @tags: [assumes_unsharded_collection]
 */
(function() {
    "use strict";

    load("jstests/aggregation/extras/utils.js");  // For assertErrorCode.

    const testDB = db.getSiblingDB("out_bypass_doc_val");
    const sourceColl = testDB.getCollection("source");
    const targetColl = testDB.getCollection("target");

    targetColl.drop();
    assert.commandWorked(testDB.createCollection(targetColl.getName(), {validator: {a: 2}}));

    sourceColl.drop();
    assert.commandWorked(sourceColl.insert({_id: 0, a: 1}));

    // Test that the bypassDocumentValidation flag is passed through to the writes on the output
    // collection.
    (function testBypassDocValidationTrue() {
        sourceColl.aggregate([{$out: targetColl.getName()}], {bypassDocumentValidation: true});
        assert.eq([{_id: 0, a: 1}], targetColl.find().toArray());

        sourceColl.aggregate([{$out: {to: targetColl.getName(), mode: "replaceCollection"}}],
                             {bypassDocumentValidation: true});
        assert.eq([{_id: 0, a: 1}], targetColl.find().toArray());

        sourceColl.aggregate(
            [{$addFields: {a: 3}}, {$out: {to: targetColl.getName(), mode: "replaceDocuments"}}],
            {bypassDocumentValidation: true});
        assert.eq([{_id: 0, a: 3}], targetColl.find().toArray());

        sourceColl.aggregate(
            [
              {$replaceRoot: {newRoot: {_id: 1, a: 4}}},
              {$out: {to: targetColl.getName(), mode: "insertDocuments"}}
            ],
            {bypassDocumentValidation: true});
        assert.eq([{_id: 0, a: 3}, {_id: 1, a: 4}], targetColl.find().sort({_id: 1}).toArray());
    }());

    // Test that mode "replaceDocuments" passes without the bypassDocumentValidation flag if the
    // updated doc is valid.
    (function testReplacementStyleUpdateWithoutBypass() {
        sourceColl.aggregate(
            [{$addFields: {a: 2}}, {$out: {to: targetColl.getName(), mode: "replaceDocuments"}}]);
        assert.eq([{_id: 0, a: 2}], targetColl.find({_id: 0}).toArray());
        sourceColl.aggregate(
            [{$addFields: {a: 2}}, {$out: {to: targetColl.getName(), mode: "replaceDocuments"}}],
            {bypassDocumentValidation: false});
        assert.eq([{_id: 0, a: 2}], targetColl.find({_id: 0}).toArray());
    }());

    function assertDocValidationFailure(cmdOptions) {
        targetColl.remove({});
        assertErrorCode(sourceColl,
                        [{$out: targetColl.getName()}],
                        ErrorCodes.DocumentValidationFailure,
                        "Expected failure without bypass set",
                        cmdOptions);

        assertErrorCode(sourceColl,
                        [{$out: {to: targetColl.getName(), mode: "replaceCollection"}}],
                        ErrorCodes.DocumentValidationFailure,
                        "Expected failure without bypass set",
                        cmdOptions);

        assertErrorCode(
            sourceColl,
            [{$addFields: {a: 3}}, {$out: {to: targetColl.getName(), mode: "replaceDocuments"}}],
            ErrorCodes.DocumentValidationFailure,
            "Expected failure without bypass set",
            cmdOptions);

        assertErrorCode(sourceColl,
                        [
                          {$replaceRoot: {newRoot: {_id: 1, a: 4}}},
                          {$out: {to: targetColl.getName(), mode: "insertDocuments"}}
                        ],
                        ErrorCodes.DocumentValidationFailure,
                        "Expected failure without bypass set",
                        cmdOptions);
        assert.eq(0, targetColl.find().itcount());
    }

    // Test that $out fails if the output document is not valid, and the bypassDocumentValidation
    // flag is not set.
    assertDocValidationFailure({});

    // Test that $out fails if the output document is not valid, and the bypassDocumentValidation
    // flag is explicitly set to false.
    assertDocValidationFailure({bypassDocumentValidation: false});

    // Test that bypassDocumentValidation is *not* needed if the source collection has a
    // validator but the output collection does not.
    (function testDocValidatorOnSourceCollection() {
        targetColl.drop();
        assert.commandWorked(testDB.runCommand({collMod: sourceColl.getName(), validator: {a: 1}}));

        sourceColl.aggregate([{$out: targetColl.getName()}]);
        assert.eq([{_id: 0, a: 1}], targetColl.find().toArray());

        sourceColl.aggregate([{$out: {to: targetColl.getName(), mode: "replaceCollection"}}]);
        assert.eq([{_id: 0, a: 1}], targetColl.find().toArray());

        sourceColl.aggregate(
            [{$addFields: {a: 3}}, {$out: {to: targetColl.getName(), mode: "replaceDocuments"}}]);
        assert.eq([{_id: 0, a: 3}], targetColl.find().toArray());

        sourceColl.aggregate([
            {$replaceRoot: {newRoot: {_id: 1, a: 4}}},
            {$out: {to: targetColl.getName(), mode: "insertDocuments"}}
        ]);
        assert.eq([{_id: 0, a: 3}, {_id: 1, a: 4}], targetColl.find().sort({_id: 1}).toArray());
    }());

    // Test that the bypassDocumentValidation is casted to true if the value is non-boolean.
    (function testNonBooleanBypassDocValidationFlag() {
        targetColl.remove({});
        assert.commandWorked(testDB.runCommand({collMod: targetColl.getName(), validator: {a: 1}}));
        sourceColl.drop();
        assert.commandWorked(sourceColl.insert({_id: 0, a: 1}));

        sourceColl.aggregate([{$out: targetColl.getName()}], {bypassDocumentValidation: 5});
        assert.eq([{_id: 0, a: 1}], targetColl.find().toArray());

        sourceColl.aggregate(
            [{$addFields: {a: 3}}, {$out: {to: targetColl.getName(), mode: "replaceDocuments"}}],
            {bypassDocumentValidation: "false"});
        assert.eq([{_id: 0, a: 3}], targetColl.find().toArray());
    }());
}());