diff options
author | James Wahlin <james@mongodb.com> | 2020-09-25 14:10:54 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-10-21 17:32:36 +0000 |
commit | 9d51067f896a8f03c7a654dc01189fef3a14b719 (patch) | |
tree | 50b7682637df55f602dae0a79b01669ba9b01eac /jstests/aggregation/extras | |
parent | 8de14ac959567c9bc29a108db65114ffd2a91163 (diff) | |
download | mongo-9d51067f896a8f03c7a654dc01189fef3a14b719.tar.gz |
SERVER-50746 Don't implicitly ignore _id in jstests/aggregation/extras/utils.js comparison functions
Diffstat (limited to 'jstests/aggregation/extras')
-rw-r--r-- | jstests/aggregation/extras/utils.js | 36 |
1 files changed, 19 insertions, 17 deletions
diff --git a/jstests/aggregation/extras/utils.js b/jstests/aggregation/extras/utils.js index 088ffa786e7..a96f4782bfe 100644 --- a/jstests/aggregation/extras/utils.js +++ b/jstests/aggregation/extras/utils.js @@ -24,9 +24,9 @@ function testExpressionWithCollation(coll, expression, result, collationSpec) { /** * Returns true if 'al' is the same as 'ar'. If the two are arrays, the arrays can be in any order. - * Objects (either 'al' and 'ar' themselves, or embedded objects) must have all the same properties, - * with the exception of '_id'. If 'al' and 'ar' are neither object nor arrays, they must compare - * equal using 'valueComparator', or == if not provided. + * Objects (either 'al' and 'ar' themselves, or embedded objects) must have all the same properties. + * If 'al' and 'ar' are neither object nor arrays, they must compare equal using 'valueComparator', + * or == if not provided. */ function anyEq(al, ar, verbose = false, valueComparator, fieldsToSkip = []) { const debug = msg => verbose ? print(msg) : null; // Helper to log 'msg' iff 'verbose' is true. @@ -37,7 +37,7 @@ function anyEq(al, ar, verbose = false, valueComparator, fieldsToSkip = []) { return false; } - if (!arrayEq(al, ar, verbose, valueComparator)) { + if (!arrayEq(al, ar, verbose, valueComparator, fieldsToSkip)) { debug(`anyEq: arrayEq(al, ar): false; al=${tojson(al)}, ar=${tojson(ar)}`); return false; } @@ -68,8 +68,8 @@ function anyEq(al, ar, verbose = false, valueComparator, fieldsToSkip = []) { * or false. Returns true or false. Only equal if they have the exact same set of properties, and * all the properties' values match according to 'valueComparator'. */ -function customDocumentEq({left, right, verbose, valueComparator}) { - return documentEq(left, right, verbose, valueComparator); +function customDocumentEq({left, right, verbose, valueComparator, fieldsToSkip = []}) { + return documentEq(left, right, verbose, valueComparator, fieldsToSkip); } /** @@ -102,8 +102,7 @@ function documentEq(dl, dr, verbose = false, valueComparator, fieldsToSkip = []) return false; } - // If the property is the _id, they don't have to be equal. - if (propertyName == '_id' || fieldsToSkip.includes(propertyName)) + if (fieldsToSkip.includes(propertyName)) continue; if (!anyEq(dl[propertyName], dr[propertyName], verbose, valueComparator, fieldsToSkip)) { @@ -174,7 +173,7 @@ function arrayEq(al, ar, verbose = false, valueComparator, fieldsToSkip = []) { return true; } -function arrayDiff(al, ar, verbose = false, valueComparator) { +function arrayDiff(al, ar, verbose = false, valueComparator, fieldsToSkip = []) { // Check that these are both arrays. if (!(al instanceof Array)) { debug('arrayDiff: al is not an array: ' + tojson(al)); @@ -192,7 +191,8 @@ function arrayDiff(al, ar, verbose = false, valueComparator) { for (let leftElem of al) { let foundMatch = false; for (let i = 0; i < ar.length; ++i) { - if (!matchedIndexesInRight.has(i) && anyEq(leftElem, ar[i], verbose, valueComparator)) { + if (!matchedIndexesInRight.has(i) && + anyEq(leftElem, ar[i], verbose, valueComparator, fieldsToSkip)) { matchedIndexesInRight.add(i); // Don't use the same value each time. foundMatch = true; break; @@ -221,11 +221,12 @@ function arrayShallowCopy(a) { /** * 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 and the _id values need not match. + * the same documents, although the order need not match and values for fields defined in + * "fieldsToSkip" need not match. * * Are non-scalar values references? */ -function resultsEq(rl, rr, verbose = false) { +function resultsEq(rl, rr, verbose = false, fieldsToSkip = []) { const debug = msg => verbose ? print(msg) : null; // Helper to log 'msg' iff 'verbose' is true. // Make clones of the arguments so that we don't damage them. @@ -242,7 +243,7 @@ function resultsEq(rl, rr, verbose = false) { // Find a match in the other array. for (let j = 0; j < rr.length; ++j) { - if (!anyEq(rl[i], rr[j], verbose)) + if (!anyEq(rl[i], rr[j], verbose, null, fieldsToSkip)) continue; // Because we made the copies above, we can edit these out of the arrays so we don't @@ -265,7 +266,7 @@ function resultsEq(rl, rr, verbose = false) { return true; } -function orderedArrayEq(al, ar, verbose = false) { +function orderedArrayEq(al, ar, verbose = false, fieldsToSkip = []) { if (al.length != ar.length) { if (verbose) print(`orderedArrayEq: array lengths do not match ${tojson(al)}, ${tojson(ar)}`); @@ -273,7 +274,7 @@ function orderedArrayEq(al, ar, verbose = false) { } for (let i = 0; i < al.length; ++i) { - if (!anyEq(al[i], ar[i], verbose)) + if (!anyEq(al[i], ar[i], verbose, null, fieldsToSkip)) return false; } @@ -348,8 +349,9 @@ function assertErrMsgDoesNotContain(coll, pipe, expectedMessage) { * the 'actual' array has a matching element in the 'expected' array, without honoring elements * order. */ -function assertArrayEq({actual = [], expected = []} = {}) { - assert(arrayEq(actual, expected), `actual=${tojson(actual)}, expected=${tojson(expected)}`); +function assertArrayEq({actual = [], expected = [], fieldsToSkip = []} = {}) { + assert(arrayEq(actual, expected, false, null, fieldsToSkip), + `actual=${tojson(actual)}, expected=${tojson(expected)}`); } /** |