summaryrefslogtreecommitdiff
path: root/jstests/core/group1.js
blob: 6100ee94c70da1811477b35099d27b9792fc5850 (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
t = db.group1;
t.drop();

t.save({n: 1, a: 1});
t.save({n: 2, a: 1});
t.save({n: 3, a: 2});
t.save({n: 4, a: 2});
t.save({n: 5, a: 2});

var p = {
    key: {a: true},
    reduce: function(obj, prev) {
        prev.count++;
    },
    initial: {count: 0}
};

res = t.group(p);

assert(res.length == 2, "A");
assert(res[0].a == 1, "B");
assert(res[0].count == 2, "C");
assert(res[1].a == 2, "D");
assert(res[1].count == 3, "E");

assert.eq(res, t.groupcmd(p), "ZZ");

ret = t.groupcmd({key: {}, reduce: p.reduce, initial: p.initial});
assert.eq(1, ret.length, "ZZ 2");
assert.eq(5, ret[0].count, "ZZ 3");

ret = t.groupcmd({
    key: {},
    reduce: function(obj, prev) {
        prev.sum += obj.n;
    },
    initial: {sum: 0}
});
assert.eq(1, ret.length, "ZZ 4");
assert.eq(15, ret[0].sum, "ZZ 5");

t.drop();

t.save({"a": 2});
t.save({"b": 5});
t.save({"a": 1});
t.save({"a": 2});

c = {
    key: {a: 1},
    cond: {},
    initial: {"count": 0},
    reduce: function(obj, prev) {
        prev.count++;
    }
};

assert.eq(t.group(c), t.groupcmd(c), "ZZZZ");

t.drop();

t.save({name: {first: "a", last: "A"}});
t.save({name: {first: "b", last: "B"}});
t.save({name: {first: "a", last: "A"}});

p = {
    key: {'name.first': true},
    reduce: function(obj, prev) {
        prev.count++;
    },
    initial: {count: 0}
};

res = t.group(p);
assert.eq(2, res.length, "Z1");
assert.eq("a", res[0]['name.first'], "Z2");
assert.eq("b", res[1]['name.first'], "Z3");
assert.eq(2, res[0].count, "Z4");
assert.eq(1, res[1].count, "Z5");

// SERVER-15851 Test invalid user input.
p = {
    ns: "group1",
    key: {"name.first": true},
    $reduce: function(obj, prev) {
        prev.count++;
    },
    initial: {count: 0},
    finalize: "abc"
};
assert.commandFailedWithCode(db.runCommand({group: p}),
                             ErrorCodes.JSInterpreterFailure,
                             "Illegal finalize function");

p = {
    ns: "group1",
    key: {"name.first": true},
    $reduce: function(obj, prev) {
        prev.count++;
    },
    initial: {count: 0},
    finalize: function(obj) {
        ob;
    }
};
assert.commandFailedWithCode(db.runCommand({group: p}),
                             ErrorCodes.JSInterpreterFailure,
                             "Illegal finalize function 2");

p = {
    ns: "group1",
    $keyf: "a",
    $reduce: function(obj, prev) {
        prev.count++;
    },
    initial: {count: 0},
    finalize: function(obj) {
        ob;
    }
};
assert.commandFailedWithCode(db.runCommand({group: p}),
                             ErrorCodes.JSInterpreterFailure,
                             "Illegal keyf function");

p = {
    ns: "group1",
    key: {"name.first": true},
    $reduce: "abc",
    initial: {count: 0}
};
assert.commandFailedWithCode(db.runCommand({group: p}),
                             ErrorCodes.JSInterpreterFailure,
                             "Illegal reduce function");

p = {
    ns: "group1",
    key: {"name.first": true},
    $reduce: function(obj, pre) {
        prev.count++;
    },
    initial: {count: 0}
};
assert.commandFailedWithCode(db.runCommand({group: p}),
                             ErrorCodes.JSInterpreterFailure,
                             "Illegal reduce function 2");

t.drop();