|
1 | | -import { describe, it, expect, beforeAll, afterAll, afterEach } from "vitest"; |
2 | | - |
3 | | -import prisma from "@calcom/prisma"; |
4 | | -import type { Booking, Credential, User } from "@calcom/prisma/client"; |
| 1 | +import { prisma } from "@calcom/prisma"; |
| 2 | +import type { Booking, Credential } from "@calcom/prisma/client"; |
| 3 | +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; |
5 | 4 |
|
6 | 5 | import { BookingReferenceRepository } from "./BookingReferenceRepository"; |
7 | 6 |
|
8 | 7 | const testRunId = `${Date.now()}-${Math.random().toString(36).substring(2, 8)}`; |
9 | 8 |
|
10 | | -describe("BookingReferenceRepository Integration Tests", () => { |
11 | | - let testUser: User; |
12 | | - let testCredential: Credential; |
13 | | - let testBooking: Booking; |
14 | | - const createdBookingReferenceIds: number[] = []; |
| 9 | +let testUserId: number; |
| 10 | +let testCredential: Credential; |
| 11 | +let testBooking: Booking; |
| 12 | +const createdBookingReferenceIds: number[] = []; |
| 13 | +const createdBookingIds: number[] = []; |
15 | 14 |
|
16 | | - beforeAll(async () => { |
17 | | - testUser = await prisma.user.create({ |
18 | | - data: { |
19 | | - email: `bookingreference-test-${testRunId}@example.com`, |
20 | | - username: `bookingreference-test-${testRunId}`, |
| 15 | +async function clearTestBookingReferences() { |
| 16 | + if (createdBookingReferenceIds.length > 0) { |
| 17 | + await prisma.bookingReference.deleteMany({ |
| 18 | + where: { |
| 19 | + id: { |
| 20 | + in: createdBookingReferenceIds, |
| 21 | + }, |
21 | 22 | }, |
22 | 23 | }); |
| 24 | + createdBookingReferenceIds.length = 0; |
| 25 | + } |
| 26 | +} |
23 | 27 |
|
24 | | - testCredential = await prisma.credential.create({ |
25 | | - data: { |
26 | | - type: "google_calendar", |
27 | | - key: {}, |
28 | | - userId: testUser.id, |
| 28 | +async function clearTestBookings() { |
| 29 | + if (createdBookingIds.length > 0) { |
| 30 | + await prisma.bookingReference.deleteMany({ |
| 31 | + where: { |
| 32 | + bookingId: { |
| 33 | + in: createdBookingIds, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }); |
| 37 | + await prisma.booking.deleteMany({ |
| 38 | + where: { |
| 39 | + id: { |
| 40 | + in: createdBookingIds, |
| 41 | + }, |
29 | 42 | }, |
30 | 43 | }); |
| 44 | + createdBookingIds.length = 0; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +describe("BookingReferenceRepository Integration Tests", () => { |
| 49 | + beforeAll(async () => { |
| 50 | + let testUser = await prisma.user.findFirst({ |
| 51 | + where: { email: "member0-acme@example.com" }, |
| 52 | + }); |
| 53 | + |
| 54 | + if (!testUser) { |
| 55 | + testUser = await prisma.user.create({ |
| 56 | + data: { |
| 57 | + email: `bookingreference-test-${testRunId}@example.com`, |
| 58 | + username: `bookingreference-test-${testRunId}`, |
| 59 | + }, |
| 60 | + }); |
| 61 | + } |
| 62 | + testUserId = testUser.id; |
| 63 | + |
| 64 | + let credential = await prisma.credential.findFirst({ |
| 65 | + where: { userId: testUserId, type: "google_calendar" }, |
| 66 | + }); |
| 67 | + |
| 68 | + if (!credential) { |
| 69 | + credential = await prisma.credential.create({ |
| 70 | + data: { |
| 71 | + type: "google_calendar", |
| 72 | + key: {}, |
| 73 | + userId: testUserId, |
| 74 | + }, |
| 75 | + }); |
| 76 | + } |
| 77 | + testCredential = credential; |
| 78 | + }); |
31 | 79 |
|
| 80 | + beforeEach(async () => { |
32 | 81 | testBooking = await prisma.booking.create({ |
33 | 82 | data: { |
34 | | - uid: `test-booking-uid-${testRunId}`, |
| 83 | + uid: `test-booking-uid-${testRunId}-${Date.now()}`, |
35 | 84 | title: "Test Booking", |
36 | 85 | startTime: new Date(), |
37 | 86 | endTime: new Date(), |
38 | | - userId: testUser.id, |
| 87 | + userId: testUserId, |
39 | 88 | }, |
40 | 89 | }); |
| 90 | + createdBookingIds.push(testBooking.id); |
41 | 91 | }); |
42 | 92 |
|
43 | 93 | afterEach(async () => { |
44 | | - if (createdBookingReferenceIds.length > 0) { |
45 | | - await prisma.bookingReference.deleteMany({ |
46 | | - where: { |
47 | | - id: { |
48 | | - in: createdBookingReferenceIds, |
49 | | - }, |
50 | | - }, |
51 | | - }); |
52 | | - createdBookingReferenceIds.splice(0, createdBookingReferenceIds.length); |
53 | | - } |
| 94 | + await clearTestBookingReferences(); |
| 95 | + await clearTestBookings(); |
54 | 96 | }); |
55 | 97 |
|
56 | 98 | afterAll(async () => { |
57 | | - await prisma.bookingReference.deleteMany({ |
58 | | - where: { |
59 | | - bookingId: testBooking.id, |
60 | | - }, |
61 | | - }); |
62 | | - |
63 | | - await prisma.booking.deleteMany({ |
64 | | - where: { |
65 | | - id: testBooking.id, |
66 | | - }, |
67 | | - }); |
68 | | - |
69 | | - await prisma.credential.deleteMany({ |
70 | | - where: { |
71 | | - id: testCredential.id, |
72 | | - }, |
73 | | - }); |
74 | | - |
75 | | - await prisma.user.deleteMany({ |
76 | | - where: { |
77 | | - id: testUser.id, |
78 | | - }, |
79 | | - }); |
| 99 | + await clearTestBookingReferences(); |
| 100 | + await clearTestBookings(); |
80 | 101 | }); |
81 | 102 |
|
82 | 103 | describe("replaceBookingReferences", () => { |
|
0 commit comments