prague-blockchain-week/utils/test.js

55 řádky
1.5 KiB
JavaScript
Surový Trvalý odkaz Normální zobrazení Historie

2023-01-23 02:27:35 +01:00
import { DeConfEngine } from "./engine.js";
// initialize ajv JSON Schema validator
import Ajv from "npm:ajv@8.8.2";
import addFormats from "npm:ajv-formats@2.1.1";
2023-01-23 02:27:35 +01:00
2023-01-24 06:05:24 +01:00
const ajv = new Ajv({ strict: false });
2023-01-23 02:27:35 +01:00
addFormats(ajv);
const dc = new DeConfEngine({ silent: true });
await dc.init();
const schemas = await dc.schemas();
const validators = {};
for (const item of schemas) {
validators[item.name] = ajv.compile(item.schema);
}
2023-01-23 15:02:55 +01:00
function checkCollection(entry, entryInfo, colName) {
for (const event of entry.data[colName]) {
Deno.test(`[${entryInfo} ${colName}=${event.id}] index`, () => {
// check event index
2023-01-24 04:02:46 +01:00
const k = entry.colMapper[colName];
2023-01-23 15:02:55 +01:00
if (!validators[k](event.data.index)) {
throw validators[k].errors;
}
});
2023-01-23 15:13:50 +01:00
// check specific collections
2023-01-28 04:22:27 +01:00
if (colName === "events" && event.data.index.venues) {
Deno.test(`[${entryInfo} ${colName}=${event.id}] venues link`, () => {
for (const placeId of event.data.index.venues) {
if (!entry.data.places.find((p) => p.id === placeId)) {
throw new Error(`Place not exists = ${placeId}`);
}
2023-01-23 15:12:56 +01:00
}
});
}
2023-01-23 15:02:55 +01:00
}
}
2023-01-23 02:27:35 +01:00
for (const entry of dc.entries) {
// check index
const entryInfo = `entry=${entry.id}`;
Deno.test(`[${entryInfo}] index`, () => {
if (!validators.index(entry.data.index)) {
throw validators.index.errors;
}
});
2023-01-23 15:02:55 +01:00
// check all collections
2023-01-24 04:02:46 +01:00
for (const col of Object.keys(entry.colMapper)) {
2023-01-23 15:02:55 +01:00
checkCollection(entry, entryInfo, col);
2023-01-23 02:27:35 +01:00
}
}