mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 04:46:05 +00:00
public commit
This commit is contained in:
29
app/__test__/data/specs/fields/BooleanField.spec.ts
Normal file
29
app/__test__/data/specs/fields/BooleanField.spec.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { BooleanField } from "../../../../src/data";
|
||||
import { runBaseFieldTests, transformPersist } from "./inc";
|
||||
|
||||
describe("[data] BooleanField", async () => {
|
||||
runBaseFieldTests(BooleanField, { defaultValue: true, schemaType: "boolean" });
|
||||
|
||||
test("transformRetrieve", async () => {
|
||||
const field = new BooleanField("test");
|
||||
expect(field.transformRetrieve(1)).toBe(true);
|
||||
expect(field.transformRetrieve(0)).toBe(false);
|
||||
expect(field.transformRetrieve("1")).toBe(true);
|
||||
expect(field.transformRetrieve("0")).toBe(false);
|
||||
expect(field.transformRetrieve(true)).toBe(true);
|
||||
expect(field.transformRetrieve(false)).toBe(false);
|
||||
expect(field.transformRetrieve(null)).toBe(null);
|
||||
expect(field.transformRetrieve(undefined)).toBe(null);
|
||||
});
|
||||
|
||||
test("transformPersist (specific)", async () => {
|
||||
const field = new BooleanField("test");
|
||||
expect(transformPersist(field, 1)).resolves.toBe(true);
|
||||
expect(transformPersist(field, 0)).resolves.toBe(false);
|
||||
expect(transformPersist(field, "1")).rejects.toThrow();
|
||||
expect(transformPersist(field, "0")).rejects.toThrow();
|
||||
expect(transformPersist(field, true)).resolves.toBe(true);
|
||||
expect(transformPersist(field, false)).resolves.toBe(false);
|
||||
});
|
||||
});
|
||||
13
app/__test__/data/specs/fields/DateField.spec.ts
Normal file
13
app/__test__/data/specs/fields/DateField.spec.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { DateField } from "../../../../src/data";
|
||||
import { runBaseFieldTests } from "./inc";
|
||||
|
||||
describe("[data] DateField", async () => {
|
||||
runBaseFieldTests(DateField, { defaultValue: new Date(), schemaType: "date" });
|
||||
|
||||
// @todo: add datefield tests
|
||||
test("week", async () => {
|
||||
const field = new DateField("test", { type: "week" });
|
||||
console.log(field.getValue("2021-W01", "submit"));
|
||||
});
|
||||
});
|
||||
44
app/__test__/data/specs/fields/EnumField.spec.ts
Normal file
44
app/__test__/data/specs/fields/EnumField.spec.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { EnumField } from "../../../../src/data";
|
||||
import { runBaseFieldTests, transformPersist } from "./inc";
|
||||
|
||||
function options(strings: string[]) {
|
||||
return { type: "strings", values: strings };
|
||||
}
|
||||
|
||||
describe("[data] EnumField", async () => {
|
||||
runBaseFieldTests(
|
||||
EnumField,
|
||||
{ defaultValue: "a", schemaType: "text" },
|
||||
{ options: options(["a", "b", "c"]) }
|
||||
);
|
||||
|
||||
test("yields if no options", async () => {
|
||||
expect(() => new EnumField("test", { options: options([]) })).toThrow();
|
||||
});
|
||||
|
||||
test("yields if default value is not a valid option", async () => {
|
||||
expect(
|
||||
() => new EnumField("test", { options: options(["a", "b"]), default_value: "c" })
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
test("transformPersist (config)", async () => {
|
||||
const field = new EnumField("test", { options: options(["a", "b", "c"]) });
|
||||
|
||||
expect(transformPersist(field, null)).resolves.toBeUndefined();
|
||||
expect(transformPersist(field, "a")).resolves.toBe("a");
|
||||
expect(transformPersist(field, "d")).rejects.toThrow();
|
||||
});
|
||||
|
||||
test("transformRetrieve", async () => {
|
||||
const field = new EnumField("test", {
|
||||
options: options(["a", "b", "c"]),
|
||||
default_value: "a",
|
||||
required: true
|
||||
});
|
||||
|
||||
expect(field.transformRetrieve(null)).toBe("a");
|
||||
expect(field.transformRetrieve("d")).toBe("a");
|
||||
});
|
||||
});
|
||||
45
app/__test__/data/specs/fields/Field.spec.ts
Normal file
45
app/__test__/data/specs/fields/Field.spec.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { Default, parse, stripMark } from "../../../../src/core/utils";
|
||||
import { Field, type SchemaResponse, TextField, baseFieldConfigSchema } from "../../../../src/data";
|
||||
import { runBaseFieldTests, transformPersist } from "./inc";
|
||||
|
||||
describe("[data] Field", async () => {
|
||||
class FieldSpec extends Field {
|
||||
schema(): SchemaResponse {
|
||||
return this.useSchemaHelper("text");
|
||||
}
|
||||
getSchema() {
|
||||
return baseFieldConfigSchema;
|
||||
}
|
||||
}
|
||||
|
||||
runBaseFieldTests(FieldSpec, { defaultValue: "test", schemaType: "text" });
|
||||
|
||||
test.only("default config", async () => {
|
||||
const field = new FieldSpec("test");
|
||||
const config = Default(baseFieldConfigSchema, {});
|
||||
expect(stripMark(new FieldSpec("test").config)).toEqual(config);
|
||||
console.log("config", new TextField("test", { required: true }).toJSON());
|
||||
});
|
||||
|
||||
test("transformPersist (specific)", async () => {
|
||||
const required = new FieldSpec("test", { required: true });
|
||||
const requiredDefault = new FieldSpec("test", {
|
||||
required: true,
|
||||
default_value: "test"
|
||||
});
|
||||
|
||||
expect(required.transformPersist(null, undefined as any, undefined as any)).rejects.toThrow();
|
||||
expect(
|
||||
required.transformPersist(undefined, undefined as any, undefined as any)
|
||||
).rejects.toThrow();
|
||||
|
||||
// works because it has a default value
|
||||
expect(
|
||||
requiredDefault.transformPersist(null, undefined as any, undefined as any)
|
||||
).resolves.toBeDefined();
|
||||
expect(
|
||||
requiredDefault.transformPersist(undefined, undefined as any, undefined as any)
|
||||
).resolves.toBeDefined();
|
||||
});
|
||||
});
|
||||
38
app/__test__/data/specs/fields/FieldIndex.spec.ts
Normal file
38
app/__test__/data/specs/fields/FieldIndex.spec.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { Type } from "../../../../src/core/utils";
|
||||
import {
|
||||
Entity,
|
||||
EntityIndex,
|
||||
type EntityManager,
|
||||
Field,
|
||||
type SchemaResponse
|
||||
} from "../../../../src/data";
|
||||
|
||||
class TestField extends Field {
|
||||
protected getSchema(): any {
|
||||
return Type.Any();
|
||||
}
|
||||
|
||||
schema(em: EntityManager<any>): SchemaResponse {
|
||||
return undefined as any;
|
||||
}
|
||||
}
|
||||
|
||||
describe("FieldIndex", async () => {
|
||||
const entity = new Entity("test", []);
|
||||
test("it constructs", async () => {
|
||||
const field = new TestField("name");
|
||||
const index = new EntityIndex(entity, [field]);
|
||||
|
||||
expect(index.fields).toEqual([field]);
|
||||
expect(index.name).toEqual("idx_test_name");
|
||||
expect(index.unique).toEqual(false);
|
||||
});
|
||||
|
||||
test("it fails on non-unique", async () => {
|
||||
const field = new TestField("name", { required: false });
|
||||
|
||||
expect(() => new EntityIndex(entity, [field], true)).toThrowError();
|
||||
expect(() => new EntityIndex(entity, [field])).toBeDefined();
|
||||
});
|
||||
});
|
||||
47
app/__test__/data/specs/fields/JsonField.spec.ts
Normal file
47
app/__test__/data/specs/fields/JsonField.spec.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { JsonField } from "../../../../src/data";
|
||||
import { runBaseFieldTests, transformPersist } from "./inc";
|
||||
|
||||
describe("[data] JsonField", async () => {
|
||||
const field = new JsonField("test");
|
||||
runBaseFieldTests(JsonField, {
|
||||
defaultValue: { a: 1 },
|
||||
sampleValues: ["string", { test: 1 }, 1],
|
||||
schemaType: "text"
|
||||
});
|
||||
|
||||
test("transformPersist (no config)", async () => {
|
||||
expect(transformPersist(field, Function)).rejects.toThrow();
|
||||
expect(transformPersist(field, undefined)).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
test("isSerializable", async () => {
|
||||
expect(field.isSerializable(1)).toBe(true);
|
||||
expect(field.isSerializable("test")).toBe(true);
|
||||
expect(field.isSerializable({ test: 1 })).toBe(true);
|
||||
expect(field.isSerializable({ test: [1, 2] })).toBe(true);
|
||||
expect(field.isSerializable(Function)).toBe(false);
|
||||
expect(field.isSerializable(undefined)).toBe(false);
|
||||
});
|
||||
|
||||
test("isSerialized", async () => {
|
||||
expect(field.isSerialized(1)).toBe(false);
|
||||
expect(field.isSerialized({ test: 1 })).toBe(false);
|
||||
expect(field.isSerialized('{"test":1}')).toBe(true);
|
||||
expect(field.isSerialized("1")).toBe(true);
|
||||
});
|
||||
|
||||
test("getValue", async () => {
|
||||
expect(field.getValue({ test: 1 }, "form")).toBe('{"test":1}');
|
||||
expect(field.getValue("string", "form")).toBe('"string"');
|
||||
expect(field.getValue(1, "form")).toBe("1");
|
||||
|
||||
expect(field.getValue('{"test":1}', "submit")).toEqual({ test: 1 });
|
||||
expect(field.getValue('"string"', "submit")).toBe("string");
|
||||
expect(field.getValue("1", "submit")).toBe(1);
|
||||
|
||||
expect(field.getValue({ test: 1 }, "table")).toBe('{"test":1}');
|
||||
expect(field.getValue("string", "table")).toBe('"string"');
|
||||
expect(field.getValue(1, "form")).toBe("1");
|
||||
});
|
||||
});
|
||||
9
app/__test__/data/specs/fields/JsonSchemaField.spec.ts
Normal file
9
app/__test__/data/specs/fields/JsonSchemaField.spec.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { JsonSchemaField } from "../../../../src/data";
|
||||
import { runBaseFieldTests } from "./inc";
|
||||
|
||||
describe("[data] JsonSchemaField", async () => {
|
||||
runBaseFieldTests(JsonSchemaField, { defaultValue: {}, schemaType: "text" });
|
||||
|
||||
// @todo: add JsonSchemaField tests
|
||||
});
|
||||
19
app/__test__/data/specs/fields/NumberField.spec.ts
Normal file
19
app/__test__/data/specs/fields/NumberField.spec.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { NumberField } from "../../../../src/data";
|
||||
import { runBaseFieldTests, transformPersist } from "./inc";
|
||||
|
||||
describe("[data] NumberField", async () => {
|
||||
test("transformPersist (config)", async () => {
|
||||
const field = new NumberField("test", { minimum: 3, maximum: 5 });
|
||||
|
||||
expect(transformPersist(field, 2)).rejects.toThrow();
|
||||
expect(transformPersist(field, 6)).rejects.toThrow();
|
||||
expect(transformPersist(field, 4)).resolves.toBe(4);
|
||||
|
||||
const field2 = new NumberField("test");
|
||||
expect(transformPersist(field2, 0)).resolves.toBe(0);
|
||||
expect(transformPersist(field2, 10000)).resolves.toBe(10000);
|
||||
});
|
||||
|
||||
runBaseFieldTests(NumberField, { defaultValue: 12, schemaType: "integer" });
|
||||
});
|
||||
37
app/__test__/data/specs/fields/PrimaryField.spec.ts
Normal file
37
app/__test__/data/specs/fields/PrimaryField.spec.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { PrimaryField } from "../../../../src/data";
|
||||
|
||||
describe("[data] PrimaryField", async () => {
|
||||
const field = new PrimaryField("primary");
|
||||
|
||||
test("name", async () => {
|
||||
expect(field.name).toBe("primary");
|
||||
});
|
||||
|
||||
test("schema", () => {
|
||||
expect(field.name).toBe("primary");
|
||||
expect(field.schema()).toEqual(["primary", "integer", expect.any(Function)]);
|
||||
});
|
||||
|
||||
test("hasDefault", async () => {
|
||||
expect(field.hasDefault()).toBe(false);
|
||||
expect(field.getDefault()).toBe(undefined);
|
||||
});
|
||||
|
||||
test("isFillable", async () => {
|
||||
expect(field.isFillable()).toBe(false);
|
||||
});
|
||||
|
||||
test("isHidden", async () => {
|
||||
expect(field.isHidden()).toBe(false);
|
||||
});
|
||||
|
||||
test("isRequired", async () => {
|
||||
expect(field.isRequired()).toBe(false);
|
||||
});
|
||||
|
||||
test("transformPersist/Retrieve", async () => {
|
||||
expect(field.transformPersist(1)).rejects.toThrow();
|
||||
expect(field.transformRetrieve(1)).toBe(1);
|
||||
});
|
||||
});
|
||||
15
app/__test__/data/specs/fields/TextField.spec.ts
Normal file
15
app/__test__/data/specs/fields/TextField.spec.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { TextField } from "../../../../src/data";
|
||||
import { runBaseFieldTests, transformPersist } from "./inc";
|
||||
|
||||
describe("[data] TextField", async () => {
|
||||
test("transformPersist (config)", async () => {
|
||||
const field = new TextField("test", { minLength: 3, maxLength: 5 });
|
||||
|
||||
expect(transformPersist(field, "a")).rejects.toThrow();
|
||||
expect(transformPersist(field, "abcdefghijklmn")).rejects.toThrow();
|
||||
expect(transformPersist(field, "abc")).resolves.toBe("abc");
|
||||
});
|
||||
|
||||
runBaseFieldTests(TextField, { defaultValue: "abc", schemaType: "text" });
|
||||
});
|
||||
162
app/__test__/data/specs/fields/inc.ts
Normal file
162
app/__test__/data/specs/fields/inc.ts
Normal file
@@ -0,0 +1,162 @@
|
||||
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");
|
||||
expect(noConfigField.schema(null as any)).toEqual([
|
||||
"no_config",
|
||||
config.schemaType,
|
||||
expect.any(Function)
|
||||
]);
|
||||
});
|
||||
|
||||
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
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user