fixtures.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //go:build unit
  2. package testutil
  3. import (
  4. "time"
  5. "github.com/Wei-Shaw/sub2api/internal/service"
  6. )
  7. // NewTestUser 创建一个可用的测试用户,可通过 opts 覆盖默认值。
  8. func NewTestUser(opts ...func(*service.User)) *service.User {
  9. u := &service.User{
  10. ID: 1,
  11. Email: "test@example.com",
  12. Username: "testuser",
  13. Role: "user",
  14. Balance: 100.0,
  15. Concurrency: 5,
  16. Status: service.StatusActive,
  17. CreatedAt: time.Now(),
  18. UpdatedAt: time.Now(),
  19. }
  20. for _, opt := range opts {
  21. opt(u)
  22. }
  23. return u
  24. }
  25. // NewTestAccount 创建一个可用的测试账户,可通过 opts 覆盖默认值。
  26. func NewTestAccount(opts ...func(*service.Account)) *service.Account {
  27. a := &service.Account{
  28. ID: 1,
  29. Name: "test-account",
  30. Platform: service.PlatformAnthropic,
  31. Status: service.StatusActive,
  32. Schedulable: true,
  33. Concurrency: 5,
  34. Priority: 1,
  35. }
  36. for _, opt := range opts {
  37. opt(a)
  38. }
  39. return a
  40. }
  41. // NewTestAPIKey 创建一个可用的测试 API Key,可通过 opts 覆盖默认值。
  42. func NewTestAPIKey(opts ...func(*service.APIKey)) *service.APIKey {
  43. groupID := int64(1)
  44. k := &service.APIKey{
  45. ID: 1,
  46. UserID: 1,
  47. Key: "sk-test-key-12345678",
  48. Name: "test-key",
  49. GroupID: &groupID,
  50. Status: service.StatusActive,
  51. CreatedAt: time.Now(),
  52. UpdatedAt: time.Now(),
  53. }
  54. for _, opt := range opts {
  55. opt(k)
  56. }
  57. return k
  58. }
  59. // NewTestGroup 创建一个可用的测试分组,可通过 opts 覆盖默认值。
  60. func NewTestGroup(opts ...func(*service.Group)) *service.Group {
  61. g := &service.Group{
  62. ID: 1,
  63. Platform: service.PlatformAnthropic,
  64. Status: service.StatusActive,
  65. Hydrated: true,
  66. }
  67. for _, opt := range opts {
  68. opt(g)
  69. }
  70. return g
  71. }