account_repo_duplicate_integration_test.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //go:build integration
  2. package repository
  3. import (
  4. "context"
  5. "fmt"
  6. "testing"
  7. "time"
  8. "github.com/Wei-Shaw/sub2api/internal/service"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestCreateWithAccountGroupsPersistsPausedCopyAtomically(t *testing.T) {
  12. ctx := context.Background()
  13. client := testEntClient(t)
  14. repo := newAccountRepositoryWithSQL(client, integrationDB, nil)
  15. suffix := time.Now().UnixNano()
  16. group, err := client.Group.Create().
  17. SetName(fmt.Sprintf("duplicate-atomic-%d", suffix)).
  18. SetPlatform(service.PlatformAnthropic).
  19. Save(ctx)
  20. require.NoError(t, err)
  21. success := &service.Account{
  22. Name: fmt.Sprintf("duplicate-success-%d", suffix),
  23. Platform: service.PlatformAnthropic,
  24. Type: service.AccountTypeAPIKey,
  25. Status: service.StatusActive,
  26. Schedulable: false,
  27. Credentials: map[string]any{"api_key": "secret"},
  28. Extra: map[string]any{},
  29. }
  30. require.NoError(t, repo.CreateWithAccountGroups(ctx, success, []service.AccountGroup{{GroupID: group.ID, Priority: 37}}))
  31. t.Cleanup(func() {
  32. _, _ = integrationDB.ExecContext(context.Background(), "DELETE FROM scheduler_outbox WHERE account_id = $1", success.ID)
  33. _, _ = integrationDB.ExecContext(context.Background(), "DELETE FROM account_groups WHERE account_id = $1", success.ID)
  34. _, _ = integrationDB.ExecContext(context.Background(), "DELETE FROM accounts WHERE id = $1", success.ID)
  35. _, _ = integrationDB.ExecContext(context.Background(), "DELETE FROM groups WHERE id = $1", group.ID)
  36. })
  37. var schedulable bool
  38. require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT schedulable FROM accounts WHERE id = $1", success.ID).Scan(&schedulable))
  39. require.False(t, schedulable)
  40. var priority int
  41. require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT priority FROM account_groups WHERE account_id = $1 AND group_id = $2", success.ID, group.ID).Scan(&priority))
  42. require.Equal(t, 37, priority)
  43. var outboxCount int
  44. require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT COUNT(*) FROM scheduler_outbox WHERE account_id = $1", success.ID).Scan(&outboxCount))
  45. require.Equal(t, 1, outboxCount)
  46. failure := &service.Account{
  47. Name: fmt.Sprintf("duplicate-failure-%d", suffix),
  48. Platform: service.PlatformAnthropic,
  49. Type: service.AccountTypeAPIKey,
  50. Status: service.StatusActive,
  51. Schedulable: false,
  52. Credentials: map[string]any{"api_key": "secret"},
  53. Extra: map[string]any{},
  54. }
  55. err = repo.CreateWithAccountGroups(ctx, failure, []service.AccountGroup{{GroupID: int64(^uint64(0) >> 1), Priority: 1}})
  56. require.Error(t, err)
  57. var accountCount, groupCount, failedOutboxCount int
  58. require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT COUNT(*) FROM accounts WHERE name = $1", failure.Name).Scan(&accountCount))
  59. require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT COUNT(*) FROM account_groups WHERE account_id = $1", failure.ID).Scan(&groupCount))
  60. require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT COUNT(*) FROM scheduler_outbox WHERE account_id = $1", failure.ID).Scan(&failedOutboxCount))
  61. require.Zero(t, accountCount)
  62. require.Zero(t, groupCount)
  63. require.Zero(t, failedOutboxCount)
  64. }