| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import { describe, it, expect, vi, beforeEach } from "vitest";
- const { mockUpsert, mockDisconnect } = vi.hoisted(() => {
- const mockUpsert = vi.fn().mockResolvedValue({});
- const mockDisconnect = vi.fn().mockResolvedValue(undefined);
- return { mockUpsert, mockDisconnect };
- });
- vi.mock("../generated/prisma", () => ({
- PrismaClient: class MockPrismaClient {
- libraryCase = { upsert: mockUpsert };
- $disconnect = mockDisconnect;
- $transaction = vi.fn().mockImplementation(async (ops: Promise<unknown>[]) => {
- const results = [];
- for (const op of ops) {
- results.push(await op);
- }
- return results;
- });
- },
- }));
- describe("prisma seed", () => {
- beforeEach(() => {
- vi.clearAllMocks();
- });
- it("appelle upsert 25 fois", async () => {
- const { main } = await import("./seed");
- await main();
- expect(mockUpsert).toHaveBeenCalledTimes(25);
- });
- it("couvre les 5 secteurs", async () => {
- const { main } = await import("./seed");
- await main();
- const sectors = mockUpsert.mock.calls.map(
- (c: [{ create: { sector: string } }]) => c[0].create.sector
- );
- const uniqueSectors = new Set(sectors);
- expect(uniqueSectors.size).toBe(5);
- expect(uniqueSectors).toContain("distribution");
- expect(uniqueSectors).toContain("industrie");
- expect(uniqueSectors).toContain("services");
- expect(uniqueSectors).toContain("sante");
- expect(uniqueSectors).toContain("finance");
- });
- it("couvre les 5 fonctions", async () => {
- const { main } = await import("./seed");
- await main();
- const departments = mockUpsert.mock.calls.map(
- (c: [{ create: { department: string } }]) => c[0].create.department
- );
- const uniqueDepts = new Set(departments);
- expect(uniqueDepts.size).toBe(5);
- expect(uniqueDepts).toContain("rh");
- expect(uniqueDepts).toContain("finance");
- expect(uniqueDepts).toContain("supply-chain");
- expect(uniqueDepts).toContain("commercial");
- expect(uniqueDepts).toContain("operations");
- });
- it("utilise des IDs stables pour l'idempotence", async () => {
- const { main } = await import("./seed");
- await main();
- const ids = mockUpsert.mock.calls.map(
- (c: [{ where: { id: string } }]) => c[0].where.id
- );
- const uniqueIds = new Set(ids);
- expect(uniqueIds.size).toBe(25);
- expect(ids).toContain("seed-dist-rh");
- expect(ids).toContain("seed-fin-ops");
- });
- it("est idempotent — deux exécutions n'ajoutent pas de doublons", async () => {
- const { main } = await import("./seed");
- await main();
- const firstRunCount = mockUpsert.mock.calls.length;
- vi.clearAllMocks();
- await main();
- const secondRunCount = mockUpsert.mock.calls.length;
- expect(firstRunCount).toBe(25);
- expect(secondRunCount).toBe(25);
- // Chaque upsert utilise le même ID — pas de create supplémentaire
- const ids = mockUpsert.mock.calls.map(
- (c: [{ where: { id: string } }]) => c[0].where.id
- );
- expect(new Set(ids).size).toBe(25);
- });
- });
|