summaryrefslogtreecommitdiff
path: root/jstests/aggregation/expressions/switch.js
blob: 64cd9e1db2f2a41a3bc308e4da0b36dba15fd2e4 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
// In SERVER-10689, the $switch expression was introduced. In this file, we test the functionality
// of the expression.

(function() {
    "use strict";

    var coll = db.switch;
    coll.drop();

    // Insert an empty document so that something can flow through the pipeline.
    coll.insert({});

    // Ensure that a branch is correctly evaluated.
    var pipeline = {
        "$project": {
            "_id": 0,
            "output": {
                "$switch": {
                    "branches": [{"case": {"$eq": [1, 1]}, "then": "one is equal to one!"}],
                }
            }
        }
    };
    var res = coll.aggregate(pipeline).toArray();

    assert.eq(res.length, 1);
    assert.eq(res[0], {"output": "one is equal to one!"});

    // Ensure that the first branch which matches is chosen.
    pipeline = {
        "$project": {
            "_id": 0,
            "output": {
                "$switch": {
                    "branches": [
                        {"case": {"$eq": [1, 1]}, "then": "one is equal to one!"},
                        {"case": {"$eq": [2, 2]}, "then": "two is equal to two!"}
                    ],
                }
            }
        }
    };
    res = coll.aggregate(pipeline).toArray();

    assert.eq(res.length, 1);
    assert.eq(res[0], {"output": "one is equal to one!"});

    // Ensure that the default is chosen if no case matches.
    pipeline = {
        "$project": {
            "_id": 0,
            "output": {
                "$switch": {
                    "branches": [{"case": {"$eq": [1, 2]}, "then": "one is equal to two!"}],
                    "default": "no case matched."
                }
            }
        }
    };
    res = coll.aggregate(pipeline).toArray();

    assert.eq(res.length, 1);
    assert.eq(res[0], {"output": "no case matched."});

    // Ensure that nullish values are treated as false when they are a "case", and are null
    // otherwise.
    pipeline = {
        "$project": {
            "_id": 0,
            "output": {
                "$switch": {
                    "branches": [{"case": null, "then": "Null was true!"}],
                    "default": "No case matched."
                }
            }
        }
    };
    res = coll.aggregate(pipeline).toArray();

    assert.eq(res.length, 1);
    assert.eq(res[0], {"output": "No case matched."});

    pipeline = {
        "$project": {
            "_id": 0,
            "output": {
                "$switch": {
                    "branches": [{"case": "$missingField", "then": "Null was true!"}],
                    "default": "No case matched."
                }
            }
        }
    };
    res = coll.aggregate(pipeline).toArray();

    assert.eq(res.length, 1);
    assert.eq(res[0], {"output": "No case matched."});

    pipeline = {
        "$project": {
            "_id": 0,
            "output": {"$switch": {"branches": [{"case": true, "then": null}], "default": false}}
        }
    };
    res = coll.aggregate(pipeline).toArray();

    assert.eq(res.length, 1);
    assert.eq(res[0], {"output": null});

    pipeline = {
        "$project": {
            "_id": 0,
            "output": {
                "$switch":
                    {"branches": [{"case": true, "then": "$missingField"}], "default": false}
            }
        }
    };
    res = coll.aggregate(pipeline).toArray();

    assert.eq(res.length, 1);
    assert.eq(res[0], {});

    pipeline = {
        "$project": {
            "_id": 0,
            "output": {"$switch": {"branches": [{"case": null, "then": false}], "default": null}}
        }
    };
    res = coll.aggregate(pipeline).toArray();

    assert.eq(res.length, 1);
    assert.eq(res[0], {"output": null});

    pipeline = {
        "$project": {
            "_id": 0,
            "output": {
                "$switch":
                    {"branches": [{"case": null, "then": false}], "default": "$missingField"}
            }
        }
    };
    res = coll.aggregate(pipeline).toArray();

    assert.eq(res.length, 1);
    assert.eq(res[0], {});
}());