summaryrefslogtreecommitdiff
path: root/jstests/libs/json_schema_test_runner.js
blob: 8955f188c2aa6780dd0296a701e8d09771f218f7 (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
/**
 * Test runner responsible for parsing and executing a JSON-Schema-Test-Suite json file.
 */
(function() {
    "use strict";

    load("jstests/libs/assert_schema_match.js");

    const coll = db.json_schema_test_corpus;
    coll.drop();

    const jsonFilename = jsTestOptions().jsonSchemaTestFile;

    if (jsonFilename === undefined) {
        throw new Error('JSON Schema tests must be run through resmoke.py');
    }

    function runSchemaTest(test, schema, banFromTopLevel) {
        assert(test.hasOwnProperty("data"), "JSON Schema test requires 'data'");
        assert(test.hasOwnProperty("valid"), "JSON Schema test requires 'valid'");
        const data = test["data"];
        const valid = test["valid"];

        try {
            assertSchemaMatch(coll,
                              {properties: {schema_test_wrapper: schema}},
                              {schema_test_wrapper: data},
                              valid);

            // Run against a top-level schema if the data is an object, since MongoDB only stores
            // records as documents.
            // (Note: JS notion of an 'object' includes arrays and null.)
            if (typeof data === "object" && !Array.isArray(data) && data !== null &&
                banFromTopLevel !== true) {
                assertSchemaMatch(coll, schema, data, valid);
            }
        } catch (e) {
            throw new Error(tojson(e) + "\n\nJSON Schema test failed for schema " + tojson(schema) +
                            " and data " + tojson(data));
        }
    }

    const testGroupList = JSON.parse(cat(jsonFilename));
    testGroupList.forEach(function(testGroup) {
        assert(testGroup.hasOwnProperty("schema"), "JSON Schema test requires a 'schema'");
        assert(testGroup.hasOwnProperty("tests"), "JSON Schema test requires a 'tests' list");
        testGroup["tests"].forEach(
            test => runSchemaTest(test, testGroup["schema"], testGroup["banFromTopLevel"]));
    });
}());