summaryrefslogtreecommitdiff
path: root/jstests/libs/json_schema_test_runner.js
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/libs/json_schema_test_runner.js')
-rw-r--r--jstests/libs/json_schema_test_runner.js35
1 files changed, 19 insertions, 16 deletions
diff --git a/jstests/libs/json_schema_test_runner.js b/jstests/libs/json_schema_test_runner.js
index 82ba8c2678d..8955f188c2a 100644
--- a/jstests/libs/json_schema_test_runner.js
+++ b/jstests/libs/json_schema_test_runner.js
@@ -4,6 +4,8 @@
(function() {
"use strict";
+ load("jstests/libs/assert_schema_match.js");
+
const coll = db.json_schema_test_corpus;
coll.drop();
@@ -13,35 +15,36 @@
throw new Error('JSON Schema tests must be run through resmoke.py');
}
- function runSchemaTest(test, schema) {
+ 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"];
- coll.drop();
- assert.writeOK(coll.insert({schema_test_wrapper: data}));
-
- let actualCount;
try {
- actualCount =
- coll.find({$jsonSchema: {properties: {schema_test_wrapper: schema}}}).itcount();
+ 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) + ": Failed to parse JSON Schema " + tojson(schema) +
- " and data : " + tojson(data));
+ throw new Error(tojson(e) + "\n\nJSON Schema test failed for schema " + tojson(schema) +
+ " and data " + tojson(data));
}
-
- const expectedCount = valid ? 1 : 0;
- assert.eq(
- expectedCount,
- actualCount,
- "JSON 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["tests"].forEach(
+ test => runSchemaTest(test, testGroup["schema"], testGroup["banFromTopLevel"]));
});
}());