Initial incorporation of Annotation Schema MUSTs (#32)
authorBigBlueHat <byoung@bigbluehat.com>
Tue, 12 Jun 2018 14:34:02 +0000 (07:34 -0700)
committerGitHub <noreply@github.com>
Tue, 12 Jun 2018 14:34:02 +0000 (07:34 -0700)
* Initial incorporation of Annotation Schema MUSTs

Fixes #31.

Plenty more schemas where these came from. Hopefully this sets the stage for
similar testings of the SHOULDs, Collections, etc.

* Gather .mjs files from test/ if present

package-lock.json [new file with mode: 0644]
package.json
test/data-model.mjs [new file with mode: 0644]
test/mocha.opts
yarn.lock

diff --git a/package-lock.json b/package-lock.json
new file mode 100644 (file)
index 0000000..e5cd3c5
--- /dev/null
@@ -0,0 +1,10 @@
+{
+  "name": "@annotator/annotator",
+  "requires": true,
+  "lockfileVersion": 1,
+  "dependencies": {
+    "web-annotation-tests": {
+      "version": "file:../web-annotation-tests"
+    }
+  }
+}
index 8d7c200..22006b9 100644 (file)
@@ -29,6 +29,7 @@
     "@babel/preset-env": "^7.0.0-beta.42",
     "@babel/register": "^7.0.0-beta.42",
     "@babel/runtime": "^7.0.0-beta.42",
+    "ajv": "^6.5.0",
     "babel-eslint": "^8.2.2",
     "babel-loader": "^8.0.0-beta.2",
     "babel-plugin-istanbul": "^4.1.5",
@@ -53,6 +54,7 @@
     "nyc": "^11.6.0",
     "prettier": "^1.11.1",
     "rimraf": "^2.6.2",
+    "web-annotation-tests": "https://github.com/w3c/web-annotation-tests",
     "webpack": "^4.2.0",
     "webpack-serve": "^0.2.0"
   },
diff --git a/test/data-model.mjs b/test/data-model.mjs
new file mode 100644 (file)
index 0000000..d85cfbe
--- /dev/null
@@ -0,0 +1,68 @@
+/* global require, process, describe, before, it, assert */
+
+import fs from 'fs';
+import URL from 'url';
+
+import Ajv from 'ajv';
+const ajv = new Ajv({ schemaId: 'auto' });
+ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
+
+// add defintion files to available schemas
+const defs = fs.readdirSync('node_modules/web-annotation-tests/definitions/');
+defs.forEach(def => {
+  if (def.substr(-4) === 'json')
+    ajv.addSchema(require('web-annotation-tests/definitions/' + def));
+});
+
+// file or URL location
+let url = '';
+// find the URL parameter (which might be a relative path to a file)
+let found_url = false;
+process.argv.forEach((val, index) => {
+  if (val.startsWith('--url')) {
+    found_url = true;
+    if (val[5] === '=') {
+      url = val.split('=')[1];
+    } else {
+      // assume the next parameter is a URL
+      url = process.argv[index + 1];
+    }
+  }
+});
+
+// load the annotationMusts test list
+const musts = JSON.parse(
+  fs.readFileSync(
+    'node_modules/web-annotation-tests/annotations/annotationMusts.test'
+  )
+);
+
+describe('Test schemas', () => {
+  let data = '';
+
+  before(function() {
+    if (!found_url) {
+      this.skip();
+    } else {
+      // load the data from the file or URL
+      let url_parsed = URL.parse(url);
+      if (url_parsed.path !== url_parsed.href) {
+        // TODO: GET the URL's JSON and use that
+      } else {
+        // assume we have a local file and use that
+        data = JSON.parse(fs.readFileSync(url_parsed.path, 'utf8'));
+      }
+      if (data === '') {
+        this.skip();
+      }
+    }
+  });
+
+  musts.assertions.forEach(path => {
+    const schema = require('web-annotation-tests/' + path);
+    it(schema.title, () => {
+      let valid = ajv.validate(schema, data);
+      assert.isOk(valid, ajv.errorsText());
+    });
+  });
+});
index 293695d..2d8e8b1 100644 (file)
@@ -4,3 +4,4 @@
 --recursive
 --watch-extensions mjs
 packages/*/test/**/*.mjs
+test/**/*.mjs
index c37ed76..2364d0a 100644 (file)
--- a/yarn.lock
+++ b/yarn.lock
@@ -853,7 +853,7 @@ ajv@^5.0.0, ajv@^5.2.3, ajv@^5.3.0:
     fast-json-stable-stringify "^2.0.0"
     json-schema-traverse "^0.3.0"
 
-ajv@^6.1.0:
+ajv@^6.1.0, ajv@^6.5.0:
   version "6.5.0"
   resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.5.0.tgz#4c8affdf80887d8f132c9c52ab8a2dc4d0b7b24c"
   dependencies:
@@ -6804,6 +6804,10 @@ wcwidth@^1.0.0:
   dependencies:
     defaults "^1.0.3"
 
+"web-annotation-tests@https://github.com/w3c/web-annotation-tests":
+  version "1.0.0"
+  resolved "https://github.com/w3c/web-annotation-tests#9226df61820a7fe7a2ddf20a4e452761f1a76c4e"
+
 webpack-dev-middleware@^3.0.0:
   version "3.0.1"
   resolved "https://registry.yarnpkg.com/webpack-dev-middleware/-/webpack-dev-middleware-3.0.1.tgz#7ffd6d0192883c83d3f262e8d7dec822493c6166"