Copy of PR #22493: fix: resolve unique constraint violations in listWithTeam integration test#22499
Conversation
… listWithTeam integration test
Originally by: zomars
# fix: resolve unique constraint violations in listWithTeam integration test
## Summary
Fixed a failing integration test in `listWithTeam.handler.integration-test.ts` that was causing unique constraint violations. The test was using hardcoded email addresses and slugs that persisted between test runs, causing failures when the same test data already existed in the database.
**Key changes:**
- Added timestamp-based unique identifiers for usernames, emails, team slugs, and event type slugs
- Implemented robust error handling in test cleanup with null checks and try-catch blocks
- Test success rate improved from 113/114 to 114/114 (100% pass rate)
**Root cause:** The test used static values like `testuser-lwt-1@example.com` and `team-1-lwt` that violated unique constraints on subsequent test runs.
## Review & Testing Checklist for Human
- [ ] **Verify test passes consistently** - Run `TZ=UTC yarn test -- --integrationTestsOnly` multiple times to ensure the fix is robust
- [ ] **Check timestamp uniqueness approach** - Confirm that `Date.now()` provides sufficient uniqueness for concurrent test scenarios
- [ ] **Validate cleanup logic** - Ensure the new error handling in `afterAll` properly cleans up test data without masking real errors
- [ ] **Test multiple rapid runs** - Execute the test suite several times in quick succession to verify no race conditions exist
---
### Diagram
```mermaid
%%{ init : { "theme" : "default" }}%%
graph TD
Test["packages/trpc/server/routers/viewer/eventTypes/<br/>listWithTeam.handler.integration-test.ts"]:::major-edit
Handler["listWithTeam.handler"]:::context
UserModel["User (Prisma Model)"]:::context
TeamModel["Team (Prisma Model)"]:::context
EventTypeModel["EventType (Prisma Model)"]:::context
Test --> Handler
Test --> UserModel
Test --> TeamModel
Test --> EventTypeModel
Test -- "creates with unique timestamps" --> UserModel
Test -- "creates with unique slugs" --> TeamModel
Test -- "creates with unique slugs" --> EventTypeModel
subgraph Legend
L1[Major Edit]:::major-edit
L2[Minor Edit]:::minor-edit
L3[Context/No Edit]:::context
end
classDef major-edit fill:#90EE90
classDef minor-edit fill:#87CEEB
classDef context fill:#FFFFFF
```
### Notes
- This fix addresses the PrismaClientKnownRequestError: Unique constraint failed on the fields: (`email`) that was preventing the integration test suite from achieving 100% pass rate
- The solution maintains test isolation by ensuring each test run creates truly unique database records
- Session requested by @zomars: https://app.devin.ai/sessions/3d62b140e87c424ab80c9ed76ac298ca
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit
* **Tests**
* Improved test reliability by ensuring unique test data for each run and enhancing cleanup procedures for better error handling.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
|
@dakshgup is attempting to deploy a commit to the cal Team on Vercel. A member of the Team first needs to authorize it. |
WalkthroughThe integration test setup and cleanup logic were updated to ensure unique entity creation by appending timestamps to identifiers. Cleanup procedures were enhanced with conditional existence checks, error handling, and logging to prevent issues when entities are missing or already deleted. Changes
Poem
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
packages/trpc/server/routers/viewer/eventTypes/listWithTeam.handler.integration-test.tsOops! Something went wrong! :( ESLint: 8.57.1 ESLint couldn't find the plugin "eslint-plugin-playwright". (The package "eslint-plugin-playwright" was not found when loaded as a Node module from the directory "".) It's likely that the plugin isn't installed correctly. Try reinstalling by running the following: The plugin "eslint-plugin-playwright" was referenced from the config file in ".eslintrc.js". If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team. ✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
|
|
Hey there and thank you for opening this pull request! 👋🏼 We require pull request titles to follow the Conventional Commits specification and it looks like your proposed title needs to be adjusted. Details: |
Graphite Automations"Add consumer team as reviewer" took an action on this PR • (07/14/25)1 reviewer was added to this PR based on Keith Williams's automation. "Add community label" took an action on this PR • (07/14/25)1 label was added to this PR based on Keith Williams's automation. |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/trpc/server/routers/viewer/eventTypes/listWithTeam.handler.integration-test.ts (1)
21-21: Consider potential race conditions with timestamp-based uniqueness.While
Date.now()provides good uniqueness for most scenarios, there's a theoretical risk of race conditions if multiple test instances run simultaneously and generate the same timestamp.Consider using
crypto.randomUUID()or a combination of timestamp + random suffix for stronger uniqueness guarantees:- const timestamp = Date.now(); + const uniqueId = `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;Then update the identifier usage throughout the test setup.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/trpc/server/routers/viewer/eventTypes/listWithTeam.handler.integration-test.ts(6 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Install dependencies / Yarn install & cache
- GitHub Check: Security Check
🔇 Additional comments (3)
packages/trpc/server/routers/viewer/eventTypes/listWithTeam.handler.integration-test.ts (3)
24-25: LGTM! Timestamp-based email and username uniqueness.The addition of timestamps to usernames and emails effectively prevents unique constraint violations while maintaining readable test data.
Also applies to: 31-32
40-40: LGTM! Consistent timestamp approach for all slug fields.The timestamp suffix has been consistently applied to all slug fields (team slugs and event type slugs), ensuring comprehensive uniqueness across test runs.
Also applies to: 54-54, 61-61, 70-70, 80-80, 89-89
98-128: Excellent improvement to cleanup logic with robust error handling.The enhanced cleanup implementation addresses several important concerns:
- Conditional existence checks: Prevents unnecessary database calls when entities don't exist
- Error handling: Try-catch block prevents cleanup failures from masking real test errors
- Null safety:
filter(Boolean)removes undefined IDs from deletion arrays- Graceful failure: Warning logs provide visibility without failing tests
One minor suggestion for improved debugging:
} catch (error) { - console.warn("Test cleanup failed:", error); + console.warn("Test cleanup failed - this may cause issues in subsequent test runs:", error); }
This is a copy of #22493
Originally by: zomars
fix: resolve unique constraint violations in listWithTeam integration test
Summary
Fixed a failing integration test in
listWithTeam.handler.integration-test.tsthat was causing unique constraint violations. The test was using hardcoded email addresses and slugs that persisted between test runs, causing failures when the same test data already existed in the database.Key changes:
Root cause: The test used static values like
testuser-lwt-1@example.comandteam-1-lwtthat violated unique constraints on subsequent test runs.Review & Testing Checklist for Human
TZ=UTC yarn test -- --integrationTestsOnlymultiple times to ensure the fix is robustDate.now()provides sufficient uniqueness for concurrent test scenariosafterAllproperly cleans up test data without masking real errorsDiagram
%%{ init : { "theme" : "default" }}%% graph TD Test["packages/trpc/server/routers/viewer/eventTypes/<br/>listWithTeam.handler.integration-test.ts"]:::major-edit Handler["listWithTeam.handler"]:::context UserModel["User (Prisma Model)"]:::context TeamModel["Team (Prisma Model)"]:::context EventTypeModel["EventType (Prisma Model)"]:::context Test --> Handler Test --> UserModel Test --> TeamModel Test --> EventTypeModel Test -- "creates with unique timestamps" --> UserModel Test -- "creates with unique slugs" --> TeamModel Test -- "creates with unique slugs" --> EventTypeModel subgraph Legend L1[Major Edit]:::major-edit L2[Minor Edit]:::minor-edit L3[Context/No Edit]:::context end classDef major-edit fill:#90EE90 classDef minor-edit fill:#87CEEB classDef context fill:#FFFFFFNotes
email) that was preventing the integration test suite from achieving 100% pass rateSummary by CodeRabbit