fix(test): 修复 CI 测试和 lint 错误
- 为所有 mock 实现添加 GetByIDs 方法以满足 AccountRepository 接口 - 重构 account_handler.go 中的类型断言,使用类型安全的变量 - 修复 gofmt 格式问题
This commit is contained in:
@@ -1099,12 +1099,8 @@ func (h *AccountHandler) BatchRefreshTier(c *gin.Context) {
|
|||||||
g.SetLimit(maxConcurrency)
|
g.SetLimit(maxConcurrency)
|
||||||
|
|
||||||
var mu sync.Mutex
|
var mu sync.Mutex
|
||||||
results := gin.H{
|
var successCount, failedCount int
|
||||||
"total": len(accounts),
|
var errors []gin.H
|
||||||
"success": 0,
|
|
||||||
"failed": 0,
|
|
||||||
"errors": []gin.H{},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, account := range accounts {
|
for _, account := range accounts {
|
||||||
acc := account // 闭包捕获
|
acc := account // 闭包捕获
|
||||||
@@ -1112,8 +1108,8 @@ func (h *AccountHandler) BatchRefreshTier(c *gin.Context) {
|
|||||||
_, extra, creds, err := h.geminiOAuthService.RefreshAccountGoogleOneTier(gctx, acc)
|
_, extra, creds, err := h.geminiOAuthService.RefreshAccountGoogleOneTier(gctx, acc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
results["failed"] = results["failed"].(int) + 1
|
failedCount++
|
||||||
results["errors"] = append(results["errors"].([]gin.H), gin.H{
|
errors = append(errors, gin.H{
|
||||||
"account_id": acc.ID,
|
"account_id": acc.ID,
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
})
|
})
|
||||||
@@ -1128,13 +1124,13 @@ func (h *AccountHandler) BatchRefreshTier(c *gin.Context) {
|
|||||||
|
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
if updateErr != nil {
|
if updateErr != nil {
|
||||||
results["failed"] = results["failed"].(int) + 1
|
failedCount++
|
||||||
results["errors"] = append(results["errors"].([]gin.H), gin.H{
|
errors = append(errors, gin.H{
|
||||||
"account_id": acc.ID,
|
"account_id": acc.ID,
|
||||||
"error": updateErr.Error(),
|
"error": updateErr.Error(),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
results["success"] = results["success"].(int) + 1
|
successCount++
|
||||||
}
|
}
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
||||||
@@ -1147,5 +1143,12 @@ func (h *AccountHandler) BatchRefreshTier(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
results := gin.H{
|
||||||
|
"total": len(accounts),
|
||||||
|
"success": successCount,
|
||||||
|
"failed": failedCount,
|
||||||
|
"errors": errors,
|
||||||
|
}
|
||||||
|
|
||||||
response.Success(c, results)
|
response.Success(c, results)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -112,10 +111,12 @@ func (c *driveClient) GetStorageQuota(ctx context.Context, accessToken, proxyURL
|
|||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
body, _ := io.ReadAll(resp.Body)
|
|
||||||
_ = resp.Body.Close()
|
_ = resp.Body.Close()
|
||||||
// 记录完整错误
|
statusText := http.StatusText(resp.StatusCode)
|
||||||
fmt.Printf("[DriveClient] API error (status %d): %s\n", resp.StatusCode, string(body))
|
if statusText == "" {
|
||||||
|
statusText = resp.Status
|
||||||
|
}
|
||||||
|
fmt.Printf("[DriveClient] Drive API error: status=%d, msg=%s\n", resp.StatusCode, statusText)
|
||||||
// 只返回通用错误
|
// 只返回通用错误
|
||||||
return nil, fmt.Errorf("drive API error: status %d", resp.StatusCode)
|
return nil, fmt.Errorf("drive API error: status %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,4 +16,3 @@ func TestDriveStorageInfo(t *testing.T) {
|
|||||||
t.Errorf("Expected usage 50GB, got %d", info.Usage)
|
t.Errorf("Expected usage 50GB, got %d", info.Usage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,10 @@ func (s *accountRepoStub) GetByID(ctx context.Context, id int64) (*Account, erro
|
|||||||
panic("unexpected GetByID call")
|
panic("unexpected GetByID call")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *accountRepoStub) GetByIDs(ctx context.Context, ids []int64) ([]*Account, error) {
|
||||||
|
panic("unexpected GetByIDs call")
|
||||||
|
}
|
||||||
|
|
||||||
// ExistsByID 返回预设的存在性检查结果。
|
// ExistsByID 返回预设的存在性检查结果。
|
||||||
// 这是 Delete 方法调用的第一个仓储方法,用于验证账号是否存在。
|
// 这是 Delete 方法调用的第一个仓储方法,用于验证账号是否存在。
|
||||||
func (s *accountRepoStub) ExistsByID(ctx context.Context, id int64) (bool, error) {
|
func (s *accountRepoStub) ExistsByID(ctx context.Context, id int64) (bool, error) {
|
||||||
|
|||||||
@@ -32,6 +32,16 @@ func (m *mockAccountRepoForPlatform) GetByID(ctx context.Context, id int64) (*Ac
|
|||||||
return nil, errors.New("account not found")
|
return nil, errors.New("account not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *mockAccountRepoForPlatform) GetByIDs(ctx context.Context, ids []int64) ([]*Account, error) {
|
||||||
|
var result []*Account
|
||||||
|
for _, id := range ids {
|
||||||
|
if acc, ok := m.accountsByID[id]; ok {
|
||||||
|
result = append(result, acc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *mockAccountRepoForPlatform) ExistsByID(ctx context.Context, id int64) (bool, error) {
|
func (m *mockAccountRepoForPlatform) ExistsByID(ctx context.Context, id int64) (bool, error) {
|
||||||
if m.accountsByID == nil {
|
if m.accountsByID == nil {
|
||||||
return false, nil
|
return false, nil
|
||||||
|
|||||||
@@ -25,6 +25,16 @@ func (m *mockAccountRepoForGemini) GetByID(ctx context.Context, id int64) (*Acco
|
|||||||
return nil, errors.New("account not found")
|
return nil, errors.New("account not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *mockAccountRepoForGemini) GetByIDs(ctx context.Context, ids []int64) ([]*Account, error) {
|
||||||
|
var result []*Account
|
||||||
|
for _, id := range ids {
|
||||||
|
if acc, ok := m.accountsByID[id]; ok {
|
||||||
|
result = append(result, acc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *mockAccountRepoForGemini) ExistsByID(ctx context.Context, id int64) (bool, error) {
|
func (m *mockAccountRepoForGemini) ExistsByID(ctx context.Context, id int64) (bool, error) {
|
||||||
if m.accountsByID == nil {
|
if m.accountsByID == nil {
|
||||||
return false, nil
|
return false, nil
|
||||||
|
|||||||
@@ -49,4 +49,3 @@ func TestInferGoogleOneTier(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user