explorer-data/utils/test.js

99 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

import Ajv from "https://esm.sh/ajv@8.17.1?pin=v58";
2023-10-07 20:48:35 +02:00
import addFormats from "https://esm.sh/ajv-formats@2.1.1";
import { betterAjvErrors } from 'https://esm.sh/@apideck/better-ajv-errors@0.3.6?pin=v58';
2023-10-07 20:48:35 +02:00
import yaml from "npm:js-yaml";
2023-10-07 20:01:09 +02:00
import { W3PData } from "./w3pdata.js";
2023-10-07 20:48:35 +02:00
const w3pd = new W3PData();
await w3pd.init();
const ajv = new Ajv({ strict: false, allErrors: true });
2023-10-07 20:48:35 +02:00
addFormats(ajv);
async function loadSchemas() {
const out = {};
for await (const f of Deno.readDir(schemaDir)) {
const col = f.name.split(".")[0];
out[col] = yaml.load(await Deno.readTextFile(`${schemaDir}/${f.name}`));
}
return out;
}
function getDeepPropertiesKeys(obj, parentKey = '') {
let keys = [];
if (obj.hasOwnProperty('properties')) {
const properties = obj['properties'];
for (const key in properties) {
if (properties.hasOwnProperty(key)) {
const newKey = parentKey ? `${parentKey}.${key}` : key;
if (properties[key].hasOwnProperty('properties')) {
keys = keys.concat(getDeepPropertiesKeys(properties[key], newKey));
} else {
keys.push(newKey);
}
}
}
}
return keys;
}
2023-10-07 20:48:35 +02:00
const matrix = {
categories: "category",
projects: "project",
2024-09-12 18:11:39 +02:00
assets: "asset",
ecosystems: "ecosystem",
features: "feature",
usecases: "usecase",
ranks: "rank",
2024-09-20 12:25:09 +02:00
custodys: "custody",
phases: "phase",
requirements: "requirement"
2023-10-07 20:48:35 +02:00
};
const schemaDir = "./schema";
const schemas = await loadSchemas();
schemas.rank.properties.references.items.properties.field.enum = getDeepPropertiesKeys(schemas.project);
schemas.project.properties.categories.items.enum = w3pd.data.categories.map((c) => c.id);
schemas.project.properties.usecases.items.enum = w3pd.data.usecases.map((c) => c.id);
2023-10-08 01:16:03 +02:00
2023-10-07 20:48:35 +02:00
for (const col of Object.keys(w3pd.data)) {
const validator = ajv.compile(schemas[matrix[col]]);
const ids = [];
2023-10-07 20:01:09 +02:00
2023-10-07 20:48:35 +02:00
for (const item of w3pd.data[col]) {
delete item._path;
const testName =
`${col}/${item.id} ` +
(col === "projects"
? `[${
Array.isArray(item.categories)
? item.categories.join(", ")
: item.categories
}]`
: "");
2023-10-07 21:06:05 +02:00
2023-10-08 01:16:03 +02:00
if (ids.includes(item.id)) {
Deno.test(testName + " (id-duplicates)", () => {
throw { message: `Duplicate project id="${item.id}"` };
});
2023-10-07 21:06:05 +02:00
}
if (Object.keys(item).length > 1) {
Deno.test(testName + " (schema)", () => {
2023-10-08 01:16:03 +02:00
if (!validator(item)) {
const betterErrors = betterAjvErrors({ errors: validator.errors });
2024-09-17 08:32:38 +02:00
// throw betterErrors;
console.log(betterErrors);
2023-10-08 01:16:03 +02:00
}
});
}
ids.push(item.id);
2023-10-07 20:48:35 +02:00
}
}