mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 04:46:05 +00:00
public commit
This commit is contained in:
60
app/__test__/data/specs/Entity.spec.ts
Normal file
60
app/__test__/data/specs/Entity.spec.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { Entity, NumberField, TextField } from "../../../src/data";
|
||||
|
||||
describe("[data] Entity", async () => {
|
||||
const entity = new Entity("test", [
|
||||
new TextField("name", { required: true }),
|
||||
new TextField("description"),
|
||||
new NumberField("age", { fillable: false, default_value: 18 }),
|
||||
new TextField("hidden", { hidden: true, default_value: "secret" })
|
||||
]);
|
||||
|
||||
test("getSelect", async () => {
|
||||
expect(entity.getSelect()).toEqual(["id", "name", "description", "age"]);
|
||||
});
|
||||
|
||||
test("getFillableFields", async () => {
|
||||
expect(entity.getFillableFields().map((f) => f.name)).toEqual([
|
||||
"name",
|
||||
"description",
|
||||
"hidden"
|
||||
]);
|
||||
});
|
||||
|
||||
test("getRequiredFields", async () => {
|
||||
expect(entity.getRequiredFields().map((f) => f.name)).toEqual(["name"]);
|
||||
});
|
||||
|
||||
test("getDefaultObject", async () => {
|
||||
expect(entity.getDefaultObject()).toEqual({
|
||||
age: 18,
|
||||
hidden: "secret"
|
||||
});
|
||||
});
|
||||
|
||||
test("getField", async () => {
|
||||
expect(entity.getField("name")).toBeInstanceOf(TextField);
|
||||
expect(entity.getField("age")).toBeInstanceOf(NumberField);
|
||||
});
|
||||
|
||||
test("getPrimaryField", async () => {
|
||||
expect(entity.getPrimaryField().name).toEqual("id");
|
||||
});
|
||||
|
||||
test("addField", async () => {
|
||||
const field = new TextField("new_field");
|
||||
entity.addField(field);
|
||||
expect(entity.getField("new_field")).toBe(field);
|
||||
});
|
||||
|
||||
// @todo: move this to ClientApp
|
||||
/*test("serialize and deserialize", async () => {
|
||||
const json = entity.toJSON();
|
||||
//sconsole.log("json", json.fields);
|
||||
const newEntity = Entity.deserialize(json);
|
||||
//console.log("newEntity", newEntity.toJSON().fields);
|
||||
expect(newEntity).toBeInstanceOf(Entity);
|
||||
expect(json).toEqual(newEntity.toJSON());
|
||||
expect(json.fields).toEqual(newEntity.toJSON().fields);
|
||||
});*/
|
||||
});
|
||||
106
app/__test__/data/specs/EntityManager.spec.ts
Normal file
106
app/__test__/data/specs/EntityManager.spec.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { afterAll, describe, expect, test } from "bun:test";
|
||||
import {
|
||||
Entity,
|
||||
EntityManager,
|
||||
ManyToManyRelation,
|
||||
ManyToOneRelation,
|
||||
SchemaManager
|
||||
} from "../../../src/data";
|
||||
import { UnableToConnectException } from "../../../src/data/errors";
|
||||
import { getDummyConnection } from "../helper";
|
||||
|
||||
const { dummyConnection, afterAllCleanup } = getDummyConnection();
|
||||
afterAll(afterAllCleanup);
|
||||
|
||||
describe("[data] EntityManager", async () => {
|
||||
test("base empty throw", async () => {
|
||||
// @ts-expect-error - testing invalid input, connection is required
|
||||
expect(() => new EntityManager([], {})).toThrow(UnableToConnectException);
|
||||
});
|
||||
|
||||
test("base w/o entities & relations", async () => {
|
||||
const em = new EntityManager([], dummyConnection);
|
||||
expect(em.entities).toEqual([]);
|
||||
expect(em.relations.all).toEqual([]);
|
||||
expect(await em.ping()).toBe(true);
|
||||
expect(() => em.entity("...")).toThrow();
|
||||
expect(() =>
|
||||
em.addRelation(new ManyToOneRelation(new Entity("1"), new Entity("2")))
|
||||
).toThrow();
|
||||
expect(em.schema()).toBeInstanceOf(SchemaManager);
|
||||
|
||||
// the rest will all throw, since they depend on em.entity()
|
||||
});
|
||||
|
||||
test("w/ 2 entities but no initial relations", async () => {
|
||||
const users = new Entity("users");
|
||||
const posts = new Entity("posts");
|
||||
|
||||
const em = new EntityManager([users, posts], dummyConnection);
|
||||
expect(em.entities).toEqual([users, posts]);
|
||||
expect(em.relations.all).toEqual([]);
|
||||
|
||||
expect(em.entity("users")).toBe(users);
|
||||
expect(em.entity("posts")).toBe(posts);
|
||||
|
||||
// expect adding relation to pass
|
||||
em.addRelation(new ManyToOneRelation(posts, users));
|
||||
expect(em.relations.all.length).toBe(1);
|
||||
expect(em.relations.all[0]).toBeInstanceOf(ManyToOneRelation);
|
||||
expect(em.relationsOf("users")).toEqual([em.relations.all[0]]);
|
||||
expect(em.relationsOf("posts")).toEqual([em.relations.all[0]]);
|
||||
expect(em.hasRelations("users")).toBe(true);
|
||||
expect(em.hasRelations("posts")).toBe(true);
|
||||
expect(em.relatedEntitiesOf("users")).toEqual([posts]);
|
||||
expect(em.relatedEntitiesOf("posts")).toEqual([users]);
|
||||
expect(em.relationReferencesOf("users")).toEqual(["posts"]);
|
||||
expect(em.relationReferencesOf("posts")).toEqual(["users"]);
|
||||
});
|
||||
|
||||
test("test target relations", async () => {
|
||||
const users = new Entity("users");
|
||||
const posts = new Entity("posts");
|
||||
const comments = new Entity("comments");
|
||||
const categories = new Entity("categories");
|
||||
|
||||
const em = new EntityManager([users, posts, comments, categories], dummyConnection);
|
||||
em.addRelation(new ManyToOneRelation(posts, users));
|
||||
em.addRelation(new ManyToOneRelation(comments, users));
|
||||
em.addRelation(new ManyToOneRelation(comments, posts));
|
||||
em.addRelation(new ManyToManyRelation(posts, categories));
|
||||
|
||||
const userTargetRel = em.relations.targetRelationsOf(users);
|
||||
const postTargetRel = em.relations.targetRelationsOf(posts);
|
||||
const commentTargetRel = em.relations.targetRelationsOf(comments);
|
||||
|
||||
expect(userTargetRel.map((r) => r.source.entity.name)).toEqual(["posts", "comments"]);
|
||||
expect(postTargetRel.map((r) => r.source.entity.name)).toEqual(["comments"]);
|
||||
expect(commentTargetRel.map((r) => r.source.entity.name)).toEqual([]);
|
||||
});
|
||||
|
||||
test("test listable relations", async () => {
|
||||
const users = new Entity("users");
|
||||
const posts = new Entity("posts");
|
||||
const comments = new Entity("comments");
|
||||
const categories = new Entity("categories");
|
||||
|
||||
const em = new EntityManager([users, posts, comments, categories], dummyConnection);
|
||||
em.addRelation(new ManyToOneRelation(posts, users));
|
||||
em.addRelation(new ManyToOneRelation(comments, users));
|
||||
em.addRelation(new ManyToOneRelation(comments, posts));
|
||||
em.addRelation(new ManyToManyRelation(posts, categories));
|
||||
|
||||
const userTargetRel = em.relations.listableRelationsOf(users);
|
||||
const postTargetRel = em.relations.listableRelationsOf(posts);
|
||||
const commentTargetRel = em.relations.listableRelationsOf(comments);
|
||||
const categoriesTargetRel = em.relations.listableRelationsOf(categories);
|
||||
|
||||
expect(userTargetRel.map((r) => r.other(users).entity.name)).toEqual(["posts", "comments"]);
|
||||
expect(postTargetRel.map((r) => r.other(posts).entity.name)).toEqual([
|
||||
"comments",
|
||||
"categories"
|
||||
]);
|
||||
expect(commentTargetRel.map((r) => r.other(comments).entity.name)).toEqual([]);
|
||||
expect(categoriesTargetRel.map((r) => r.other(categories).entity.name)).toEqual(["posts"]);
|
||||
});
|
||||
});
|
||||
43
app/__test__/data/specs/JoinBuilder.spec.ts
Normal file
43
app/__test__/data/specs/JoinBuilder.spec.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { afterAll, describe, expect, test } from "bun:test";
|
||||
import { Entity, EntityManager, ManyToOneRelation, TextField } from "../../../src/data";
|
||||
import { JoinBuilder } from "../../../src/data/entities/query/JoinBuilder";
|
||||
import { getDummyConnection } from "../helper";
|
||||
|
||||
const { dummyConnection, afterAllCleanup } = getDummyConnection();
|
||||
afterAll(afterAllCleanup);
|
||||
|
||||
describe("[data] JoinBuilder", async () => {
|
||||
test("missing relation", async () => {
|
||||
const users = new Entity("users", [new TextField("username")]);
|
||||
const em = new EntityManager([users], dummyConnection);
|
||||
|
||||
expect(() =>
|
||||
JoinBuilder.addClause(em, em.connection.kysely.selectFrom("users"), users, ["posts"])
|
||||
).toThrow('Relation "posts" not found');
|
||||
});
|
||||
|
||||
test("addClause: ManyToOne", async () => {
|
||||
const users = new Entity("users", [new TextField("username")]);
|
||||
const posts = new Entity("posts", [new TextField("content")]);
|
||||
const relations = [new ManyToOneRelation(posts, users, { mappedBy: "author" })];
|
||||
const em = new EntityManager([users, posts], dummyConnection, relations);
|
||||
|
||||
const qb = JoinBuilder.addClause(em, em.connection.kysely.selectFrom("users"), users, [
|
||||
"posts"
|
||||
]);
|
||||
|
||||
const res = qb.compile();
|
||||
console.log("compiled", res.sql);
|
||||
|
||||
/*expect(res.sql).toBe(
|
||||
'select from "users" inner join "posts" on "posts"."author_id" = "users"."id" group by "users"."id"',
|
||||
);*/
|
||||
|
||||
const qb2 = JoinBuilder.addClause(em, em.connection.kysely.selectFrom("posts"), posts, [
|
||||
"author"
|
||||
]);
|
||||
|
||||
const res2 = qb2.compile();
|
||||
console.log("compiled2", res2.sql);
|
||||
});
|
||||
});
|
||||
302
app/__test__/data/specs/Mutator.spec.ts
Normal file
302
app/__test__/data/specs/Mutator.spec.ts
Normal file
@@ -0,0 +1,302 @@
|
||||
import { afterAll, describe, expect, test } from "bun:test";
|
||||
import {
|
||||
Entity,
|
||||
EntityManager,
|
||||
ManyToOneRelation,
|
||||
MutatorEvents,
|
||||
NumberField,
|
||||
OneToOneRelation,
|
||||
type RelationField,
|
||||
RelationMutator,
|
||||
TextField
|
||||
} from "../../../src/data";
|
||||
import { getDummyConnection } from "../helper";
|
||||
|
||||
const { dummyConnection, afterAllCleanup } = getDummyConnection();
|
||||
afterAll(afterAllCleanup);
|
||||
|
||||
describe("[data] Mutator (base)", async () => {
|
||||
const entity = new Entity("items", [
|
||||
new TextField("label", { required: true }),
|
||||
new NumberField("count"),
|
||||
new TextField("hidden", { hidden: true }),
|
||||
new TextField("not_fillable", { fillable: false })
|
||||
]);
|
||||
const em = new EntityManager([entity], dummyConnection);
|
||||
await em.schema().sync({ force: true });
|
||||
|
||||
const payload = { label: "item 1", count: 1 };
|
||||
|
||||
test("insertOne", async () => {
|
||||
expect(em.mutator(entity).getValidatedData(payload, "create")).resolves.toEqual(payload);
|
||||
const res = await em.mutator(entity).insertOne(payload);
|
||||
|
||||
// checking params, because we can't know the id
|
||||
// if it wouldn't be successful, it would throw an error
|
||||
expect(res.parameters).toEqual(Object.values(payload));
|
||||
|
||||
// but expect additional fields to be present
|
||||
expect((res.data as any).not_fillable).toBeDefined();
|
||||
});
|
||||
|
||||
test("updateOne", async () => {
|
||||
const { data } = await em.mutator(entity).insertOne(payload);
|
||||
const updated = await em.mutator(entity).updateOne(data.id, {
|
||||
count: 2
|
||||
});
|
||||
|
||||
expect(updated.parameters).toEqual([2, data.id]);
|
||||
expect(updated.data.count).toBe(2);
|
||||
});
|
||||
|
||||
test("deleteOne", async () => {
|
||||
const { data } = await em.mutator(entity).insertOne(payload);
|
||||
const deleted = await em.mutator(entity).deleteOne(data.id);
|
||||
|
||||
expect(deleted.parameters).toEqual([data.id]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("[data] Mutator (ManyToOne)", async () => {
|
||||
const posts = new Entity("posts", [new TextField("title")]);
|
||||
const users = new Entity("users", [new TextField("username")]);
|
||||
const relations = [new ManyToOneRelation(posts, users)];
|
||||
const em = new EntityManager([posts, users], dummyConnection, relations);
|
||||
await em.schema().sync({ force: true });
|
||||
|
||||
test("RelationMutator", async () => {
|
||||
// create entries
|
||||
const userData = await em.mutator(users).insertOne({ username: "user1" });
|
||||
const postData = await em.mutator(posts).insertOne({ title: "post1" });
|
||||
|
||||
const postRelMutator = new RelationMutator(posts, em);
|
||||
const postRelField = posts.getField("users_id")! as RelationField;
|
||||
expect(postRelMutator.getRelationalKeys()).toEqual(["users", "users_id"]);
|
||||
|
||||
// persisting relational field should just return key value to be added
|
||||
expect(
|
||||
postRelMutator.persistRelationField(postRelField, "users_id", userData.data.id)
|
||||
).resolves.toEqual(["users_id", userData.data.id]);
|
||||
|
||||
// persisting invalid value should throw
|
||||
expect(postRelMutator.persistRelationField(postRelField, "users_id", 0)).rejects.toThrow();
|
||||
|
||||
// persisting reference should ...
|
||||
expect(
|
||||
postRelMutator.persistReference(relations[0], "users", {
|
||||
$set: { id: userData.data.id }
|
||||
})
|
||||
).resolves.toEqual(["users_id", userData.data.id]);
|
||||
// @todo: add what methods are allowed to relation, like $create should not be allowed for post<>users
|
||||
|
||||
process.exit(0);
|
||||
|
||||
const userRelMutator = new RelationMutator(users, em);
|
||||
expect(userRelMutator.getRelationalKeys()).toEqual(["posts"]);
|
||||
});
|
||||
|
||||
test("insertOne: missing ref", async () => {
|
||||
expect(
|
||||
em.mutator(posts).insertOne({
|
||||
title: "post1",
|
||||
users_id: 1 // user does not exist yet
|
||||
})
|
||||
).rejects.toThrow();
|
||||
});
|
||||
|
||||
test("insertOne: missing required relation", async () => {
|
||||
const items = new Entity("items", [new TextField("label")]);
|
||||
const cats = new Entity("cats");
|
||||
const relations = [new ManyToOneRelation(items, cats, { required: true })];
|
||||
const em = new EntityManager([items, cats], dummyConnection, relations);
|
||||
|
||||
expect(em.mutator(items).insertOne({ label: "test" })).rejects.toThrow(
|
||||
'Field "cats_id" is required'
|
||||
);
|
||||
});
|
||||
|
||||
test("insertOne: using field name", async () => {
|
||||
const { data } = await em.mutator(users).insertOne({ username: "user1" });
|
||||
const res = await em.mutator(posts).insertOne({
|
||||
title: "post1",
|
||||
users_id: data.id
|
||||
});
|
||||
expect(res.data.users_id).toBe(data.id);
|
||||
|
||||
// setting "null" should be allowed
|
||||
const res2 = await em.mutator(posts).insertOne({
|
||||
title: "post1",
|
||||
users_id: null
|
||||
});
|
||||
expect(res2.data.users_id).toBe(null);
|
||||
});
|
||||
|
||||
test("insertOne: using reference", async () => {
|
||||
const { data } = await em.mutator(users).insertOne({ username: "user1" });
|
||||
const res = await em.mutator(posts).insertOne({
|
||||
title: "post1",
|
||||
users: { $set: { id: data.id } }
|
||||
});
|
||||
expect(res.data.users_id).toBe(data.id);
|
||||
|
||||
// setting "null" should be allowed
|
||||
const res2 = await em.mutator(posts).insertOne({
|
||||
title: "post1",
|
||||
users: { $set: { id: null } }
|
||||
});
|
||||
expect(res2.data.users_id).toBe(null);
|
||||
});
|
||||
|
||||
test("insertOne: performing unsupported operations", async () => {
|
||||
expect(
|
||||
em.mutator(posts).insertOne({
|
||||
title: "test",
|
||||
users: { $create: { username: "test" } }
|
||||
})
|
||||
).rejects.toThrow();
|
||||
});
|
||||
|
||||
test("updateOne", async () => {
|
||||
const res1 = await em.mutator(users).insertOne({ username: "user1" });
|
||||
const res1_1 = await em.mutator(users).insertOne({ username: "user1" });
|
||||
const res2 = await em.mutator(posts).insertOne({ title: "post1" });
|
||||
|
||||
const up1 = await em.mutator(posts).updateOne(res2.data.id, {
|
||||
users: { $set: { id: res1.data.id } }
|
||||
});
|
||||
expect(up1.data.users_id).toBe(res1.data.id);
|
||||
|
||||
const up2 = await em.mutator(posts).updateOne(res2.data.id, {
|
||||
users: { $set: { id: res1_1.data.id } }
|
||||
});
|
||||
expect(up2.data.users_id).toBe(res1_1.data.id);
|
||||
|
||||
const up3_1 = await em.mutator(posts).updateOne(res2.data.id, {
|
||||
users_id: res1.data.id
|
||||
});
|
||||
expect(up3_1.data.users_id).toBe(res1.data.id);
|
||||
|
||||
const up3_2 = await em.mutator(posts).updateOne(res2.data.id, {
|
||||
users_id: res1_1.data.id
|
||||
});
|
||||
expect(up3_2.data.users_id).toBe(res1_1.data.id);
|
||||
|
||||
const up4 = await em.mutator(posts).updateOne(res2.data.id, {
|
||||
users_id: null
|
||||
});
|
||||
expect(up4.data.users_id).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("[data] Mutator (OneToOne)", async () => {
|
||||
const users = new Entity("users", [new TextField("username")]);
|
||||
const settings = new Entity("settings", [new TextField("theme")]);
|
||||
const relations = [new OneToOneRelation(users, settings)];
|
||||
const em = new EntityManager([users, settings], dummyConnection, relations);
|
||||
await em.schema().sync({ force: true });
|
||||
|
||||
test("insertOne: missing ref", async () => {
|
||||
expect(
|
||||
em.mutator(users).insertOne({
|
||||
username: "test",
|
||||
settings_id: 1 // todo: throws because it doesn't exist, but it shouldn't be allowed
|
||||
})
|
||||
).rejects.toThrow();
|
||||
});
|
||||
|
||||
test("insertOne: using reference", async () => {
|
||||
// $set is not allowed in OneToOne
|
||||
const { data } = await em.mutator(settings).insertOne({ theme: "dark" });
|
||||
expect(
|
||||
em.mutator(users).insertOne({
|
||||
username: "test",
|
||||
settings: { $set: { id: data.id } }
|
||||
})
|
||||
).rejects.toThrow();
|
||||
});
|
||||
|
||||
test("insertOne: using $create", async () => {
|
||||
const res = await em.mutator(users).insertOne({
|
||||
username: "test",
|
||||
settings: { $create: { theme: "dark" } }
|
||||
});
|
||||
expect(res.data.settings_id).toBeDefined();
|
||||
});
|
||||
});
|
||||
/*
|
||||
describe("[data] Mutator (ManyToMany)", async () => {
|
||||
const posts = new Entity("posts", [new TextField("title")]);
|
||||
const tags = new Entity("tags", [new TextField("name")]);
|
||||
const relations = [new ManyToOneRelation(posts, tags)];
|
||||
const em = new EntityManager([posts, tags], dummyConnection, relations);
|
||||
await em.schema().sync({ force: true });
|
||||
|
||||
test("insertOne: missing ref", async () => {
|
||||
expect(
|
||||
em.mutator(posts).insertOne({
|
||||
title: "post1",
|
||||
tags_id: 1, // tag does not exist yet
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
});
|
||||
|
||||
test("insertOne: using reference", async () => {
|
||||
const { data } = await em.mutator(tags).insertOne({ name: "tag1" });
|
||||
const res = await em.mutator(posts).insertOne({
|
||||
title: "post1",
|
||||
tags: { $attach: { id: data.id } },
|
||||
});
|
||||
expect(res.data.tags).toContain(data.id);
|
||||
});
|
||||
|
||||
test("insertOne: using $create", async () => {
|
||||
const res = await em.mutator(posts).insertOne({
|
||||
title: "post1",
|
||||
tags: { $create: { name: "tag1" } },
|
||||
});
|
||||
expect(res.data.tags).toBeDefined();
|
||||
});
|
||||
|
||||
test("insertOne: using $detach", async () => {
|
||||
const { data: tagData } = await em.mutator(tags).insertOne({ name: "tag1" });
|
||||
const { data: postData } = await em.mutator(posts).insertOne({ title: "post1" });
|
||||
|
||||
const res = await em.mutator(posts).insertOne({
|
||||
title: "post1",
|
||||
tags: { $attach: { id: tagData.id } },
|
||||
});
|
||||
expect(res.data.tags).toContain(tagData.id);
|
||||
|
||||
const res2 = await em.mutator(posts).updateOne(postData.id, {
|
||||
tags: { $detach: { id: tagData.id } },
|
||||
});
|
||||
expect(res2.data.tags).not.toContain(tagData.id);
|
||||
});
|
||||
});*/
|
||||
|
||||
describe("[data] Mutator (Events)", async () => {
|
||||
const entity = new Entity("test", [new TextField("label")]);
|
||||
const em = new EntityManager([entity], dummyConnection);
|
||||
await em.schema().sync({ force: true });
|
||||
const events = new Map<string, any>();
|
||||
|
||||
const mutator = em.mutator(entity);
|
||||
mutator.emgr.onAny((event) => {
|
||||
// @ts-ignore
|
||||
events.set(event.constructor.slug, event);
|
||||
});
|
||||
|
||||
test("events were fired", async () => {
|
||||
const { data } = await mutator.insertOne({ label: "test" });
|
||||
expect(events.has(MutatorEvents.MutatorInsertBefore.slug)).toBeTrue();
|
||||
expect(events.has(MutatorEvents.MutatorInsertAfter.slug)).toBeTrue();
|
||||
|
||||
await mutator.updateOne(data.id, { label: "test2" });
|
||||
expect(events.has(MutatorEvents.MutatorUpdateBefore.slug)).toBeTrue();
|
||||
expect(events.has(MutatorEvents.MutatorUpdateAfter.slug)).toBeTrue();
|
||||
|
||||
await mutator.deleteOne(data.id);
|
||||
expect(events.has(MutatorEvents.MutatorDeleteBefore.slug)).toBeTrue();
|
||||
expect(events.has(MutatorEvents.MutatorDeleteAfter.slug)).toBeTrue();
|
||||
});
|
||||
});
|
||||
222
app/__test__/data/specs/Repository.spec.ts
Normal file
222
app/__test__/data/specs/Repository.spec.ts
Normal file
@@ -0,0 +1,222 @@
|
||||
import { afterAll, describe, expect, test } from "bun:test";
|
||||
// @ts-ignore
|
||||
import { Perf } from "@bknd/core/utils";
|
||||
import type { Kysely, Transaction } from "kysely";
|
||||
import {
|
||||
Entity,
|
||||
EntityManager,
|
||||
LibsqlConnection,
|
||||
ManyToOneRelation,
|
||||
RepositoryEvents,
|
||||
TextField
|
||||
} from "../../../src/data";
|
||||
import { getDummyConnection } from "../helper";
|
||||
|
||||
type E = Kysely<any> | Transaction<any>;
|
||||
|
||||
const { dummyConnection, afterAllCleanup } = getDummyConnection();
|
||||
afterAll(afterAllCleanup);
|
||||
|
||||
async function sleep(ms: number) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, ms);
|
||||
});
|
||||
}
|
||||
|
||||
describe("[Repository]", async () => {
|
||||
test("bulk", async () => {
|
||||
//const connection = dummyConnection;
|
||||
//const connection = getLocalLibsqlConnection();
|
||||
const credentials = null as any; // @todo: determine what to do here
|
||||
const connection = new LibsqlConnection(credentials);
|
||||
|
||||
const em = new EntityManager([], connection);
|
||||
/*const emLibsql = new EntityManager([], {
|
||||
url: connection.url.replace("https", "libsql"),
|
||||
authToken: connection.authToken,
|
||||
});*/
|
||||
const table = "posts";
|
||||
|
||||
const client = connection.getClient();
|
||||
if (!client) {
|
||||
console.log("Cannot perform test without libsql connection");
|
||||
return;
|
||||
}
|
||||
|
||||
const conn = em.connection.kysely;
|
||||
const selectQ = (e: E) => e.selectFrom(table).selectAll().limit(2);
|
||||
const countQ = (e: E) => e.selectFrom(table).select(e.fn.count("*").as("count"));
|
||||
|
||||
async function executeTransaction(em: EntityManager<any>) {
|
||||
return await em.connection.kysely.transaction().execute(async (e) => {
|
||||
const res = await selectQ(e).execute();
|
||||
const count = await countQ(e).execute();
|
||||
|
||||
return [res, count];
|
||||
});
|
||||
}
|
||||
|
||||
async function executeBatch(em: EntityManager<any>) {
|
||||
const queries = [selectQ(conn), countQ(conn)];
|
||||
return await em.connection.batchQuery(queries);
|
||||
}
|
||||
|
||||
async function executeSingleKysely(em: EntityManager<any>) {
|
||||
const res = await selectQ(conn).execute();
|
||||
const count = await countQ(conn).execute();
|
||||
return [res, count];
|
||||
}
|
||||
|
||||
async function executeSingleClient(em: EntityManager<any>) {
|
||||
const q1 = selectQ(conn).compile();
|
||||
const res = await client.execute({
|
||||
sql: q1.sql,
|
||||
args: q1.parameters as any
|
||||
});
|
||||
|
||||
const q2 = countQ(conn).compile();
|
||||
const count = await client.execute({
|
||||
sql: q2.sql,
|
||||
args: q2.parameters as any
|
||||
});
|
||||
return [res, count];
|
||||
}
|
||||
|
||||
const transaction = await executeTransaction(em);
|
||||
const batch = await executeBatch(em);
|
||||
|
||||
expect(batch).toEqual(transaction as any);
|
||||
|
||||
const testperf = false;
|
||||
if (testperf) {
|
||||
const times = 5;
|
||||
|
||||
const exec = async (
|
||||
name: string,
|
||||
fn: (em: EntityManager<any>) => Promise<any>,
|
||||
em: EntityManager<any>
|
||||
) => {
|
||||
const res = await Perf.execute(() => fn(em), times);
|
||||
await sleep(1000);
|
||||
const info = {
|
||||
name,
|
||||
total: res.total.toFixed(2),
|
||||
avg: (res.total / times).toFixed(2),
|
||||
first: res.marks[0].time.toFixed(2),
|
||||
last: res.marks[res.marks.length - 1].time.toFixed(2)
|
||||
};
|
||||
console.log(info.name, info, res.marks);
|
||||
return info;
|
||||
};
|
||||
|
||||
const data: any[] = [];
|
||||
data.push(await exec("transaction.http", executeTransaction, em));
|
||||
data.push(await exec("bulk.http", executeBatch, em));
|
||||
data.push(await exec("singleKy.http", executeSingleKysely, em));
|
||||
data.push(await exec("singleCl.http", executeSingleClient, em));
|
||||
|
||||
/*data.push(await exec("transaction.libsql", executeTransaction, emLibsql));
|
||||
data.push(await exec("bulk.libsql", executeBatch, emLibsql));
|
||||
data.push(await exec("singleKy.libsql", executeSingleKysely, emLibsql));
|
||||
data.push(await exec("singleCl.libsql", executeSingleClient, emLibsql));*/
|
||||
|
||||
console.table(data);
|
||||
/**
|
||||
* ┌───┬────────────────────┬────────┬────────┬────────┬────────┐
|
||||
* │ │ name │ total │ avg │ first │ last │
|
||||
* ├───┼────────────────────┼────────┼────────┼────────┼────────┤
|
||||
* │ 0 │ transaction.http │ 681.29 │ 136.26 │ 136.46 │ 396.09 │
|
||||
* │ 1 │ bulk.http │ 164.82 │ 32.96 │ 32.95 │ 99.91 │
|
||||
* │ 2 │ singleKy.http │ 330.01 │ 66.00 │ 65.86 │ 195.41 │
|
||||
* │ 3 │ singleCl.http │ 326.17 │ 65.23 │ 61.32 │ 198.08 │
|
||||
* │ 4 │ transaction.libsql │ 856.79 │ 171.36 │ 132.31 │ 595.24 │
|
||||
* │ 5 │ bulk.libsql │ 180.63 │ 36.13 │ 35.39 │ 107.71 │
|
||||
* │ 6 │ singleKy.libsql │ 347.11 │ 69.42 │ 65.00 │ 207.14 │
|
||||
* │ 7 │ singleCl.libsql │ 328.60 │ 65.72 │ 62.19 │ 195.04 │
|
||||
* └───┴────────────────────┴────────┴────────┴────────┴────────┘
|
||||
*/
|
||||
}
|
||||
});
|
||||
|
||||
test("count & exists", async () => {
|
||||
const items = new Entity("items", [new TextField("label")]);
|
||||
const em = new EntityManager([items], dummyConnection);
|
||||
|
||||
await em.connection.kysely.schema
|
||||
.createTable("items")
|
||||
.ifNotExists()
|
||||
.addColumn("id", "integer", (col) => col.primaryKey().autoIncrement().notNull())
|
||||
.addColumn("label", "text")
|
||||
.execute();
|
||||
|
||||
// fill
|
||||
await em.connection.kysely
|
||||
.insertInto("items")
|
||||
.values([{ label: "a" }, { label: "b" }, { label: "c" }])
|
||||
.execute();
|
||||
|
||||
// count all
|
||||
const res = await em.repository(items).count();
|
||||
expect(res.sql).toBe('select count(*) as "count" from "items"');
|
||||
expect(res.count).toBe(3);
|
||||
|
||||
// count filtered
|
||||
const res2 = await em.repository(items).count({ label: { $in: ["a", "b"] } });
|
||||
|
||||
expect(res2.sql).toBe('select count(*) as "count" from "items" where "label" in (?, ?)');
|
||||
expect(res2.parameters).toEqual(["a", "b"]);
|
||||
expect(res2.count).toBe(2);
|
||||
|
||||
// check exists
|
||||
const res3 = await em.repository(items).exists({ label: "a" });
|
||||
expect(res3.exists).toBe(true);
|
||||
|
||||
const res4 = await em.repository(items).exists({ label: "d" });
|
||||
expect(res4.exists).toBe(false);
|
||||
|
||||
// for now, allow empty filter
|
||||
const res5 = await em.repository(items).exists({});
|
||||
expect(res5.exists).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("[data] Repository (Events)", async () => {
|
||||
const items = new Entity("items", [new TextField("label")]);
|
||||
const categories = new Entity("categories", [new TextField("label")]);
|
||||
const em = new EntityManager([items, categories], dummyConnection, [
|
||||
new ManyToOneRelation(categories, items)
|
||||
]);
|
||||
await em.schema().sync({ force: true });
|
||||
const events = new Map<string, any>();
|
||||
|
||||
em.repository(items).emgr.onAny((event) => {
|
||||
// @ts-ignore
|
||||
events.set(event.constructor.slug, event);
|
||||
});
|
||||
em.repository(categories).emgr.onAny((event) => {
|
||||
// @ts-ignore
|
||||
events.set(event.constructor.slug, event);
|
||||
});
|
||||
|
||||
test("events were fired", async () => {
|
||||
await em.repository(items).findId(1);
|
||||
expect(events.has(RepositoryEvents.RepositoryFindOneBefore.slug)).toBeTrue();
|
||||
expect(events.has(RepositoryEvents.RepositoryFindOneAfter.slug)).toBeTrue();
|
||||
events.clear();
|
||||
|
||||
await em.repository(items).findOne({ id: 1 });
|
||||
expect(events.has(RepositoryEvents.RepositoryFindOneBefore.slug)).toBeTrue();
|
||||
expect(events.has(RepositoryEvents.RepositoryFindOneAfter.slug)).toBeTrue();
|
||||
events.clear();
|
||||
|
||||
await em.repository(items).findMany({ where: { id: 1 } });
|
||||
expect(events.has(RepositoryEvents.RepositoryFindManyBefore.slug)).toBeTrue();
|
||||
expect(events.has(RepositoryEvents.RepositoryFindManyAfter.slug)).toBeTrue();
|
||||
events.clear();
|
||||
|
||||
await em.repository(items).findManyByReference(1, "categories");
|
||||
expect(events.has(RepositoryEvents.RepositoryFindManyBefore.slug)).toBeTrue();
|
||||
expect(events.has(RepositoryEvents.RepositoryFindManyAfter.slug)).toBeTrue();
|
||||
events.clear();
|
||||
});
|
||||
});
|
||||
269
app/__test__/data/specs/SchemaManager.spec.ts
Normal file
269
app/__test__/data/specs/SchemaManager.spec.ts
Normal file
@@ -0,0 +1,269 @@
|
||||
// eslint-disable-next-line import/no-unresolved
|
||||
import { afterAll, describe, expect, test } from "bun:test";
|
||||
import { randomString } from "../../../src/core/utils";
|
||||
import { Entity, EntityIndex, EntityManager, SchemaManager, TextField } from "../../../src/data";
|
||||
import { getDummyConnection } from "../helper";
|
||||
|
||||
const { dummyConnection, afterAllCleanup } = getDummyConnection();
|
||||
afterAll(afterAllCleanup);
|
||||
|
||||
describe("SchemaManager tests", async () => {
|
||||
test("introspect entity", async () => {
|
||||
const email = new TextField("email");
|
||||
const entity = new Entity("test", [new TextField("username"), email, new TextField("bio")]);
|
||||
const index = new EntityIndex(entity, [email]);
|
||||
const em = new EntityManager([entity], dummyConnection, [], [index]);
|
||||
const schema = new SchemaManager(em);
|
||||
|
||||
const introspection = schema.getIntrospectionFromEntity(em.entities[0]);
|
||||
expect(introspection).toEqual({
|
||||
name: "test",
|
||||
isView: false,
|
||||
columns: [
|
||||
{
|
||||
name: "id",
|
||||
dataType: "TEXT",
|
||||
isNullable: true,
|
||||
isAutoIncrementing: true,
|
||||
hasDefaultValue: false,
|
||||
comment: undefined
|
||||
},
|
||||
{
|
||||
name: "username",
|
||||
dataType: "TEXT",
|
||||
isNullable: true,
|
||||
isAutoIncrementing: false,
|
||||
hasDefaultValue: false,
|
||||
comment: undefined
|
||||
},
|
||||
{
|
||||
name: "email",
|
||||
dataType: "TEXT",
|
||||
isNullable: true,
|
||||
isAutoIncrementing: false,
|
||||
hasDefaultValue: false,
|
||||
comment: undefined
|
||||
},
|
||||
{
|
||||
name: "bio",
|
||||
dataType: "TEXT",
|
||||
isNullable: true,
|
||||
isAutoIncrementing: false,
|
||||
hasDefaultValue: false,
|
||||
comment: undefined
|
||||
}
|
||||
],
|
||||
indices: [
|
||||
{
|
||||
name: "idx_test_email",
|
||||
table: "test",
|
||||
isUnique: false,
|
||||
columns: [
|
||||
{
|
||||
name: "email",
|
||||
order: 0
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
test("add column", async () => {
|
||||
const table = "add_column";
|
||||
const index = "idx_add_column";
|
||||
const em = new EntityManager(
|
||||
[
|
||||
new Entity(table, [
|
||||
new TextField("username"),
|
||||
new TextField("email"),
|
||||
new TextField("bio")
|
||||
])
|
||||
],
|
||||
dummyConnection
|
||||
);
|
||||
const kysely = em.connection.kysely;
|
||||
|
||||
await kysely.schema
|
||||
.createTable(table)
|
||||
.ifNotExists()
|
||||
.addColumn("id", "integer", (col) => col.primaryKey().autoIncrement().notNull())
|
||||
.addColumn("username", "text")
|
||||
.addColumn("email", "text")
|
||||
.execute();
|
||||
await kysely.schema.createIndex(index).on(table).columns(["username"]).execute();
|
||||
|
||||
const schema = new SchemaManager(em);
|
||||
const diff = await schema.getDiff();
|
||||
|
||||
expect(diff).toEqual([
|
||||
{
|
||||
name: table,
|
||||
isNew: false,
|
||||
columns: { add: ["bio"], drop: [], change: [] },
|
||||
indices: { add: [], drop: [index] }
|
||||
}
|
||||
]);
|
||||
|
||||
// now sync
|
||||
await schema.sync({ force: true, drop: true });
|
||||
const diffAfter = await schema.getDiff();
|
||||
|
||||
console.log("diffAfter", diffAfter);
|
||||
expect(diffAfter.length).toBe(0);
|
||||
|
||||
await kysely.schema.dropTable(table).execute();
|
||||
});
|
||||
|
||||
test("drop column", async () => {
|
||||
const table = "drop_column";
|
||||
const em = new EntityManager(
|
||||
[new Entity(table, [new TextField("username")])],
|
||||
dummyConnection
|
||||
);
|
||||
const kysely = em.connection.kysely;
|
||||
|
||||
await kysely.schema
|
||||
.createTable(table)
|
||||
.ifNotExists()
|
||||
.addColumn("id", "integer", (col) => col.primaryKey().autoIncrement().notNull())
|
||||
.addColumn("username", "text")
|
||||
.addColumn("email", "text")
|
||||
.execute();
|
||||
|
||||
const schema = new SchemaManager(em);
|
||||
const diff = await schema.getDiff();
|
||||
|
||||
expect(diff).toEqual([
|
||||
{
|
||||
name: table,
|
||||
isNew: false,
|
||||
columns: {
|
||||
add: [],
|
||||
drop: ["email"],
|
||||
change: []
|
||||
},
|
||||
indices: { add: [], drop: [] }
|
||||
}
|
||||
]);
|
||||
|
||||
// now sync
|
||||
await schema.sync({ force: true, drop: true });
|
||||
const diffAfter = await schema.getDiff();
|
||||
|
||||
//console.log("diffAfter", diffAfter);
|
||||
expect(diffAfter.length).toBe(0);
|
||||
|
||||
await kysely.schema.dropTable(table).execute();
|
||||
});
|
||||
|
||||
test("create table and add column", async () => {
|
||||
const usersTable = "create_users";
|
||||
const postsTable = "create_posts";
|
||||
const em = new EntityManager(
|
||||
[
|
||||
new Entity(usersTable, [
|
||||
new TextField("username"),
|
||||
new TextField("email"),
|
||||
new TextField("bio")
|
||||
]),
|
||||
new Entity(postsTable, [
|
||||
new TextField("title"),
|
||||
new TextField("content"),
|
||||
new TextField("created_at")
|
||||
])
|
||||
],
|
||||
dummyConnection
|
||||
);
|
||||
const kysely = em.connection.kysely;
|
||||
|
||||
await kysely.schema
|
||||
.createTable(usersTable)
|
||||
.addColumn("id", "integer", (col) => col.primaryKey().autoIncrement().notNull())
|
||||
.addColumn("username", "text")
|
||||
.addColumn("email", "text")
|
||||
.execute();
|
||||
|
||||
const schema = new SchemaManager(em);
|
||||
const diff = await schema.getDiff();
|
||||
|
||||
expect(diff).toEqual([
|
||||
{
|
||||
name: usersTable,
|
||||
isNew: false,
|
||||
columns: { add: ["bio"], drop: [], change: [] },
|
||||
indices: { add: [], drop: [] }
|
||||
},
|
||||
{
|
||||
name: postsTable,
|
||||
isNew: true,
|
||||
columns: {
|
||||
add: ["id", "title", "content", "created_at"],
|
||||
drop: [],
|
||||
change: []
|
||||
},
|
||||
indices: { add: [], drop: [] }
|
||||
}
|
||||
]);
|
||||
|
||||
// now sync
|
||||
await schema.sync({ force: true });
|
||||
const diffAfter = await schema.getDiff();
|
||||
|
||||
//console.log("diffAfter", diffAfter);
|
||||
expect(diffAfter.length).toBe(0);
|
||||
|
||||
await kysely.schema.dropTable(usersTable).execute();
|
||||
await kysely.schema.dropTable(postsTable).execute();
|
||||
});
|
||||
|
||||
test("adds index on create", async () => {
|
||||
const entity = new Entity(randomString(16), [new TextField("email")]);
|
||||
const index = new EntityIndex(entity, [entity.getField("email")!]);
|
||||
const em = new EntityManager([entity], dummyConnection, [], [index]);
|
||||
|
||||
const diff = await em.schema().getDiff();
|
||||
expect(diff).toEqual([
|
||||
{
|
||||
name: entity.name,
|
||||
isNew: true,
|
||||
columns: { add: ["id", "email"], drop: [], change: [] },
|
||||
indices: { add: [index.name!], drop: [] }
|
||||
}
|
||||
]);
|
||||
|
||||
// sync and then check again
|
||||
await em.schema().sync({ force: true });
|
||||
|
||||
const diffAfter = await em.schema().getDiff();
|
||||
expect(diffAfter.length).toBe(0);
|
||||
});
|
||||
|
||||
test("adds index after", async () => {
|
||||
const { dummyConnection } = getDummyConnection();
|
||||
|
||||
const entity = new Entity(randomString(16), [new TextField("email", { required: true })]);
|
||||
const em = new EntityManager([entity], dummyConnection);
|
||||
await em.schema().sync({ force: true });
|
||||
|
||||
// now add index
|
||||
const index = new EntityIndex(entity, [entity.getField("email")!], true);
|
||||
em.addIndex(index);
|
||||
|
||||
const diff = await em.schema().getDiff();
|
||||
expect(diff).toEqual([
|
||||
{
|
||||
name: entity.name,
|
||||
isNew: false,
|
||||
columns: { add: [], drop: [], change: [] },
|
||||
indices: { add: [index.name!], drop: [] }
|
||||
}
|
||||
]);
|
||||
|
||||
// sync and then check again
|
||||
await em.schema().sync({ force: true });
|
||||
|
||||
const diffAfter = await em.schema().getDiff();
|
||||
expect(diffAfter.length).toBe(0);
|
||||
});
|
||||
});
|
||||
195
app/__test__/data/specs/WithBuilder.spec.ts
Normal file
195
app/__test__/data/specs/WithBuilder.spec.ts
Normal file
@@ -0,0 +1,195 @@
|
||||
import { afterAll, describe, expect, test } from "bun:test";
|
||||
import {
|
||||
Entity,
|
||||
EntityManager,
|
||||
ManyToManyRelation,
|
||||
ManyToOneRelation,
|
||||
PolymorphicRelation,
|
||||
TextField,
|
||||
WithBuilder
|
||||
} from "../../../src/data";
|
||||
import { getDummyConnection } from "../helper";
|
||||
|
||||
const { dummyConnection, afterAllCleanup } = getDummyConnection();
|
||||
afterAll(afterAllCleanup);
|
||||
|
||||
describe("[data] WithBuilder", async () => {
|
||||
test("missing relation", async () => {
|
||||
const users = new Entity("users", [new TextField("username")]);
|
||||
const em = new EntityManager([users], dummyConnection);
|
||||
|
||||
expect(() =>
|
||||
WithBuilder.addClause(em, em.connection.kysely.selectFrom("users"), users, ["posts"])
|
||||
).toThrow('Relation "posts" not found');
|
||||
});
|
||||
|
||||
test("addClause: ManyToOne", async () => {
|
||||
const users = new Entity("users", [new TextField("username")]);
|
||||
const posts = new Entity("posts", [new TextField("content")]);
|
||||
const relations = [new ManyToOneRelation(posts, users, { mappedBy: "author" })];
|
||||
const em = new EntityManager([users, posts], dummyConnection, relations);
|
||||
|
||||
const qb = WithBuilder.addClause(em, em.connection.kysely.selectFrom("users"), users, [
|
||||
"posts"
|
||||
]);
|
||||
|
||||
const res = qb.compile();
|
||||
|
||||
expect(res.sql).toBe(
|
||||
'select (select coalesce(json_group_array(json_object(\'id\', "agg"."id", \'content\', "agg"."content", \'author_id\', "agg"."author_id")), \'[]\') from (select "posts"."id" as "id", "posts"."content" as "content", "posts"."author_id" as "author_id" from "posts" where "users"."id" = "posts"."author_id" limit ?) as agg) as "posts" from "users"'
|
||||
);
|
||||
expect(res.parameters).toEqual([5]);
|
||||
|
||||
const qb2 = WithBuilder.addClause(
|
||||
em,
|
||||
em.connection.kysely.selectFrom("posts"),
|
||||
posts, // @todo: try with "users", it gives output!
|
||||
["author"]
|
||||
);
|
||||
|
||||
const res2 = qb2.compile();
|
||||
|
||||
expect(res2.sql).toBe(
|
||||
'select (select json_object(\'id\', "obj"."id", \'username\', "obj"."username") from (select "users"."id" as "id", "users"."username" as "username" from "users" where "posts"."author_id" = "users"."id" limit ?) as obj) as "author" from "posts"'
|
||||
);
|
||||
expect(res2.parameters).toEqual([1]);
|
||||
});
|
||||
|
||||
test("test with empty join", async () => {
|
||||
const qb = { qb: 1 } as any;
|
||||
|
||||
expect(WithBuilder.addClause(null as any, qb, null as any, [])).toBe(qb);
|
||||
});
|
||||
|
||||
test("test manytomany", async () => {
|
||||
const posts = new Entity("posts", [new TextField("title")]);
|
||||
const categories = new Entity("categories", [new TextField("label")]);
|
||||
|
||||
const entities = [posts, categories];
|
||||
const relations = [new ManyToManyRelation(posts, categories)];
|
||||
|
||||
const em = new EntityManager(entities, dummyConnection, relations);
|
||||
await em.schema().sync({ force: true });
|
||||
|
||||
await em.mutator(posts).insertOne({ title: "fashion post" });
|
||||
await em.mutator(posts).insertOne({ title: "beauty post" });
|
||||
|
||||
await em.mutator(categories).insertOne({ label: "fashion" });
|
||||
await em.mutator(categories).insertOne({ label: "beauty" });
|
||||
await em.mutator(categories).insertOne({ label: "tech" });
|
||||
|
||||
await em.connection.kysely
|
||||
.insertInto("posts_categories")
|
||||
.values([
|
||||
{ posts_id: 1, categories_id: 1 },
|
||||
{ posts_id: 2, categories_id: 2 },
|
||||
{ posts_id: 1, categories_id: 2 }
|
||||
])
|
||||
.execute();
|
||||
|
||||
//console.log((await em.repository().findMany("posts_categories")).result);
|
||||
|
||||
const res = await em.repository(posts).findMany({ with: ["categories"] });
|
||||
|
||||
expect(res.data).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
title: "fashion post",
|
||||
categories: [
|
||||
{ id: 1, label: "fashion" },
|
||||
{ id: 2, label: "beauty" }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
title: "beauty post",
|
||||
categories: [{ id: 2, label: "beauty" }]
|
||||
}
|
||||
]);
|
||||
|
||||
const res2 = await em.repository(categories).findMany({ with: ["posts"] });
|
||||
|
||||
//console.log(res2.sql, res2.data);
|
||||
|
||||
expect(res2.data).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
label: "fashion",
|
||||
posts: [{ id: 1, title: "fashion post" }]
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
label: "beauty",
|
||||
posts: [
|
||||
{ id: 2, title: "beauty post" },
|
||||
{ id: 1, title: "fashion post" }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
label: "tech",
|
||||
posts: []
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
test("polymorphic", async () => {
|
||||
const categories = new Entity("categories", [new TextField("name")]);
|
||||
const media = new Entity("media", [new TextField("path")]);
|
||||
|
||||
const entities = [media, categories];
|
||||
const single = new PolymorphicRelation(categories, media, {
|
||||
mappedBy: "single",
|
||||
targetCardinality: 1
|
||||
});
|
||||
const multiple = new PolymorphicRelation(categories, media, { mappedBy: "multiple" });
|
||||
|
||||
const em = new EntityManager(entities, dummyConnection, [single, multiple]);
|
||||
|
||||
const qb = WithBuilder.addClause(
|
||||
em,
|
||||
em.connection.kysely.selectFrom("categories"),
|
||||
categories,
|
||||
["single"]
|
||||
);
|
||||
const res = qb.compile();
|
||||
expect(res.sql).toBe(
|
||||
'select (select json_object(\'id\', "obj"."id", \'path\', "obj"."path") from (select "media"."id" as "id", "media"."path" as "path" from "media" where "media"."reference" = ? and "categories"."id" = "media"."entity_id" limit ?) as obj) as "single" from "categories"'
|
||||
);
|
||||
expect(res.parameters).toEqual(["categories.single", 1]);
|
||||
|
||||
const qb2 = WithBuilder.addClause(
|
||||
em,
|
||||
em.connection.kysely.selectFrom("categories"),
|
||||
categories,
|
||||
["multiple"]
|
||||
);
|
||||
const res2 = qb2.compile();
|
||||
expect(res2.sql).toBe(
|
||||
'select (select coalesce(json_group_array(json_object(\'id\', "agg"."id", \'path\', "agg"."path")), \'[]\') from (select "media"."id" as "id", "media"."path" as "path" from "media" where "media"."reference" = ? and "categories"."id" = "media"."entity_id" limit ?) as agg) as "multiple" from "categories"'
|
||||
);
|
||||
expect(res2.parameters).toEqual(["categories.multiple", 5]);
|
||||
});
|
||||
|
||||
/*test("test manytoone", async () => {
|
||||
const posts = new Entity("posts", [new TextField("title")]);
|
||||
const users = new Entity("users", [new TextField("username")]);
|
||||
const relations = [
|
||||
new ManyToOneRelation(posts, users, { mappedBy: "author" }),
|
||||
];
|
||||
const em = new EntityManager([users, posts], dummyConnection, relations);
|
||||
console.log((await em.schema().sync(true)).map((s) => s.sql).join("\n"));
|
||||
await em.schema().sync();
|
||||
|
||||
await em.mutator().insertOne("users", { username: "user1" });
|
||||
await em.mutator().insertOne("users", { username: "user2" });
|
||||
|
||||
await em.mutator().insertOne("posts", { title: "post1", author_id: 1 });
|
||||
await em.mutator().insertOne("posts", { title: "post2", author_id: 2 });
|
||||
|
||||
console.log((await em.repository().findMany("posts")).result);
|
||||
|
||||
const res = await em.repository().findMany("posts", { join: ["author"] });
|
||||
console.log(res.sql, res.parameters, res.result);
|
||||
});*/
|
||||
});
|
||||
92
app/__test__/data/specs/connection/Connection.spec.ts
Normal file
92
app/__test__/data/specs/connection/Connection.spec.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { afterAll, describe, expect, test } from "bun:test";
|
||||
import { EntityManager } from "../../../../src/data";
|
||||
import { getDummyConnection } from "../../helper";
|
||||
|
||||
const { dummyConnection, afterAllCleanup } = getDummyConnection();
|
||||
afterAll(afterAllCleanup);
|
||||
|
||||
describe("Connection", async () => {
|
||||
test("it introspects indices correctly", async () => {
|
||||
const em = new EntityManager([], dummyConnection);
|
||||
const kysely = em.connection.kysely;
|
||||
|
||||
await kysely.schema.createTable("items").ifNotExists().addColumn("name", "text").execute();
|
||||
await kysely.schema.createIndex("idx_items_name").on("items").columns(["name"]).execute();
|
||||
|
||||
const indices = await em.connection.getIntrospector().getIndices("items");
|
||||
expect(indices).toEqual([
|
||||
{
|
||||
name: "idx_items_name",
|
||||
table: "items",
|
||||
isUnique: false,
|
||||
columns: [
|
||||
{
|
||||
name: "name",
|
||||
order: 0
|
||||
}
|
||||
]
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
test("it introspects indices on multiple columns correctly", async () => {
|
||||
const em = new EntityManager([], dummyConnection);
|
||||
const kysely = em.connection.kysely;
|
||||
|
||||
await kysely.schema
|
||||
.createTable("items_multiple")
|
||||
.ifNotExists()
|
||||
.addColumn("name", "text")
|
||||
.addColumn("desc", "text")
|
||||
.execute();
|
||||
await kysely.schema
|
||||
.createIndex("idx_items_multiple")
|
||||
.on("items_multiple")
|
||||
.columns(["name", "desc"])
|
||||
.execute();
|
||||
|
||||
const indices = await em.connection.getIntrospector().getIndices("items_multiple");
|
||||
expect(indices).toEqual([
|
||||
{
|
||||
name: "idx_items_multiple",
|
||||
table: "items_multiple",
|
||||
isUnique: false,
|
||||
columns: [
|
||||
{
|
||||
name: "name",
|
||||
order: 0
|
||||
},
|
||||
{
|
||||
name: "desc",
|
||||
order: 1
|
||||
}
|
||||
]
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
test("it introspects unique indices correctly", async () => {
|
||||
const em = new EntityManager([], dummyConnection);
|
||||
const kysely = em.connection.kysely;
|
||||
const tbl_name = "items_unique";
|
||||
const idx_name = "idx_items_unique";
|
||||
|
||||
await kysely.schema.createTable(tbl_name).ifNotExists().addColumn("name", "text").execute();
|
||||
await kysely.schema.createIndex(idx_name).on(tbl_name).columns(["name"]).unique().execute();
|
||||
|
||||
const indices = await em.connection.getIntrospector().getIndices(tbl_name);
|
||||
expect(indices).toEqual([
|
||||
{
|
||||
name: idx_name,
|
||||
table: tbl_name,
|
||||
isUnique: true,
|
||||
columns: [
|
||||
{
|
||||
name: "name",
|
||||
order: 0
|
||||
}
|
||||
]
|
||||
}
|
||||
]);
|
||||
});
|
||||
});
|
||||
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
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
78
app/__test__/data/specs/relations/EntityRelation.spec.ts
Normal file
78
app/__test__/data/specs/relations/EntityRelation.spec.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { describe, expect, it, test } from "bun:test";
|
||||
import { Entity, type EntityManager } from "../../../../src/data";
|
||||
import {
|
||||
type BaseRelationConfig,
|
||||
EntityRelation,
|
||||
EntityRelationAnchor,
|
||||
RelationTypes
|
||||
} from "../../../../src/data/relations";
|
||||
|
||||
class TestEntityRelation extends EntityRelation {
|
||||
constructor(config?: BaseRelationConfig) {
|
||||
super(
|
||||
new EntityRelationAnchor(new Entity("source"), "source"),
|
||||
new EntityRelationAnchor(new Entity("target"), "target"),
|
||||
config
|
||||
);
|
||||
}
|
||||
initialize(em: EntityManager<any>) {}
|
||||
type() {
|
||||
return RelationTypes.ManyToOne; /* doesn't matter */
|
||||
}
|
||||
setDirections(directions: ("source" | "target")[]) {
|
||||
this.directions = directions;
|
||||
return this;
|
||||
}
|
||||
|
||||
buildWith(a: any, b: any, c: any): any {
|
||||
return;
|
||||
}
|
||||
|
||||
buildJoin(a: any, b: any): any {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
describe("[data] EntityRelation", async () => {
|
||||
test("other", async () => {
|
||||
const relation = new TestEntityRelation();
|
||||
expect(relation.other("source").entity.name).toBe("target");
|
||||
expect(relation.other("target").entity.name).toBe("source");
|
||||
});
|
||||
|
||||
it("visibleFrom", async () => {
|
||||
const relation = new TestEntityRelation();
|
||||
// by default, both sides are visible
|
||||
expect(relation.visibleFrom("source")).toBe(true);
|
||||
expect(relation.visibleFrom("target")).toBe(true);
|
||||
|
||||
// make source invisible
|
||||
relation.setDirections(["target"]);
|
||||
expect(relation.visibleFrom("source")).toBe(false);
|
||||
expect(relation.visibleFrom("target")).toBe(true);
|
||||
|
||||
// make target invisible
|
||||
relation.setDirections(["source"]);
|
||||
expect(relation.visibleFrom("source")).toBe(true);
|
||||
expect(relation.visibleFrom("target")).toBe(false);
|
||||
});
|
||||
|
||||
it("hydrate", async () => {
|
||||
// @todo: implement
|
||||
});
|
||||
|
||||
it("isListableFor", async () => {
|
||||
// by default, the relation is listable from target side
|
||||
const relation = new TestEntityRelation();
|
||||
expect(relation.isListableFor(relation.target.entity)).toBe(true);
|
||||
expect(relation.isListableFor(relation.source.entity)).toBe(false);
|
||||
});
|
||||
|
||||
it("required", async () => {
|
||||
const relation1 = new TestEntityRelation();
|
||||
expect(relation1.config.required).toBe(false);
|
||||
|
||||
const relation2 = new TestEntityRelation({ required: true });
|
||||
expect(relation2.config.required).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user