Skip to content

Copy of PR #22493: fix: resolve unique constraint violations in listWithTeam integration test#3

Open
dakshgup wants to merge 1 commit intomainfrom
copy-pr-22493-devin/1752527545-fix-integration-test-unique-constraints
Open

Copy of PR #22493: fix: resolve unique constraint violations in listWithTeam integration test#3
dakshgup wants to merge 1 commit intomainfrom
copy-pr-22493-devin/1752527545-fix-integration-test-unique-constraints

Conversation

@dakshgup
Copy link
Owner

This is a copy of calcom#22493

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

%%{ 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
Loading

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

Summary by CodeRabbit

  • Tests
    • Improved test reliability by ensuring unique test data for each run and enhancing cleanup procedures for better error handling.

… 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 -->
@github-actions
Copy link

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:

No release type found in pull request title "Copy of PR #22493: fix: resolve unique constraint violations in listWithTeam integration test". Add a prefix to indicate what kind of release this pull request corresponds to. For reference, see https://www.conventionalcommits.org/

Available types:
 - feat: A new feature
 - fix: A bug fix
 - docs: Documentation only changes
 - style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
 - refactor: A code change that neither fixes a bug nor adds a feature
 - perf: A code change that improves performance
 - test: Adding missing tests or correcting existing tests
 - build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
 - ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
 - chore: Other changes that don't modify src or test files
 - revert: Reverts a previous commit

@dakshgup
Copy link
Owner Author

cursor review

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ BugBot reviewed your changes and found no bugs!


BugBot free trial expires on July 22, 2025
Learn more in the Cursor dashboard.

Was this report helpful? Give feedback by reacting with 👍 or 👎

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Summary

This PR fixes failing integration tests by addressing unique constraint violations in the listWithTeam.handler.integration-test.ts file. The key issue was that tests were using static values for emails and slugs, causing database conflicts when tests were run multiple times. The solution adds timestamp-based unique identifiers and improves error handling during test cleanup.

Specific changes include:

  • Using Date.now() to generate unique identifiers for test data (usernames, emails, team slugs)
  • Adding robust error handling with try-catch blocks in test cleanup
  • Implementing proper null checks and Boolean filtering for cleanup operations

This fix takes the test suite from a 99.1% pass rate (113/114) to 100% (114/114).

Confidence score: 5/5

  1. This PR is very safe to merge as it only affects test code and has no impact on production
  2. The changes follow best practices for test isolation and error handling
  3. Files needing attention:
    • packages/trpc/server/routers/viewer/eventTypes/listWithTeam.handler.integration-test.ts - Verify the timestamp-based uniqueness approach works for concurrent test scenarios

1 file reviewed, no comments
Edit PR Review Bot Settings | Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant