summaryrefslogtreecommitdiff
path: root/jstests/core/commands_that_do_not_write_do_not_accept_wc.js
blob: 4b882c95fc0e61825252fc2d8487b9f87e5c8d04 (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
/**
 * This file tests commands that do not support write concern. It passes both valid and invalid
 * writeConcern fields to commands and expects the commands to fail with a writeConcernNotSupported
 * error.
 */

(function() {
    "use strict";
    var collName = 'leaves';

    var commands = [];

    commands.push({count: collName, query: {type: 'oak'}});

    commands.push({aggregate: collName, pipeline: [{$sort: {type: 1}}], cursor: {}});

    commands.push({
        mapReduce: collName,
        map: function() {
            this.tags.forEach(function(z) {
                emit(z, 1);
            });
        },
        reduce: function(key, values) {
            return {count: values.length};
        },
        out: {inline: 1}
    });

    function assertWriteConcernNotSupportedError(res) {
        assert.commandFailed(res);
        assert.eq(res.code, ErrorCodes.InvalidOptions);
        assert(!res.writeConcernError);
    }

    // Test a variety of valid and invalid writeConcerns to confirm that they still all get
    // the correct error.
    var writeConcerns = [{w: 'invalid'}, {w: 1}];

    function testUnsupportedWriteConcern(wc, cmd) {
        cmd.writeConcern = wc;
        jsTest.log("Testing " + tojson(cmd));

        var res = db.runCommand(cmd);
        assertWriteConcernNotSupportedError(res);
    }

    // Verify that each command gets a writeConcernNotSupported error.
    commands.forEach(function(cmd) {
        writeConcerns.forEach(function(wc) {
            testUnsupportedWriteConcern(wc, cmd);
        });
    });
})();