From c26b4318eeaad7117566d1e0ffd5dca77dbdd717 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Sun, 1 Jun 2025 11:24:20 -0600 Subject: [PATCH] Finish writing the schema tests --- module/__tests__/registration.mjs | 4 ++ module/__tests__/schemas/row.mjs | 96 ++++++++++++++++++++++++++++++ module/__tests__/schemas/table.mjs | 43 +++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 module/__tests__/schemas/row.mjs create mode 100644 module/__tests__/schemas/table.mjs diff --git a/module/__tests__/registration.mjs b/module/__tests__/registration.mjs index 787c4f1..83e025c 100644 --- a/module/__tests__/registration.mjs +++ b/module/__tests__/registration.mjs @@ -1,9 +1,13 @@ import { barGraphTests } from "./schemas/barGraph.mjs"; import { numberBucketTests } from "./schemas/numberBucket.mjs"; +import { rowTests } from "./schemas/row.mjs"; import { stringBucketTests } from "./schemas/stringBucket.mjs"; +import { tableTests } from "./schemas/table.mjs"; Hooks.on(`quenchReady`, (quench) => { numberBucketTests(quench); stringBucketTests(quench); barGraphTests(quench); + tableTests(quench); + rowTests(quench); }); diff --git a/module/__tests__/schemas/row.mjs b/module/__tests__/schemas/row.mjs new file mode 100644 index 0000000..394e42e --- /dev/null +++ b/module/__tests__/schemas/row.mjs @@ -0,0 +1,96 @@ +import { api } from "../../api.mjs"; +import { PrivacyMode } from "../../utils/privacy.mjs"; + +export function rowTests(quench) { + quench.registerBatch( + `${__ID__}.rowSchema`, + (ctx) => { + const { describe, it, expect } = ctx; + + describe(`the row schema`, () => { + it(`should allow number-based values`, () => { + const { error } = api.schemas.row.validate( + { + _id: `1`, + timestamp: (new Date()).toISOString(), + value: 1, + privacy: PrivacyMode.PUBLIC, + }, + ); + expect(error).to.be.undefined; + }); + + it(`should allow string-based values`, () => { + const { error } = api.schemas.row.validate( + { + _id: `1`, + timestamp: (new Date()).toISOString(), + value: `apple`, + privacy: PrivacyMode.PUBLIC, + }, + ); + expect(error).to.be.undefined; + }); + + it(`shouldn't allow invalid privacy modes`, () => { + const { error } = api.schemas.row.validate( + { + _id: `1`, + timestamp: (new Date()).toISOString(), + value: 1, + privacy: `yahaha`, + }, + ); + expect(error).not.to.be.undefined; + }); + + it(`shouldn't allow invalid value modes`, () => { + const { error } = api.schemas.row.validate( + { + _id: `1`, + timestamp: (new Date()).toISOString(), + value: true, + privacy: PrivacyMode.PUBLIC, + }, + ); + expect(error).not.to.be.undefined; + }); + + it(`shouldn't allow non-ISO date formats`, () => { + const { error } = api.schemas.row.validate( + { + _id: `1`, + timestamp: (new Date()).toDateString(), + value: 1, + privacy: PrivacyMode.PUBLIC, + }, + ); + expect(error).not.to.be.undefined; + }); + + it(`should require an ID to be present`, () => { + const { error } = api.schemas.row.validate( + { + timestamp: (new Date()).toISOString(), + value: true, + privacy: PrivacyMode.PUBLIC, + }, + ); + expect(error).not.to.be.undefined; + }); + + it(`shouldn't allow empty string as a value`, () => { + const { error } = api.schemas.row.validate( + { + _id: `1`, + timestamp: (new Date()).toISOString(), + value: ``, + privacy: PrivacyMode.PUBLIC, + }, + ); + expect(error).not.to.be.undefined; + }); + }); + }, + ); +}; diff --git a/module/__tests__/schemas/table.mjs b/module/__tests__/schemas/table.mjs new file mode 100644 index 0000000..c3d430e --- /dev/null +++ b/module/__tests__/schemas/table.mjs @@ -0,0 +1,43 @@ +import { api } from "../../api.mjs"; + +const graph = { type: `bar` }; +const buckets = { type: `string` }; + +export function tableTests(quench) { + quench.registerBatch( + `${__ID__}.tableSchema`, + (ctx) => { + const { describe, it, expect } = ctx; + + describe(`the table schema`, () => { + it(`should require that name be a non-empty string`, () => { + const { error } = api.schemas.table.validate( + { name: ``, graph, buckets }, + ); + expect(error).not.to.be.undefined; + }); + + it(`should require that name only contain alphanumeric characters`, () => { + const { error } = api.schemas.table.validate( + { name: `:(`, graph, buckets }, + ); + expect(error).not.to.be.undefined; + }); + + it(`should allow the name to contain spaces`, () => { + const { error } = api.schemas.table.validate( + { name: `a name with spaces`, graph, buckets }, + ); + expect(error).to.be.undefined; + }); + + it(`should allow a single forward slash for subtables`, () => { + const { error } = api.schemas.table.validate( + { name: `Table/subtable`, graph, buckets }, + ); + expect(error).to.be.undefined; + }); + }); + }, + ); +};