apxtrib/api/models/unittest/Checkjson.js

159 lines
3.6 KiB
JavaScript
Raw Normal View History

2023-03-27 05:52:21 +00:00
/*
Unit testing
*/
const assert = require("assert");
2023-04-13 05:46:35 +00:00
const Checkjson = require("../Checkjson.js");
2023-11-05 11:03:25 +00:00
const conf = require(`${process.env.dirtown}/conf.json`);
2023-03-27 05:52:21 +00:00
2023-04-13 05:46:35 +00:00
const ut = { name: "Checkjson" };
2023-03-27 05:52:21 +00:00
const schema = {
$schema: "http://json-schema.org/schema#",
2023-04-13 05:46:35 +00:00
title: "Dummy schema to test Checkjson.js",
description: "Checkjson is use on server as well as into a browser",
2023-03-27 05:52:21 +00:00
$comment: "We change schema type on the fly to simplify the test",
2023-04-13 05:46:35 +00:00
type: "object",
2023-03-27 05:52:21 +00:00
properties: {
totest: {},
},
};
const testproperties = [
2023-11-05 11:03:25 +00:00
{
name: "test0",
data: { totest: true },
properties: { totest: { type: "boolean" } },
status: 200,
},
2023-03-27 05:52:21 +00:00
{
name: "test1",
data: { totest: "blabla" },
properties: { totest: { type: "string" } },
2023-11-05 11:03:25 +00:00
status: 200,
2023-03-27 05:52:21 +00:00
},
{
name: "test2",
data: { totest: 123 },
properties: { totest: { type: "string" } },
2023-11-05 11:03:25 +00:00
status: 417,
2023-03-27 05:52:21 +00:00
},
{
name: "test3",
data: { totest: 123.13 },
properties: { totest: { type: "integer" } },
2023-11-05 11:03:25 +00:00
status: 417,
2023-03-27 05:52:21 +00:00
},
{
name: "test4",
data: { totest: 123 },
properties: { totest: { type: "number" } },
2023-11-05 11:03:25 +00:00
status: 200,
2023-03-27 05:52:21 +00:00
},
{
name: "test5",
data: { totest: 12312 },
properties: { totest: { type: "number" } },
2023-11-05 11:03:25 +00:00
status: 200,
2023-03-27 05:52:21 +00:00
},
{
name: "test6",
data: { totest: 12.313 },
properties: { totest: { type: "float" } },
2023-11-05 11:03:25 +00:00
status: 200,
2023-03-27 05:52:21 +00:00
},
{
name: "test7",
2023-11-05 11:03:25 +00:00
data: { totest: "blablab sfde" },
2023-03-27 05:52:21 +00:00
properties: { totest: { type: "string", minLength: 1111 } },
2023-11-05 11:03:25 +00:00
status: 417,
2023-03-27 05:52:21 +00:00
},
{
name: "test8",
2023-11-05 11:03:25 +00:00
data: { totest: "blablab sfde" },
properties: { totest: { type: "string", minLength: 4, maxLength: 128 } },
status: 200,
2023-03-27 05:52:21 +00:00
},
{
name: "test9",
2023-11-05 11:03:25 +00:00
data: { totest: 12 },
properties: { totest: { type: "integer", multipleOf: 3 } },
status: 200,
2023-03-27 05:52:21 +00:00
},
{
name: "test10",
2023-11-05 11:03:25 +00:00
data: { totest: 9 },
properties: {
totest: { type: "number", minimum: -10, exclusiveMaximum: 10 },
},
status: 200,
2023-03-27 05:52:21 +00:00
},
{
name: "test11",
2023-11-05 11:03:25 +00:00
data: { totest: 10 },
properties: {
totest: { type: "number", minimum: -10, exclusiveMaximum: 10 },
},
status: 417,
2023-03-27 05:52:21 +00:00
},
{
name: "test12",
2023-11-05 11:03:25 +00:00
data: { totest: "gfhrtabcdgfr" },
properties: { totest: { type: "string", pattern: /.*abc.*/ } },
status: 200,
2023-03-27 05:52:21 +00:00
},
{
name: "test13",
2023-11-05 11:03:25 +00:00
data: { totest: "toto@google.com" },
properties: { totest: { type: "string", format: "email" } },
status: 200,
2023-03-27 05:52:21 +00:00
},
{
name: "test14",
2023-11-05 11:03:25 +00:00
data: { totest: "Aze123@0" },
properties: { totest: { type: "string", format: "password" } },
status: 200,
2023-03-27 05:52:21 +00:00
},
{
name: "test15",
2023-11-05 11:03:25 +00:00
data: { totest: "value1" },
properties: {
totest: { type: "string", enum: ["value1", "value2", "value3"] },
},
status: 200,
2023-03-27 05:52:21 +00:00
},
{
name: "test16",
2023-11-05 11:03:25 +00:00
data: { totest: ["t1", "t2"] },
properties: { totest: { type: ["string", "number"] } },
status: 417,
},
2023-03-27 05:52:21 +00:00
{
name: "test17",
data: { totest: 12 },
2023-11-05 11:03:25 +00:00
properties: { totest: { type: ["string", "number"] } },
status: 200,
},
2023-03-27 05:52:21 +00:00
];
ut.testproperties = (options) => {
let msg = "";
testproperties.forEach((t) => {
schema.properties = t.properties;
2023-04-13 05:46:35 +00:00
const res = Checkjson.schema.data(schema, t.data);
2023-03-27 05:52:21 +00:00
if (res.status != t.status) {
2023-11-05 11:03:25 +00:00
msg = msg == "" ? "Unconsistent testproperties() name list: " : `${msg},`;
2023-03-27 05:52:21 +00:00
if (options.verbose) {
2023-11-05 11:03:25 +00:00
console.log(t);
console.log(res);
2023-03-27 05:52:21 +00:00
}
msg += res.err.map((e) => ` ${t.name} ${e.info}`);
}
});
return assert.deepEqual(msg, "", msg);
};
ut.run = (options) => {
2023-04-13 05:46:35 +00:00
console.log("Test Checkjson properties");
2023-03-27 05:52:21 +00:00
ut.testproperties(options);
};
module.exports = ut;