account_repo_spark_shadow_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //go:build integration
  2. package repository
  3. import (
  4. "context"
  5. "testing"
  6. "github.com/Wei-Shaw/sub2api/internal/service"
  7. )
  8. func TestAccountRepoSparkShadowRoundTrip(t *testing.T) {
  9. ctx := context.Background()
  10. tx := testEntTx(t)
  11. repo := newAccountRepositoryWithSQL(tx.Client(), tx, nil)
  12. parent := &service.Account{
  13. Name: "parent",
  14. Platform: service.PlatformOpenAI,
  15. Type: service.AccountTypeOAuth,
  16. Status: service.StatusActive,
  17. }
  18. if err := repo.Create(ctx, parent); err != nil {
  19. t.Fatalf("create parent: %v", err)
  20. }
  21. pid := parent.ID
  22. shadow := &service.Account{
  23. Name: "shadow",
  24. Platform: service.PlatformOpenAI,
  25. Type: service.AccountTypeOAuth,
  26. Status: service.StatusActive,
  27. ParentAccountID: &pid,
  28. QuotaDimension: service.QuotaDimensionSpark,
  29. }
  30. if err := repo.Create(ctx, shadow); err != nil {
  31. t.Fatalf("create shadow: %v", err)
  32. }
  33. got, err := repo.GetByID(ctx, shadow.ID)
  34. if err != nil {
  35. t.Fatalf("get: %v", err)
  36. }
  37. if got.ParentAccountID == nil || *got.ParentAccountID != pid {
  38. t.Fatalf("ParentAccountID round-trip: %v", got.ParentAccountID)
  39. }
  40. if got.QuotaDimension != service.QuotaDimensionSpark {
  41. t.Fatalf("QuotaDimension: %q", got.QuotaDimension)
  42. }
  43. }
  44. func TestListShadowsByParent(t *testing.T) {
  45. // Schema enforces at most one spark shadow per parent (uq_accounts_spark_shadow_per_parent).
  46. // Test strategy: create 2 parents each with 1 spark shadow + 1 unrelated account;
  47. // assert ListShadowsByParent(parent1.ID) returns exactly 1 (filtering by both
  48. // parent_account_id and quota_dimension='spark', excluding parent2's shadow and unrelated).
  49. ctx := context.Background()
  50. tx := testEntTx(t)
  51. repo := newAccountRepositoryWithSQL(tx.Client(), tx, nil)
  52. // Create parent1 and its spark shadow
  53. parent1 := &service.Account{
  54. Name: "list-parent1",
  55. Platform: service.PlatformOpenAI,
  56. Type: service.AccountTypeOAuth,
  57. Status: service.StatusActive,
  58. }
  59. if err := repo.Create(ctx, parent1); err != nil {
  60. t.Fatalf("create parent1: %v", err)
  61. }
  62. pid1 := parent1.ID
  63. shadow1 := &service.Account{
  64. Name: "shadow1",
  65. Platform: service.PlatformOpenAI,
  66. Type: service.AccountTypeOAuth,
  67. Status: service.StatusActive,
  68. ParentAccountID: &pid1,
  69. QuotaDimension: service.QuotaDimensionSpark,
  70. }
  71. if err := repo.Create(ctx, shadow1); err != nil {
  72. t.Fatalf("create shadow1: %v", err)
  73. }
  74. // Create parent2 and its spark shadow (must NOT appear in parent1's list)
  75. parent2 := &service.Account{
  76. Name: "list-parent2",
  77. Platform: service.PlatformOpenAI,
  78. Type: service.AccountTypeOAuth,
  79. Status: service.StatusActive,
  80. }
  81. if err := repo.Create(ctx, parent2); err != nil {
  82. t.Fatalf("create parent2: %v", err)
  83. }
  84. pid2 := parent2.ID
  85. shadow2 := &service.Account{
  86. Name: "shadow2",
  87. Platform: service.PlatformOpenAI,
  88. Type: service.AccountTypeOAuth,
  89. Status: service.StatusActive,
  90. ParentAccountID: &pid2,
  91. QuotaDimension: service.QuotaDimensionSpark,
  92. }
  93. if err := repo.Create(ctx, shadow2); err != nil {
  94. t.Fatalf("create shadow2: %v", err)
  95. }
  96. // Create 1 unrelated normal account (no parent, global dimension)
  97. unrelated := &service.Account{
  98. Name: "unrelated",
  99. Platform: service.PlatformOpenAI,
  100. Type: service.AccountTypeOAuth,
  101. Status: service.StatusActive,
  102. }
  103. if err := repo.Create(ctx, unrelated); err != nil {
  104. t.Fatalf("create unrelated: %v", err)
  105. }
  106. // Assert ListShadowsByParent returns exactly 1 for parent1
  107. got, err := repo.ListShadowsByParent(ctx, pid1)
  108. if err != nil {
  109. t.Fatalf("ListShadowsByParent: %v", err)
  110. }
  111. if len(got) != 1 {
  112. t.Fatalf("expected 1 spark shadow for parent1, got %d", len(got))
  113. }
  114. acc := got[0]
  115. if acc.ParentAccountID == nil || *acc.ParentAccountID != pid1 {
  116. t.Errorf("unexpected ParentAccountID: %v", acc.ParentAccountID)
  117. }
  118. if acc.QuotaDimension != service.QuotaDimensionSpark {
  119. t.Errorf("unexpected QuotaDimension: %q", acc.QuotaDimension)
  120. }
  121. if acc.ID != shadow1.ID {
  122. t.Errorf("expected shadow1.ID=%d, got %d", shadow1.ID, acc.ID)
  123. }
  124. }