summaryrefslogtreecommitdiff
path: root/jstests/libs/json_schema_test_runner.js
diff options
context:
space:
mode:
authorNick Zolnierz <nicholas.zolnierz@mongodb.com>2017-07-31 18:14:18 -0400
committerNick Zolnierz <nicholas.zolnierz@mongodb.com>2017-08-11 11:43:45 -0400
commit91f42e47345e58c3f277dba6276d70a41cb8daec (patch)
tree9a4ba9b81d4c8dec0a94e15fa4865ca77a5f86c9 /jstests/libs/json_schema_test_runner.js
parentf2b0ffdc54647aa6dd7cddfb740a63f419d30d55 (diff)
downloadmongo-91f42e47345e58c3f277dba6276d70a41cb8daec.tar.gz
SERVER-29593: Integrate the official JSON Schema test corpus into the unit tests
Diffstat (limited to 'jstests/libs/json_schema_test_runner.js')
-rw-r--r--jstests/libs/json_schema_test_runner.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/jstests/libs/json_schema_test_runner.js b/jstests/libs/json_schema_test_runner.js
new file mode 100644
index 00000000000..623084939d4
--- /dev/null
+++ b/jstests/libs/json_schema_test_runner.js
@@ -0,0 +1,46 @@
+/**
+ * Test runner responsible for parsing and executing a JSON-Schema-Test-Suite json file.
+ */
+(function() {
+ "use strict";
+
+ 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) {
+ 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({foo: data}));
+
+ let actualCount;
+ try {
+ actualCount = coll.find({$jsonSchema: {properties: {foo: schema}}}).itcount();
+ } catch (e) {
+ throw new Error(tojson(e) + ": Failed to parse JSON 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"]));
+ });
+}());