summaryrefslogtreecommitdiff
path: root/jstests/aggregation/bugs/server20163.js
blob: e61ba606c2406205889cac306a2f801af4a80a71 (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
// SERVER-20163 introduced the $zip expression. In this test file, we check the behavior and error
// cases of the expression.
load("jstests/aggregation/extras/utils.js");  // For assertErrorCode.

(function() {
    "use strict";

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

    coll.insert({'long': [1, 2, 3], 'short': ['x', 'y']});

    var zipObj = 3;
    assertErrorCode(coll,
                    [{$project: {zipped: {$zip: zipObj}}}],
                    34460,
                    "$zip requires an object" + " as an argument.");

    zipObj = {inputs: []};
    assertErrorCode(coll,
                    [{$project: {zipped: {$zip: zipObj}}}],
                    34465,
                    "$zip requires at least" + " one input array");

    zipObj = {inputs: {"a": "b"}};
    assertErrorCode(coll, [{$project: {zipped: {$zip: zipObj}}}], 34461, "inputs is not an array");

    zipObj = {inputs: ["$a"], defaults: ["A"]};
    assertErrorCode(coll,
                    [{$project: {zipped: {$zip: zipObj}}}],
                    34466,
                    "cannot specify defaults" + " unless useLongestLength is true.");

    zipObj = {inputs: ["$a"], defaults: ["A", "B"], useLongestLength: true};
    assertErrorCode(coll,
                    [{$project: {zipped: {$zip: zipObj}}}],
                    34467,
                    "inputs and defaults" + " must be the same length.");

    zipObj = {inputs: ["$a"], defaults: {"a": "b"}};
    assertErrorCode(
        coll, [{$project: {zipped: {$zip: zipObj}}}], 34462, "defaults is not an" + " array");

    zipObj = {inputs: ["$a"], defaults: ["A"], useLongestLength: 1};
    assertErrorCode(
        coll, [{$project: {zipped: {$zip: zipObj}}}], 34463, "useLongestLength is not" + " a bool");

    zipObj = {inputs: ["$a", "$b"], defaults: ["A"], notAField: 1};
    assertErrorCode(coll, [{$project: {zipped: {$zip: zipObj}}}], 34464, "unknown argument");

    zipObj = {inputs: ["A", "B"]};
    assertErrorCode(coll,
                    [{$project: {zipped: {$zip: zipObj}}}],
                    34468,
                    "an element of inputs" + " was not an array.");

    zipObj = {inputs: [[1, 2, 3], ["A", "B", "C"]]};
    var res = coll.aggregate([{$project: {zipped: {$zip: zipObj}}}]);
    var output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].zipped, [[1, "A"], [2, "B"], [3, "C"]]);

    zipObj = {inputs: [[1, 2, 3], null]};
    res = coll.aggregate([{$project: {zipped: {$zip: zipObj}}}]);
    output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].zipped, null);

    zipObj = {inputs: [null, [1, 2, 3]]};
    res = coll.aggregate([{$project: {zipped: {$zip: zipObj}}}]);
    output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].zipped, null);

    zipObj = {inputs: ["$missing", [1, 2, 3]]};
    res = coll.aggregate([{$project: {zipped: {$zip: zipObj}}}]);
    output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].zipped, null);

    zipObj = {inputs: [undefined, [1, 2, 3]]};
    res = coll.aggregate([{$project: {zipped: {$zip: zipObj}}}]);
    output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].zipped, null);

    zipObj = {inputs: [[1, 2, 3], ["A", "B"]]};
    res = coll.aggregate([{$project: {zipped: {$zip: zipObj}}}]);
    output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].zipped, [[1, "A"], [2, "B"]]);

    zipObj = {inputs: [["A", "B"], [1, 2, 3]]};
    res = coll.aggregate([{$project: {zipped: {$zip: zipObj}}}]);
    output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].zipped, [["A", 1], ["B", 2]]);

    zipObj = {inputs: [[], []]};
    res = coll.aggregate([{$project: {zipped: {$zip: zipObj}}}]);
    output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].zipped, []);

    zipObj = {inputs: [["$short"], ["$long"]]};
    res = coll.aggregate([{$project: {zipped: {$zip: zipObj}}}]);
    output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].zipped, [[['x', 'y'], [1, 2, 3]]]);

    zipObj = {inputs: ["$short", "$long"]};
    res = coll.aggregate([{$project: {zipped: {$zip: zipObj}}}]);
    output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].zipped, [['x', 1], ['y', 2]]);

    zipObj = {inputs: [["$long"]]};
    res = coll.aggregate([{$project: {zipped: {$zip: zipObj}}}]);
    output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].zipped, [[[1, 2, 3]]]);

    zipObj = {inputs: [[1, 2, 3], ['a', 'b', 'c'], ['c', 'b', 'a']]};
    res = coll.aggregate([{$project: {zipped: {$zip: zipObj}}}]);
    output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].zipped, [[1, 'a', 'c'], [2, 'b', 'b'], [3, 'c', 'a']]);

    zipObj = {inputs: [[1, 2, 3], ["A", "B"]], defaults: ["C", "D"], useLongestLength: true};
    res = coll.aggregate([{$project: {zipped: {$zip: zipObj}}}]);
    output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].zipped, [[1, "A"], [2, "B"], [3, "D"]]);

    zipObj = {inputs: [[1, 2, 3], ["A", "B"]], useLongestLength: true};
    res = coll.aggregate([{$project: {zipped: {$zip: zipObj}}}]);
    output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].zipped, [[1, "A"], [2, "B"], [3, null]]);
}());