Skip to content

Scheduling infrastructure for absolutely everyone.

fix: improve BookingReferenceRepository integration test isolation (#26707)

Keith Williams539214b16444
diff --git a/packages/features/bookingReference/repositories/BookingReferenceRepository.integration-test.ts b/packages/features/bookingReference/repositories/BookingReferenceRepository.integration-test.ts
index 17822d4e130..d21d8675021 100644
--- a/packages/features/bookingReference/repositories/BookingReferenceRepository.integration-test.ts
+++ b/packages/features/bookingReference/repositories/BookingReferenceRepository.integration-test.ts
@@ -1,82 +1,103 @@
-import { describe, it, expect, beforeAll, afterAll, afterEach } from "vitest";
-
-import prisma from "@calcom/prisma";
-import type { Booking, Credential, User } from "@calcom/prisma/client";
+import { prisma } from "@calcom/prisma";
+import type { Booking, Credential } from "@calcom/prisma/client";
+import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
import { BookingReferenceRepository } from "./BookingReferenceRepository";
const testRunId = `${Date.now()}-${Math.random().toString(36).substring(2, 8)}`;
-describe("BookingReferenceRepository Integration Tests", () => {
- let testUser: User;
- let testCredential: Credential;
- let testBooking: Booking;
- const createdBookingReferenceIds: number[] = [];
+let testUserId: number;
+let testCredential: Credential;
+let testBooking: Booking;
+const createdBookingReferenceIds: number[] = [];
+const createdBookingIds: number[] = [];
- beforeAll(async () => {
- testUser = await prisma.user.create({
- data: {
- email: `bookingreference-test-${testRunId}@example.com`,
- username: `bookingreference-test-${testRunId}`,
+async function clearTestBookingReferences() {
+ if (createdBookingReferenceIds.length > 0) {
+ await prisma.bookingReference.deleteMany({
+ where: {
+ id: {
+ in: createdBookingReferenceIds,
+ },
},
});
+ createdBookingReferenceIds.length = 0;
+ }
+}
- testCredential = await prisma.credential.create({
- data: {
- type: "google_calendar",
- key: {},
- userId: testUser.id,
+async function clearTestBookings() {
+ if (createdBookingIds.length > 0) {
+ await prisma.bookingReference.deleteMany({
+ where: {
+ bookingId: {
+ in: createdBookingIds,
+ },
+ },
+ });
+ await prisma.booking.deleteMany({
+ where: {
+ id: {
+ in: createdBookingIds,
+ },
},
});
+ createdBookingIds.length = 0;
+ }
+}
+
+describe("BookingReferenceRepository Integration Tests", () => {
+ beforeAll(async () => {
+ let testUser = await prisma.user.findFirst({
+ where: { email: "[email protected]" },
+ });
+
+ if (!testUser) {
+ testUser = await prisma.user.create({
+ data: {
+ email: `bookingreference-test-${testRunId}@example.com`,
+ username: `bookingreference-test-${testRunId}`,
+ },
+ });
+ }
+ testUserId = testUser.id;
+
+ let credential = await prisma.credential.findFirst({
+ where: { userId: testUserId, type: "google_calendar" },
+ });
+
+ if (!credential) {
+ credential = await prisma.credential.create({
+ data: {
+ type: "google_calendar",
+ key: {},
+ userId: testUserId,
+ },
+ });
+ }
+ testCredential = credential;
+ });
+ beforeEach(async () => {
testBooking = await prisma.booking.create({
data: {
- uid: `test-booking-uid-${testRunId}`,
+ uid: `test-booking-uid-${testRunId}-${Date.now()}`,
title: "Test Booking",
startTime: new Date(),
endTime: new Date(),
- userId: testUser.id,
+ userId: testUserId,
},
});
+ createdBookingIds.push(testBooking.id);
});
afterEach(async () => {
- if (createdBookingReferenceIds.length > 0) {
- await prisma.bookingReference.deleteMany({
- where: {
- id: {
- in: createdBookingReferenceIds,
- },
- },
- });
- createdBookingReferenceIds.splice(0, createdBookingReferenceIds.length);
- }
+ await clearTestBookingReferences();
+ await clearTestBookings();
});
afterAll(async () => {
- await prisma.bookingReference.deleteMany({
- where: {
- bookingId: testBooking.id,
- },
- });
-
- await prisma.booking.deleteMany({
- where: {
- id: testBooking.id,
- },
- });
-
- await prisma.credential.deleteMany({
- where: {
- id: testCredential.id,
- },
- });
-
- await prisma.user.deleteMany({
- where: {
- id: testUser.id,
- },
- });
+ await clearTestBookingReferences();
+ await clearTestBookings();
});
describe("replaceBookingReferences", () => {