Add prisma seed tests and refactor seed script
This commit is contained in:
49
__tests__/seed.test.ts
Normal file
49
__tests__/seed.test.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { mock } from "bun:test";
|
||||
|
||||
const mockPrisma = {
|
||||
user: {
|
||||
upsert: mock(async () => ({ id: "user-1", email: "admin@example.com" })),
|
||||
},
|
||||
apiKey: {
|
||||
upsert: mock(async () => ({ id: "key-1" })),
|
||||
},
|
||||
webHook: {
|
||||
findFirst: mock(async () => null as any),
|
||||
create: mock(async () => ({ id: "webhook-1" })),
|
||||
},
|
||||
chatFlows: {
|
||||
upsert: mock(async () => ({ id: "flow-1" })),
|
||||
},
|
||||
$disconnect: mock(async () => {}),
|
||||
};
|
||||
|
||||
mock.module("@/server/lib/prisma", () => ({
|
||||
prisma: mockPrisma,
|
||||
}));
|
||||
|
||||
import { expect, test, describe } from "bun:test";
|
||||
import { seed } from "../prisma/seed";
|
||||
|
||||
describe("Prisma Seed", () => {
|
||||
test("seed function should populate default data", async () => {
|
||||
const result = await seed();
|
||||
|
||||
expect(result.user.email).toBe("admin@example.com");
|
||||
expect(mockPrisma.user.upsert).toHaveBeenCalled();
|
||||
expect(mockPrisma.apiKey.upsert).toHaveBeenCalled();
|
||||
expect(mockPrisma.webHook.findFirst).toHaveBeenCalled();
|
||||
expect(mockPrisma.webHook.create).toHaveBeenCalled();
|
||||
expect(mockPrisma.chatFlows.upsert).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("seed function should skip webhook creation if it already exists", async () => {
|
||||
// Reset mocks
|
||||
mockPrisma.webHook.create.mockClear();
|
||||
mockPrisma.webHook.findFirst.mockResolvedValueOnce({ id: "existing-webhook" });
|
||||
|
||||
await seed();
|
||||
|
||||
expect(mockPrisma.webHook.findFirst).toHaveBeenCalled();
|
||||
expect(mockPrisma.webHook.create).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user