seed.test.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { describe, it, expect, vi, beforeEach } from "vitest";
  2. const { mockUpsert, mockDisconnect } = vi.hoisted(() => {
  3. const mockUpsert = vi.fn().mockResolvedValue({});
  4. const mockDisconnect = vi.fn().mockResolvedValue(undefined);
  5. return { mockUpsert, mockDisconnect };
  6. });
  7. vi.mock("../generated/prisma", () => ({
  8. PrismaClient: class MockPrismaClient {
  9. libraryCase = { upsert: mockUpsert };
  10. $disconnect = mockDisconnect;
  11. $transaction = vi.fn().mockImplementation(async (ops: Promise<unknown>[]) => {
  12. const results = [];
  13. for (const op of ops) {
  14. results.push(await op);
  15. }
  16. return results;
  17. });
  18. },
  19. }));
  20. describe("prisma seed", () => {
  21. beforeEach(() => {
  22. vi.clearAllMocks();
  23. });
  24. it("appelle upsert 25 fois", async () => {
  25. const { main } = await import("./seed");
  26. await main();
  27. expect(mockUpsert).toHaveBeenCalledTimes(25);
  28. });
  29. it("couvre les 5 secteurs", async () => {
  30. const { main } = await import("./seed");
  31. await main();
  32. const sectors = mockUpsert.mock.calls.map(
  33. (c: [{ create: { sector: string } }]) => c[0].create.sector
  34. );
  35. const uniqueSectors = new Set(sectors);
  36. expect(uniqueSectors.size).toBe(5);
  37. expect(uniqueSectors).toContain("distribution");
  38. expect(uniqueSectors).toContain("industrie");
  39. expect(uniqueSectors).toContain("services");
  40. expect(uniqueSectors).toContain("sante");
  41. expect(uniqueSectors).toContain("finance");
  42. });
  43. it("couvre les 5 fonctions", async () => {
  44. const { main } = await import("./seed");
  45. await main();
  46. const departments = mockUpsert.mock.calls.map(
  47. (c: [{ create: { department: string } }]) => c[0].create.department
  48. );
  49. const uniqueDepts = new Set(departments);
  50. expect(uniqueDepts.size).toBe(5);
  51. expect(uniqueDepts).toContain("rh");
  52. expect(uniqueDepts).toContain("finance");
  53. expect(uniqueDepts).toContain("supply-chain");
  54. expect(uniqueDepts).toContain("commercial");
  55. expect(uniqueDepts).toContain("operations");
  56. });
  57. it("utilise des IDs stables pour l'idempotence", async () => {
  58. const { main } = await import("./seed");
  59. await main();
  60. const ids = mockUpsert.mock.calls.map(
  61. (c: [{ where: { id: string } }]) => c[0].where.id
  62. );
  63. const uniqueIds = new Set(ids);
  64. expect(uniqueIds.size).toBe(25);
  65. expect(ids).toContain("seed-dist-rh");
  66. expect(ids).toContain("seed-fin-ops");
  67. });
  68. it("est idempotent — deux exécutions n'ajoutent pas de doublons", async () => {
  69. const { main } = await import("./seed");
  70. await main();
  71. const firstRunCount = mockUpsert.mock.calls.length;
  72. vi.clearAllMocks();
  73. await main();
  74. const secondRunCount = mockUpsert.mock.calls.length;
  75. expect(firstRunCount).toBe(25);
  76. expect(secondRunCount).toBe(25);
  77. // Chaque upsert utilise le même ID — pas de create supplémentaire
  78. const ids = mockUpsert.mock.calls.map(
  79. (c: [{ where: { id: string } }]) => c[0].where.id
  80. );
  81. expect(new Set(ids).size).toBe(25);
  82. });
  83. });