summaryrefslogtreecommitdiff
path: root/jstests/aggregation/extras/utils.js
blob: 68b2597c5338a2ca28586f14ea5ad63c2801d0af (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
 * Compute the result of evaluating 'expression', and compare it to 'result'. Replaces the contents
 * of 'coll' with a single empty document.
 */
function testExpression(coll, expression, result) {
    testExpressionWithCollation(coll, expression, result);
}

/**
 * Compute the result of evaluating 'expression', and compare it to 'result', using 'collationSpec'
 * as the collation spec. Replaces the contents of 'coll' with a single empty document.
 */
function testExpressionWithCollation(coll, expression, result, collationSpec) {
    coll.remove({});
    coll.insert({});

    var options = collationSpec !== undefined ? {collation: collationSpec} : undefined;

    var res = coll.aggregate([{$project: {output: expression}}], options).toArray();

    assert.eq(res.length, 1, tojson(res));
    assert.eq(res[0].output, result, tojson(res));
}

/*
  Utility functions used to test aggregation
*/

function anyEq(al, ar, v) {
    if (typeof(v) == 'undefined')
        v = false;

    if (al instanceof Array) {
        if (!(ar instanceof Array)) {
            if (v)
                print('anyEq: ar is not an array ' + ar);
            return false;
        }

        if (!arrayEq(al, ar, v)) {
            if (v)
                print('anyEq: arrayEq(al, ar): false; al=' + al + ' ar=' + ar);
            return false;
        }
    } else if (al instanceof Object) {
        if (!(ar instanceof Object)) {
            if (v)
                print('anyEq: ar is not an object ' + ar);
            return false;
        }

        if (!documentEq(al, ar, v)) {
            if (v)
                print('anyEq: documentEq(al, ar): false; al=' + al + ' ar=' + ar);
            return false;
        }
    } else if (al != ar) {
        if (v)
            print('anyEq: (al != ar): false; al=' + al + ' ar=' + ar);
        return false;
    }

    /* if we got here, they matched */
    return true;
}

/*
  Compare two documents for equality.  Returns true or false.  Only equal if
  they have the exact same set of properties, and all the properties' values
  match.
*/
function documentEq(dl, dr, v) {
    if (typeof(v) == 'undefined')
        v = false;

    /* make sure these are both objects */
    if (!(dl instanceof Object)) {
        if (v)
            print('documentEq:  dl is not an object ' + dl);
        return false;
    }
    if (!(dr instanceof Object)) {
        if (v)
            print('documentEq:  dr is not an object ' + dr);
        return false;
    }

    /* start by checking for all of dl's properties in dr */
    for (var propertyName in dl) {
        /* skip inherited properties */
        if (!dl.hasOwnProperty(propertyName))
            continue;

        /* the documents aren't equal if they don't both have the property */
        if (!dr.hasOwnProperty(propertyName)) {
            if (v)
                print('documentEq: dr doesn\'t have property ' + propertyName);
            return false;
        }

        /* if the property is the _id, they don't have to be equal */
        if (propertyName == '_id')
            continue;

        if (!anyEq(dl[propertyName], dr[propertyName], v)) {
            return false;
        }
    }

    /* now make sure that dr doesn't have any extras that dl doesn't have */
    for (var propertyName in dr) {
        if (!dr.hasOwnProperty(propertyName))
            continue;

        /* if dl doesn't have this complain; if it does, we compared it above */
        if (!dl.hasOwnProperty(propertyName)) {
            if (v)
                print('documentEq: dl is missing property ' + propertyName);
            return false;
        }
    }

    /* if we got here, the two documents are an exact match */
    return true;
}

function arrayEq(al, ar, v) {
    if (typeof(v) == 'undefined')
        v = false;

    /* check that these are both arrays */
    if (!(al instanceof Array)) {
        if (v)
            print('arrayEq: al is not an array: ' + al);
        return false;
    }

    if (!(ar instanceof Array)) {
        if (v)
            print('arrayEq: ar is not an array: ' + ar);
        return false;
    }

    if (al.length != ar.length) {
        if (v)
            print('arrayEq: array lengths do not match: ' + al + ', ' + ar);
        return false;
    }

    var i = 0;
    var j = 0;
    while (i < al.length) {
        if (anyEq(al[i], ar[j], v)) {
            j = 0;
            i++;
        } else if (j < ar.length) {
            j++;
        } else {
            return false;
        }
    }

    /* if we got here, the two objects matched */
    return true;
}

/*
  Make a shallow copy of an array.
*/
function arrayShallowCopy(a) {
    assert(a instanceof Array, 'arrayShallowCopy: argument is not an array');

    var c = [];
    for (var i = 0; i < a.length; ++i)
        c.push(a[i]);

    return c;
}

/*
  Compare two sets of documents (expressed as arrays) to see if they match.
  The two sets must have the same documents, although the order need not
  match.

  Are non-scalar values references?
*/
function resultsEq(rl, rr, v) {
    if (typeof(v) == 'undefined')
        v = false;

    /* make clones of the arguments so that we don't damage them */
    rl = arrayShallowCopy(rl);
    rr = arrayShallowCopy(rr);

    if (rl.length != rr.length) {
        if (v)
            print('resultsEq:  array lengths do not match ' + rl + ', ' + rr);
        return false;
    }

    for (var i = 0; i < rl.length; ++i) {
        var foundIt = false;

        /* find a match in the other array */
        for (var j = 0; j < rr.length; ++j) {
            if (!anyEq(rl[i], rr[j], v))
                continue;

            /*
              Because we made the copies above, we can edit these out of the
              arrays so we don't check on them anymore.

              For the inner loop, we're going to be skipping out, so we don't
              need to be too careful.
            */
            rr.splice(j, 1);
            foundIt = true;
            break;
            /* TODO */
        }

        if (!foundIt) {
            /* if we got here, we didn't find this item */
            if (v)
                print('resultsEq: search target missing index:  ' + i);
            return false;
        }
    }

    /* if we got here, everything matched */
    assert(!rr.length);
    return true;
}

function orderedArrayEq(al, ar, v) {
    if (al.length != ar.length) {
        if (v)
            print('orderedArrayEq:  array lengths do not match ' + al + ', ' + ar);
        return false;
    }

    /* check the elements in the array */
    for (var i = 0; i < al.length; ++i) {
        if (!anyEq(al[i], ar[i], v))
            return false;
    }

    /* if we got here, everything matched */
    return true;
}

/*
  Assert that aggregation fails with a specific code.
  Error message is optional.
*/

function assertErrorCode(coll, pipe, code, errmsg) {
    if (!Array.isArray(pipe)) {
        pipe = [pipe];
    }

    // Test non-cursor
    var res = coll.runCommand("aggregate", {pipeline: pipe});
    if (res.ok || res.code != code)
        printjson({pipeline: pipe, result: res});

    /* assert failure with proper error code */
    assert(!res.ok, errmsg || "failed in assertErrorCode");
    assert.eq(res.code, code);

    // Test with cursors
    var cmd = {pipeline: pipe};
    // cmd.cursor = {};
    cmd.cursor = {batchSize: 0};

    var cursorRes = coll.runCommand("aggregate", cmd);
    if (cursorRes.ok) {
        var followupBatchSize = 0;  // default
        var cursor = new DBCommandCursor(cursorRes._mongo, cursorRes, followupBatchSize);

        var error = assert.throws(function() {
            cursor.itcount();
        }, [], "expected error: " + code);

        assert.eq(error.code, code);
    } else {
        assert.eq(cursorRes.code, code);
    }
}