summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/create_indexes_shell_helper.js
blob: f9f9f7b9f0669112610b590b7fa1a9c795a15ba4 (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
(function() {
    "use strict";
    var t = db.create_indexes_shell_helper;
    t.drop();

    var mongo = db.getMongo();

    try {
        var commandsRan = [];
        var insertsRan = [];
        var mockMongo = {
            forceWriteMode: function(mode) {
                _writeMode = mode;
            },
            writeMode: function() {
                return _writeMode;
            },
            getSlaveOk: function() {
                return true;
            },
            runCommand: function(db, cmd, opts) {
                commandsRan.push({db: db, cmd: cmd, opts: opts});
                return {
                    ok: 1.0
                };
            },
            insert: function(db, indexSpecs, opts) {
                insertsRan.push({db: db, indexSpecs: indexSpecs, opts: opts});
                return {
                    ok: 1.0
                };
            },
            getWriteConcern: function() {
                return null;
            },
            useWriteCommands: function() {
                return true;
            },
            hasWriteCommands: function() {
                return true;
            }
        };

        db._mongo = mockMongo;

        mockMongo.forceWriteMode("commands");

        t.createIndexes([{x: 1}]);
        assert.eq(commandsRan.length, 1);
        assert(commandsRan[0].cmd.hasOwnProperty("createIndexes"));
        assert.eq(commandsRan[0].cmd["indexes"][0],
                  {ns: "test.create_indexes_shell_helper", key: {x: 1}, name: "x_1"});

        commandsRan = [];

        t.createIndexes([{y: 1}, {z: -1}]);
        assert.eq(commandsRan.length, 1);
        assert(commandsRan[0].cmd.hasOwnProperty("createIndexes"));
        assert.eq(commandsRan[0].cmd["indexes"][0],
                  {ns: "test.create_indexes_shell_helper", key: {y: 1}, name: "y_1"});
        assert.eq(commandsRan[0].cmd["indexes"][1],
                  {ns: "test.create_indexes_shell_helper", key: {z: -1}, name: "z_-1"});

        commandsRan = [];

        t.createIndex({a: 1});
        assert.eq(commandsRan.length, 1);
        assert(commandsRan[0].cmd.hasOwnProperty("createIndexes"));
        assert.eq(commandsRan[0].cmd["indexes"][0],
                  {ns: "test.create_indexes_shell_helper", key: {a: 1}, name: "a_1"});

        db.getMongo().forceWriteMode("compatibility");

        commandsRan = [];
        assert.eq(commandsRan.length, 0);
        t.createIndex({b: 1});
        assert.eq(insertsRan.length, 1);
        assert.eq(insertsRan[0]["indexSpecs"]["ns"], "test.create_indexes_shell_helper");
        assert.eq(insertsRan[0]["indexSpecs"]["key"], {b: 1});
        assert.eq(insertsRan[0]["indexSpecs"]["name"], "b_1");
        // getLastError is called in the course of the bulk insert
        assert.eq(commandsRan.length, 1);
        assert(commandsRan[0].cmd.hasOwnProperty("getlasterror"));
    } finally {
        db._mongo = mongo;
    }
}());