account_quota_schedulable_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //go:build unit
  2. package service
  3. import (
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestAccountIsSchedulable_QuotaExceeded(t *testing.T) {
  9. now := time.Now()
  10. tests := []struct {
  11. name string
  12. account *Account
  13. want bool
  14. }{
  15. {
  16. name: "apikey daily quota exceeded",
  17. account: &Account{
  18. Status: StatusActive,
  19. Schedulable: true,
  20. Type: AccountTypeAPIKey,
  21. Extra: map[string]any{
  22. "quota_daily_limit": 10.0,
  23. "quota_daily_used": 10.0,
  24. "quota_daily_start": now.Add(-1 * time.Hour).Format(time.RFC3339),
  25. },
  26. },
  27. want: false,
  28. },
  29. {
  30. name: "apikey weekly quota exceeded",
  31. account: &Account{
  32. Status: StatusActive,
  33. Schedulable: true,
  34. Type: AccountTypeAPIKey,
  35. Extra: map[string]any{
  36. "quota_weekly_limit": 50.0,
  37. "quota_weekly_used": 50.0,
  38. "quota_weekly_start": now.Add(-2 * 24 * time.Hour).Format(time.RFC3339),
  39. },
  40. },
  41. want: false,
  42. },
  43. {
  44. name: "apikey total quota exceeded",
  45. account: &Account{
  46. Status: StatusActive,
  47. Schedulable: true,
  48. Type: AccountTypeAPIKey,
  49. Extra: map[string]any{
  50. "quota_limit": 100.0,
  51. "quota_used": 100.0,
  52. },
  53. },
  54. want: false,
  55. },
  56. {
  57. name: "apikey quota not exceeded",
  58. account: &Account{
  59. Status: StatusActive,
  60. Schedulable: true,
  61. Type: AccountTypeAPIKey,
  62. Extra: map[string]any{
  63. "quota_daily_limit": 10.0,
  64. "quota_daily_used": 5.0,
  65. "quota_daily_start": now.Add(-1 * time.Hour).Format(time.RFC3339),
  66. },
  67. },
  68. want: true,
  69. },
  70. {
  71. name: "apikey expired daily period restores schedulable",
  72. account: &Account{
  73. Status: StatusActive,
  74. Schedulable: true,
  75. Type: AccountTypeAPIKey,
  76. Extra: map[string]any{
  77. "quota_daily_limit": 10.0,
  78. "quota_daily_used": 10.0,
  79. "quota_daily_start": now.Add(-25 * time.Hour).Format(time.RFC3339),
  80. },
  81. },
  82. want: true,
  83. },
  84. {
  85. name: "oauth ignores quota exceeded",
  86. account: &Account{
  87. Status: StatusActive,
  88. Schedulable: true,
  89. Type: AccountTypeOAuth,
  90. Extra: map[string]any{
  91. "quota_daily_limit": 10.0,
  92. "quota_daily_used": 10.0,
  93. "quota_daily_start": now.Add(-1 * time.Hour).Format(time.RFC3339),
  94. },
  95. },
  96. want: true,
  97. },
  98. {
  99. name: "bedrock quota exceeded",
  100. account: &Account{
  101. Status: StatusActive,
  102. Schedulable: true,
  103. Type: AccountTypeBedrock,
  104. Extra: map[string]any{
  105. "quota_limit": 200.0,
  106. "quota_used": 200.0,
  107. },
  108. },
  109. want: false,
  110. },
  111. }
  112. for _, tt := range tests {
  113. t.Run(tt.name, func(t *testing.T) {
  114. require.Equal(t, tt.want, tt.account.IsSchedulable())
  115. })
  116. }
  117. }