reorganized storage adapter and added test suites for adapter and fields (#124)

* reorganized storage adapter and added test suites for adapter and fields

* added build command in ci pipeline

* updated workflow to also run node tests

* updated workflow: try with separate tasks

* updated workflow: try with separate tasks

* updated workflow: added tsx as dev dependency

* updated workflow: try with find instead of glob
This commit is contained in:
dswbx
2025-03-27 20:41:42 +01:00
committed by GitHub
parent 40c9ef9d90
commit 9e3c081e50
45 changed files with 605 additions and 940 deletions

View File

@@ -1,9 +1,9 @@
import { describe, expect, test } from "bun:test";
import { BooleanField } from "../../../../src/data";
import { runBaseFieldTests, transformPersist } from "./inc";
import { fieldTestSuite, transformPersist } from "data/fields/field-test-suite";
describe("[data] BooleanField", async () => {
runBaseFieldTests(BooleanField, { defaultValue: true, schemaType: "boolean" });
fieldTestSuite({ expect, test }, BooleanField, { defaultValue: true, schemaType: "boolean" });
test("transformRetrieve", async () => {
const field = new BooleanField("test");

View File

@@ -1,9 +1,9 @@
import { describe, expect, test } from "bun:test";
import { DateField } from "../../../../src/data";
import { runBaseFieldTests } from "./inc";
import { fieldTestSuite } from "data/fields/field-test-suite";
describe("[data] DateField", async () => {
runBaseFieldTests(DateField, { defaultValue: new Date(), schemaType: "date" });
fieldTestSuite({ expect, test }, DateField, { defaultValue: new Date(), schemaType: "date" });
// @todo: add datefield tests
test("week", async () => {

View File

@@ -1,13 +1,15 @@
import { describe, expect, test } from "bun:test";
import { EnumField } from "../../../../src/data";
import { runBaseFieldTests, transformPersist } from "./inc";
import { fieldTestSuite, transformPersist } from "data/fields/field-test-suite";
function options(strings: string[]) {
return { type: "strings", values: strings };
}
describe("[data] EnumField", async () => {
runBaseFieldTests(
fieldTestSuite(
{ expect, test },
// @ts-ignore
EnumField,
{ defaultValue: "a", schemaType: "text" },
{ options: options(["a", "b", "c"]) },
@@ -15,11 +17,13 @@ describe("[data] EnumField", async () => {
test("yields if default value is not a valid option", async () => {
expect(
// @ts-ignore
() => new EnumField("test", { options: options(["a", "b"]), default_value: "c" }),
).toThrow();
});
test("transformPersist (config)", async () => {
// @ts-ignore
const field = new EnumField("test", { options: options(["a", "b", "c"]) });
expect(transformPersist(field, null)).resolves.toBeUndefined();
@@ -29,6 +33,7 @@ describe("[data] EnumField", async () => {
test("transformRetrieve", async () => {
const field = new EnumField("test", {
// @ts-ignore
options: options(["a", "b", "c"]),
default_value: "a",
required: true,

View File

@@ -1,7 +1,7 @@
import { describe, expect, test } from "bun:test";
import { Default, stripMark } from "../../../../src/core/utils";
import { baseFieldConfigSchema, Field } from "../../../../src/data/fields/Field";
import { runBaseFieldTests } from "./inc";
import { fieldTestSuite } from "data/fields/field-test-suite";
describe("[data] Field", async () => {
class FieldSpec extends Field {
@@ -19,7 +19,7 @@ describe("[data] Field", async () => {
});
});
runBaseFieldTests(FieldSpec, { defaultValue: "test", schemaType: "text" });
fieldTestSuite({ expect, test }, FieldSpec, { defaultValue: "test", schemaType: "text" });
test("default config", async () => {
const config = Default(baseFieldConfigSchema, {});

View File

@@ -1,19 +1,13 @@
import { describe, expect, test } from "bun:test";
import { Type } from "../../../../src/core/utils";
import {
Entity,
EntityIndex,
type EntityManager,
Field,
type SchemaResponse,
} from "../../../../src/data";
import { Entity, EntityIndex, Field } from "../../../../src/data";
class TestField extends Field {
protected getSchema(): any {
return Type.Any();
}
schema(em: EntityManager<any>): SchemaResponse {
override schema() {
return undefined as any;
}
}

View File

@@ -1,10 +1,10 @@
import { describe, expect, test } from "bun:test";
import { JsonField } from "../../../../src/data";
import { runBaseFieldTests, transformPersist } from "./inc";
import { fieldTestSuite, transformPersist } from "data/fields/field-test-suite";
describe("[data] JsonField", async () => {
const field = new JsonField("test");
runBaseFieldTests(JsonField, {
fieldTestSuite({ expect, test }, JsonField, {
defaultValue: { a: 1 },
sampleValues: ["string", { test: 1 }, 1],
schemaType: "text",

View File

@@ -1,9 +1,10 @@
import { describe, expect, test } from "bun:test";
import { JsonSchemaField } from "../../../../src/data";
import { runBaseFieldTests } from "./inc";
import { fieldTestSuite } from "data/fields/field-test-suite";
describe("[data] JsonSchemaField", async () => {
runBaseFieldTests(JsonSchemaField, { defaultValue: {}, schemaType: "text" });
// @ts-ignore
fieldTestSuite({ expect, test }, JsonSchemaField, { defaultValue: {}, schemaType: "text" });
// @todo: add JsonSchemaField tests
});

View File

@@ -1,6 +1,6 @@
import { describe, expect, test } from "bun:test";
import { NumberField } from "../../../../src/data";
import { runBaseFieldTests, transformPersist } from "./inc";
import { fieldTestSuite, transformPersist } from "data/fields/field-test-suite";
describe("[data] NumberField", async () => {
test("transformPersist (config)", async () => {
@@ -15,5 +15,5 @@ describe("[data] NumberField", async () => {
expect(transformPersist(field2, 10000)).resolves.toBe(10000);
});
runBaseFieldTests(NumberField, { defaultValue: 12, schemaType: "integer" });
fieldTestSuite({ expect, test }, NumberField, { defaultValue: 12, schemaType: "integer" });
});

View File

@@ -1,6 +1,6 @@
import { describe, expect, test } from "bun:test";
import { TextField } from "../../../../src/data";
import { runBaseFieldTests, transformPersist } from "./inc";
import { fieldTestSuite, transformPersist } from "data/fields/field-test-suite";
describe("[data] TextField", async () => {
test("transformPersist (config)", async () => {
@@ -11,5 +11,5 @@ describe("[data] TextField", async () => {
expect(transformPersist(field, "abc")).resolves.toBe("abc");
});
runBaseFieldTests(TextField, { defaultValue: "abc", schemaType: "text" });
fieldTestSuite({ expect, test }, TextField, { defaultValue: "abc", schemaType: "text" });
});

View File

@@ -1,165 +0,0 @@
import { expect, test } from "bun:test";
import type { ColumnDataType } from "kysely";
import { omit } from "lodash-es";
import type { BaseFieldConfig, Field, TActionContext } from "../../../../src/data";
type ConstructableField = new (name: string, config?: Partial<BaseFieldConfig>) => Field;
type FieldTestConfig = {
defaultValue: any;
sampleValues?: any[];
schemaType: ColumnDataType;
};
export function transformPersist(field: Field, value: any, context?: TActionContext) {
return field.transformPersist(value, undefined as any, context as any);
}
export function runBaseFieldTests(
fieldClass: ConstructableField,
config: FieldTestConfig,
_requiredConfig: any = {},
) {
const noConfigField = new fieldClass("no_config", _requiredConfig);
const fillable = new fieldClass("fillable", { ..._requiredConfig, fillable: true });
const required = new fieldClass("required", { ..._requiredConfig, required: true });
const hidden = new fieldClass("hidden", { ..._requiredConfig, hidden: true });
const dflt = new fieldClass("dflt", { ..._requiredConfig, default_value: config.defaultValue });
const requiredAndDefault = new fieldClass("full", {
..._requiredConfig,
fillable: true,
required: true,
default_value: config.defaultValue,
});
test("schema", () => {
expect(noConfigField.name).toBe("no_config");
const { type, name, nullable, dflt } = noConfigField.schema()!;
expect({ type, name, nullable, dflt }).toEqual({
type: config.schemaType as any,
name: "no_config",
nullable: true, // always true
dflt: undefined, // never using default value
});
});
test("hasDefault", async () => {
expect(noConfigField.hasDefault()).toBe(false);
expect(noConfigField.getDefault()).toBeUndefined();
expect(dflt.hasDefault()).toBe(true);
expect(dflt.getDefault()).toBe(config.defaultValue);
});
test("isFillable", async () => {
expect(noConfigField.isFillable()).toBe(true);
expect(fillable.isFillable()).toBe(true);
expect(hidden.isFillable()).toBe(true);
expect(required.isFillable()).toBe(true);
});
test("isHidden", async () => {
expect(noConfigField.isHidden()).toBe(false);
expect(hidden.isHidden()).toBe(true);
expect(fillable.isHidden()).toBe(false);
expect(required.isHidden()).toBe(false);
});
test("isRequired", async () => {
expect(noConfigField.isRequired()).toBe(false);
expect(required.isRequired()).toBe(true);
expect(hidden.isRequired()).toBe(false);
expect(fillable.isRequired()).toBe(false);
});
test.if(Array.isArray(config.sampleValues))("getValue (RenderContext)", async () => {
const isPrimitive = (v) => ["string", "number"].includes(typeof v);
for (const value of config.sampleValues!) {
// "form"
expect(isPrimitive(noConfigField.getValue(value, "form"))).toBeTrue();
// "table"
expect(isPrimitive(noConfigField.getValue(value, "table"))).toBeTrue();
// "read"
// "submit"
}
});
test("transformPersist", async () => {
const persist = await transformPersist(noConfigField, config.defaultValue);
expect(config.defaultValue).toEqual(noConfigField.transformRetrieve(config.defaultValue));
expect(transformPersist(noConfigField, null)).resolves.toBeUndefined();
expect(transformPersist(noConfigField, undefined)).resolves.toBeUndefined();
expect(transformPersist(requiredAndDefault, null)).resolves.toBe(persist);
expect(transformPersist(dflt, null)).resolves.toBe(persist);
});
test("toJSON", async () => {
const _config = {
..._requiredConfig,
//order: 1,
fillable: true,
required: false,
hidden: false,
//virtual: false,
//default_value: undefined
};
function fieldJson(field: Field) {
const json = field.toJSON();
return {
...json,
config: omit(json.config, ["html"]),
};
}
expect(fieldJson(noConfigField)).toEqual({
//name: "no_config",
type: noConfigField.type,
config: _config,
});
expect(fieldJson(fillable)).toEqual({
//name: "fillable",
type: noConfigField.type,
config: _config,
});
expect(fieldJson(required)).toEqual({
//name: "required",
type: required.type,
config: {
..._config,
required: true,
},
});
expect(fieldJson(hidden)).toEqual({
//name: "hidden",
type: required.type,
config: {
..._config,
hidden: true,
},
});
expect(fieldJson(dflt)).toEqual({
//name: "dflt",
type: dflt.type,
config: {
..._config,
default_value: config.defaultValue,
},
});
expect(fieldJson(requiredAndDefault)).toEqual({
//name: "full",
type: requiredAndDefault.type,
config: {
..._config,
fillable: true,
required: true,
default_value: config.defaultValue,
},
});
});
}