From 6f7465ee5a3e3db9c1c23f6e41e54a9f67b82ee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Gierwia=C5=82o?= Date: Sun, 23 Nov 2025 22:59:06 +0100 Subject: [PATCH] fix(tests): add wsdcId cleanup to prevent unique constraint violations Both users.test.js and auth-phase1.5.test.js were failing due to unique constraint violations on wsdc_id field when running after other test suites. Added wsdcId to cleanup queries and proper deletion of related data (messages, matches, eventParticipants). --- backend/src/__tests__/auth-phase1.5.test.js | 62 ++++++++++++++++++++- backend/src/__tests__/users.test.js | 14 +++-- 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/backend/src/__tests__/auth-phase1.5.test.js b/backend/src/__tests__/auth-phase1.5.test.js index 146e5d5..c88f5a0 100644 --- a/backend/src/__tests__/auth-phase1.5.test.js +++ b/backend/src/__tests__/auth-phase1.5.test.js @@ -27,12 +27,41 @@ beforeAll(async () => { 'resend@example.com', 'expired@example.com', 'reset@example.com', 'newpass@example.com', 'expiredreset@example.com', 'emailfail@example.com', 'wsdc@example.com', 'user1@example.com', 'user2@example.com']; + const testWsdcIds = ['26997', '12345']; + + // Find test users + const testUsers = await prisma.user.findMany({ + where: { + OR: [ + { username: { in: testUsernames } }, + { email: { in: testEmails } }, + { wsdcId: { in: testWsdcIds } } + ] + }, + select: { id: true } + }); + const testUserIds = testUsers.map(u => u.id); + + // Delete related data for test users only + if (testUserIds.length > 0) { + await prisma.message.deleteMany({ where: { userId: { in: testUserIds } } }); + await prisma.match.deleteMany({ + where: { + OR: [ + { user1Id: { in: testUserIds } }, + { user2Id: { in: testUserIds } } + ] + } + }); + await prisma.eventParticipant.deleteMany({ where: { userId: { in: testUserIds } } }); + } await prisma.user.deleteMany({ where: { OR: [ { username: { in: testUsernames } }, - { email: { in: testEmails } } + { email: { in: testEmails } }, + { wsdcId: { in: testWsdcIds } } ] } }); @@ -47,12 +76,41 @@ afterAll(async () => { 'resend@example.com', 'expired@example.com', 'reset@example.com', 'newpass@example.com', 'expiredreset@example.com', 'emailfail@example.com', 'wsdc@example.com', 'user1@example.com', 'user2@example.com']; + const testWsdcIds = ['26997', '12345']; + + // Find test users + const testUsers = await prisma.user.findMany({ + where: { + OR: [ + { username: { in: testUsernames } }, + { email: { in: testEmails } }, + { wsdcId: { in: testWsdcIds } } + ] + }, + select: { id: true } + }); + const testUserIds = testUsers.map(u => u.id); + + // Delete related data for test users only + if (testUserIds.length > 0) { + await prisma.message.deleteMany({ where: { userId: { in: testUserIds } } }); + await prisma.match.deleteMany({ + where: { + OR: [ + { user1Id: { in: testUserIds } }, + { user2Id: { in: testUserIds } } + ] + } + }); + await prisma.eventParticipant.deleteMany({ where: { userId: { in: testUserIds } } }); + } await prisma.user.deleteMany({ where: { OR: [ { username: { in: testUsernames } }, - { email: { in: testEmails } } + { email: { in: testEmails } }, + { wsdcId: { in: testWsdcIds } } ] } }); diff --git a/backend/src/__tests__/users.test.js b/backend/src/__tests__/users.test.js index a4b7720..58df0f3 100644 --- a/backend/src/__tests__/users.test.js +++ b/backend/src/__tests__/users.test.js @@ -12,13 +12,15 @@ beforeAll(async () => { // Clean up only specific test data (not all tables) const testUsernames = ['users_john_dancer', 'users_sarah_swings']; const testEmails = ['users_john@example.com', 'users_sarah@example.com']; + const testWsdcIds = ['12345']; // Find test users const testUsers = await prisma.user.findMany({ where: { OR: [ { username: { in: testUsernames } }, - { email: { in: testEmails } } + { email: { in: testEmails } }, + { wsdcId: { in: testWsdcIds } } ] }, select: { id: true } @@ -45,7 +47,8 @@ beforeAll(async () => { where: { OR: [ { username: { in: testUsernames } }, - { email: { in: testEmails } } + { email: { in: testEmails } }, + { wsdcId: { in: testWsdcIds } } ] } }); @@ -87,12 +90,14 @@ afterAll(async () => { // Clean up only specific test data const testUsernames = ['users_john_dancer', 'users_sarah_swings']; const testEmails = ['users_john@example.com', 'users_sarah@example.com']; + const testWsdcIds = ['12345']; const testUsers = await prisma.user.findMany({ where: { OR: [ { username: { in: testUsernames } }, - { email: { in: testEmails } } + { email: { in: testEmails } }, + { wsdcId: { in: testWsdcIds } } ] }, select: { id: true } @@ -117,7 +122,8 @@ afterAll(async () => { where: { OR: [ { username: { in: testUsernames } }, - { email: { in: testEmails } } + { email: { in: testEmails } }, + { wsdcId: { in: testWsdcIds } } ] } });