diff options
author | David Percy <david.percy@mongodb.com> | 2022-07-12 20:24:57 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-08-17 15:37:04 +0000 |
commit | b902366ad23713ed7493e121f0218fead287bc6d (patch) | |
tree | 762de8a4a020d82ff3cf43faf7a35ba736e37a30 /jstests/libs/golden_test.js | |
parent | aca57c0414496efbfaa914343e3b599366f15a14 (diff) | |
download | mongo-b902366ad23713ed7493e121f0218fead287bc6d.tar.gz |
SERVER-67415 Create golden-data JS suites
Adds two new suites: query_golden_classic and query_golden_cqf, which
use the golden-data test framework (docs/golden_data_test_framework.md).
These suites diff the expected and actual output instead of using
assert.eq and similar functions.
This change includes some refactoring of the golden-test classes to avoid
pulling the unittest library into the mongo shell.
Diffstat (limited to 'jstests/libs/golden_test.js')
-rw-r--r-- | jstests/libs/golden_test.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/jstests/libs/golden_test.js b/jstests/libs/golden_test.js new file mode 100644 index 00000000000..f3e218f1795 --- /dev/null +++ b/jstests/libs/golden_test.js @@ -0,0 +1,57 @@ + +function tojsonOnelineSortKeys(x) { + let indent = " "; + let nolint = true; + let depth = undefined; + let sortKeys = true; + return tojson(x, indent, nolint, depth, sortKeys); +} + +// Takes an array of documents. +// - Discards the field ordering, by recursively sorting the fields of each object. +// - Discards the result-set ordering by sorting the array of normalized documents. +// Returns a string. +function normalize(result) { + return result.map(d => tojsonOnelineSortKeys(d)).sort().join('\n') + '\n'; +} + +// Override print to output to both stdout and the golden file. +// This affects everything that uses print: printjson, jsTestLog, etc. +print = (() => { + const original = print; + return function print(...args) { + // Imitate GlobalInfo::Functions::print::call. + const str = args.map(a => a == null ? '[unknown type]' : a).join(' '); + _writeGoldenData(str); + + return original(...args); + }; +})(); + +// Takes an array or cursor, and prints a normalized version of it. +// +// Normalizing means ignoring: +// - order of fields in a document +// - order of documents in the array/cursor. +// +// If running the query fails, this catches and prints the exception. +function show(cursorOrArray) { + if (!Array.isArray(cursorOrArray)) { + try { + cursorOrArray = cursorOrArray.toArray(); + } catch (e) { + print(tojson(e)); + return; + } + } + + print(normalize(cursorOrArray)); +} + +// Run any set-up necessary for a golden jstest. +// This function should be called from the suite definition, so that individual tests don't need +// to remember to call it. This function should not be called from any libs/*.js file, because +// it's surprising if load() has side effects (besides defining JS functions / values). +function beginGoldenTest() { + _openGoldenData(jsTestName()); +} |