mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
public commit
This commit is contained in:
50
app/__test__/data/mutation.relation.test.ts
Normal file
50
app/__test__/data/mutation.relation.test.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
// eslint-disable-next-line import/no-unresolved
|
||||
import { afterAll, describe, expect, test } from "bun:test";
|
||||
import {
|
||||
Entity,
|
||||
EntityManager,
|
||||
ManyToOneRelation,
|
||||
NumberField,
|
||||
SchemaManager,
|
||||
TextField
|
||||
} from "../../src/data";
|
||||
import { getDummyConnection } from "./helper";
|
||||
|
||||
const { dummyConnection, afterAllCleanup } = getDummyConnection();
|
||||
afterAll(afterAllCleanup);
|
||||
|
||||
describe("Mutator relation", async () => {
|
||||
const connection = dummyConnection;
|
||||
//const connection = getLocalLibsqlConnection();
|
||||
//const connection = getCreds("DB_DATA");
|
||||
|
||||
const posts = new Entity("posts", [
|
||||
new TextField("title"),
|
||||
new TextField("content", { default_value: "..." }),
|
||||
new NumberField("count", { default_value: 0 })
|
||||
]);
|
||||
|
||||
const users = new Entity("users", [new TextField("username")]);
|
||||
|
||||
const relations = [new ManyToOneRelation(posts, users)];
|
||||
|
||||
const em = new EntityManager([posts, users], connection, relations);
|
||||
|
||||
const schema = new SchemaManager(em);
|
||||
await schema.sync({ force: true });
|
||||
|
||||
test("add users", async () => {
|
||||
const { data } = await em.mutator(users).insertOne({ username: "user1" });
|
||||
await em.mutator(users).insertOne({ username: "user2" });
|
||||
|
||||
// create some posts
|
||||
await em.mutator(posts).insertOne({ title: "post1", content: "content1" });
|
||||
|
||||
// expect to throw
|
||||
expect(em.mutator(posts).insertOne({ title: "post2", users_id: 10 })).rejects.toThrow();
|
||||
|
||||
expect(
|
||||
em.mutator(posts).insertOne({ title: "post2", users_id: data.id })
|
||||
).resolves.toBeDefined();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user