Commit e619ab38 authored by PTSEFTON's avatar PTSEFTON
Browse files

Added testing framework

parent 4f8bd9b7
This diff is collapsed.
......@@ -50,10 +50,11 @@ table.table {
<div class="jumbotron">
<h4 class="citation"><%- citation %></h3>
<h3 class="item_name"><%- item_name %></h4>
<h4><%- zip_link %></h4>
<a href="./ro-crate-metadata.jsonld">Download all the metadata for <span class='name'><%- dataset_name %></span> in JSON-LD format</a>
<div class="check"></div>
</div>
......
......@@ -29,7 +29,6 @@ class Checker {
name: "Has root Dataset",
message: "There is a JSON-LD item with @type of Dataset (http://schema.org/dataset)"
}
)
if (this.helper.root_node) {
checkItem.status = true;
......@@ -65,7 +64,6 @@ class Checker {
if (t["@id"] && this.helper.item_by_id[t["@id"]]) {
var creator = this.helper.item_by_id[t["@id"]];
const types = this.helper.value_as_array(creator["@type"]);
console.log("TYPES", types)
if (types.includes("Person") || types.includes("Organization")){
checkItem.status = true;
}
......@@ -89,43 +87,110 @@ class Checker {
var checkItem = new CheckItem(
{
name: "Has a license ",
message: "The root Dataset has a License with a URL or a local copy"
message: "The root Dataset has a License of type CreativeWork with a description"
}
)
if (this.helper.root_node && this.helper.root_node.license) {
const targets = this.helper.value_as_array(this.helper.root_node.license);
for (let t of targets) {
var license = this.helper.item_by_id[t["@id"]];
const types = this.helper.value_as_array(license["@type"]);
// TODO check that license path is a path or URL is a URL and check name and description are legit
if (types.includes("CreativeWork") && license.name && licence.description && (license.path || license.URL )){
checkItem.status = true;
break;
if (t["@id"]) {
var license = this.helper.item_by_id[t["@id"]];
const types = this.helper.value_as_array(license["@type"]);
// TODO check that license path is a path or URL is a URL and check name and description are legit
if (types.includes("CreativeWork") && license.name && license.description){
checkItem.status = true;
break;
}
}
}
}
return checkItem;
}
hasDatePublished() {
var checkItem = new CheckItem(
{
name: "Has a datePublished ",
message: "The root Dataset has a datePublished with ONE value which is an ISO 8601 format precision of at least a day"
}
)
var date = this.helper.value_as_array(this.helper.root_node.datePublished);
if (date.length != 1) {
checkItem.diagnostics = `Number of datePublished values is ${date.length} NOT 1`;
return checkItem;
}
checkItem.status =date[0].match(/^\d{4}-([0]\d|1[0-2])-([0-2]\d|3[01])/)
return checkItem;
}
hasContactPoint() {
var checkItem = new CheckItem(
{
name: "Has a contactPoint",
message: "The root Dataset has at least one contactPoint property which references a ContactPoint of type Customer Service"
}
)
if (this.helper.root_node && this.helper.root_node.contactPoint) {
const targets = this.helper.value_as_array(this.helper.root_node.contactPoint);
for (let t of targets) {
if (t["@id"]) {
var contact= this.helper.item_by_id[t["@id"]];
const types = this.helper.value_as_array(contact["@type"]);
if (types.includes("ContactPoint") && this.helper.value_as_array(contact.contactType).includes("customer service") && contact.email) {
checkItem.status = true;
break;
}
}
}
}
return checkItem;
}
check() {
var context = this.hasContext();
this.checklist.push(context);
var dataset = this.hasRootDataset();
this.checklist.push(dataset);
var name = this.hasName()
this.checklist.push(name);
var creator = this.hasCreator();
this.checklist.push(creator);
var license = this.hasLicense();
this.checklist.push(license);
var date = this.hasDatePublished();
this.checklist.push(date);
var contact = this.hasContactPoint();
this.checklist.push(contact);
this.isROCrate = context.status && dataset.status && name.status && creator.status && license.status && date.status;
// TODO:
// this.isDistributable
check () {
this.checklist.push(this.hasContext());
this.checklist.push(this.hasRootDataset());
this.checklist.push(this.hasName());
this.checklist.push(this.hasCreator());
this.checklist.push(this.hasLicense());
// this.isCitable
}
summarize() {
if (this.isROCrate) {
return "This is a valid RO-Crate";
}
else {
return "This is not a valid RO-Crate";
}
}
report() {
var report = [];
for (var item of this.checklist) {
const tick = item.status ? "✔️" : "";
report.push(`${tick} ${item.name}: ${item.message}]`);
report.push(`${tick} ${item.name}: ${item.message}`);
}
return report.join("\n");
......
const Preview = require("./ro-crate-preview.js");
const Checker = require("./checker.js");
var meta;
var preview;
const check = function check() {
var checker = new Checker(meta);
checker.check();
var preview
$("div.check").html(`<details><summary>${checker.summarize()}</summary><a href='#___check____'><pre>${checker.report()}</pre></a></details>`);
}
async function load() {
meta = JSON.parse($("script[type='application/ld+json']").text());
preview = await new Preview(meta);
$("div.check").html("<button><a href='#___check____'>Check this crate</a></button>")
var hash = location.hash;
if (hash) {
if (hash.startsWith('#___check')){
check()
} else if (hash) {
preview.display(unescape(hash.replace("#", "")));
}
else {
......@@ -17,6 +24,8 @@ async function load() {
}
}
window.onhashchange = function() {
load()
}
......
......@@ -184,8 +184,9 @@ class jsonldHelper {
}
add_back_links() {
// Add @reverse properties if not there
for (let item of this.json_ld["@graph"]) {
for (let item of this.json_ld["@graph"]){
this.make_back_links(item);
}
}
......
......@@ -49,6 +49,7 @@ class Preview {
this.json_string = JSON.stringify(json);
this.helper = new jsonldHelper();
this.helper.init(JSON.parse(this.json_string));
this.helper.add_back_links();
this.rootId = this.helper.rootId;
this.root = this.helper.root_node;
}
......@@ -139,8 +140,7 @@ class Preview {
}
else {
this.addMetadataRow(meta, item, prop);
}
}
}
if (item.path){
var path = this.helper.value_as_array(item.path)[0];
......@@ -299,7 +299,7 @@ class Preview {
name = val['@id'];
}
}
return $("<a href='#" + target["@id"] + "'> </a>").html(name).attr("onclick", "preview.display('" + target["@id"] + "')" );
return $("<a href='#" + target["@id"] + "'> </a>").html(name);
}
}
/*
......
......@@ -26,7 +26,7 @@ chai.use(require('chai-fs'));
const defaults = require("../lib/defaults")
describe('Incremental checking', function () {
it('should have a @context', async function () {
it('should have trigger all the right reporting', async function () {
//json = JSON.parse(fs.readFileSync("test_data/sample-ro-crate-metadata.jsonld"));
json = {}
......@@ -74,17 +74,53 @@ describe('Incremental checking', function () {
assert(checker.hasCreator().status, "Does have a creator with @type Person or Organization");
// License
// No name, description
assert(!checker.hasLicense().status, "Does not have a license with @type CreativeWork");
var license = {"@id": "http://example.com/some_kind_of_license", "@type": "CreativeWork", "URL": "http://example.com/some_kind_of_license"}
dataset.license = {"@id": "http://example.com/some_kind_of_license"}
var license = {"@id": "http://example.com/some_kind_of_license", "@type": "CreativeWork", "URL": "http://example.com/some_kind_of_license"};
dataset.license = {"@id": "http://example.com/some_kind_of_license"};
json["@graph"].push(license);
var checker = new Checker(json);
assert(!checker.hasLicense().status, "Does not have a license with @type CreativeWork");
license.name = "Some license";
license.path = "/path/";
license.description = "Description of at least 20 characters.";
assert(checker.hasLicense().status, "Does not have a license with @type CreativeWork");
// Should not check out as having a liense without path
delete license.path;
assert(!checker.hasLicense().status, "Does not have a license with @type CreativeWork");
license.url = "http://example.com/some_license";
assert(checker.hasLicense().status, "Does not have a license with @type CreativeWork");
// datePublished
assert(!checker.hasDatePublished().status, "Does not have a datePublished");
dataset.datePublished = "2017"; // Not enough detail!
assert(!checker.hasDatePublished().status, "Does not have a datePublished (not enough detail)");
dataset.datePublished = ["2017-07-21", "2019-08-09"]; // this should do it
assert(!checker.hasDatePublished().status, "Does not have have a single datePublished");
dataset.datePublished = ["2017-07-21"]; // this should do it
assert(checker.hasDatePublished().status, "Does have a datePublished");
//contactPoint missing
assert(!checker.hasContactPoint().status, "Does not have have a single contact point");
var contact = {"@id": "some.email@example.com", "@type": "ContactPoint"} // Not enough
dataset.contact = [{"@id": "some.email@example.com"}];
json["@graph"].push(contact);
var checker = new Checker(json);
assert(!checker.hasContactPoint().status, "Does not have have a contact point with enough properties");
contact.contactType = 'customer service';
contact.email = 'some@email'; // TODO: Not validated!
var checker = new Checker(json);
console.log(JSON.stringify(json["@graph"]))
assert(checker.hasContactPoint().status, "Does have have a contact point");
checker.check();
console.log(checker.report());
});
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment