Просмотр исходного кода

fix: 清理渠道选择删除后的代码质量问题

- router/api-router.go: 修复删除路由后的缩进异常
- model/channel_pricing.go: 清理 Insert/Update/Delete 中遗留空行
- controller/relay_test.go: newTestContext 接收 *testing.T 参数并调用 t.Helper()
master
fengsilin 3 недель назад
Родитель
Сommit
c64afdc6c2
3 измененных файлов: 17 добавлений и 19 удалений
  1. +15
    -14
      controller/relay_test.go
  2. +0
    -3
      model/channel_pricing.go
  3. +2
    -2
      router/api-router.go

+ 15
- 14
controller/relay_test.go Просмотреть файл

@@ -16,7 +16,8 @@ import (
// shouldRetry — 渠道选择删除后的验证测试
// ---------------------------------------------------------------------------

func newTestContext() *gin.Context {
func newTestContext(t *testing.T) *gin.Context {
t.Helper()
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
@@ -25,7 +26,7 @@ func newTestContext() *gin.Context {
}

func TestShouldRetry_NilError_ReturnsFalse(t *testing.T) {
c := newTestContext()
c := newTestContext(t)
assert.False(t, shouldRetry(c, nil, 3))
}

@@ -33,7 +34,7 @@ func TestShouldRetry_5xxErrorWithRetryLeft_ReturnsTrue(t *testing.T) {
// 渠道选择删除后,所有请求都能正常重试
// 之前设置了 specific_channel_id 时 shouldRetry 会返回 false
// 现在即使不设置任何 channel 相关 context,5xx 也应该重试
c := newTestContext()
c := newTestContext(t)
apiErr := types.NewErrorWithStatusCode(
errors.New("upstream error"),
types.ErrorCodeBadResponseStatusCode,
@@ -43,7 +44,7 @@ func TestShouldRetry_5xxErrorWithRetryLeft_ReturnsTrue(t *testing.T) {
}

func TestShouldRetry_5xxErrorZeroRetryLeft_ReturnsFalse(t *testing.T) {
c := newTestContext()
c := newTestContext(t)
apiErr := types.NewErrorWithStatusCode(
errors.New("upstream error"),
types.ErrorCodeBadResponseStatusCode,
@@ -53,7 +54,7 @@ func TestShouldRetry_5xxErrorZeroRetryLeft_ReturnsFalse(t *testing.T) {
}

func TestShouldRetry_SkipRetryError_ReturnsFalse(t *testing.T) {
c := newTestContext()
c := newTestContext(t)
apiErr := types.NewError(
errors.New("skip retry"),
types.ErrorCodeBadResponseStatusCode,
@@ -64,7 +65,7 @@ func TestShouldRetry_SkipRetryError_ReturnsFalse(t *testing.T) {

func TestShouldRetry_ChannelError_ReturnsTrue(t *testing.T) {
// channel: 前缀的错误码会触发立即重试
c := newTestContext()
c := newTestContext(t)
apiErr := types.NewError(
errors.New("channel error"),
"channel:test",
@@ -74,7 +75,7 @@ func TestShouldRetry_ChannelError_ReturnsTrue(t *testing.T) {
}

func TestShouldRetry_2xxStatus_ReturnsFalse(t *testing.T) {
c := newTestContext()
c := newTestContext(t)
apiErr := types.NewErrorWithStatusCode(
errors.New("ok"),
types.ErrorCodeBadResponseStatusCode,
@@ -84,7 +85,7 @@ func TestShouldRetry_2xxStatus_ReturnsFalse(t *testing.T) {
}

func TestShouldRetry_429Status_ReturnsTrue(t *testing.T) {
c := newTestContext()
c := newTestContext(t)
apiErr := types.NewErrorWithStatusCode(
errors.New("rate limited"),
types.ErrorCodeBadResponseStatusCode,
@@ -98,12 +99,12 @@ func TestShouldRetry_429Status_ReturnsTrue(t *testing.T) {
// ---------------------------------------------------------------------------

func TestShouldRetryTaskRelay_NilError_ReturnsFalse(t *testing.T) {
c := newTestContext()
c := newTestContext(t)
assert.False(t, shouldRetryTaskRelay(c, 1, nil, 3))
}

func TestShouldRetryTaskRelay_5xxWithRetryLeft_ReturnsTrue(t *testing.T) {
c := newTestContext()
c := newTestContext(t)
taskErr := &dto.TaskError{
Error: errors.New("server error"),
StatusCode: http.StatusInternalServerError,
@@ -112,7 +113,7 @@ func TestShouldRetryTaskRelay_5xxWithRetryLeft_ReturnsTrue(t *testing.T) {
}

func TestShouldRetryTaskRelay_5xxZeroRetryLeft_ReturnsFalse(t *testing.T) {
c := newTestContext()
c := newTestContext(t)
taskErr := &dto.TaskError{
Error: errors.New("server error"),
StatusCode: http.StatusInternalServerError,
@@ -121,7 +122,7 @@ func TestShouldRetryTaskRelay_5xxZeroRetryLeft_ReturnsFalse(t *testing.T) {
}

func TestShouldRetryTaskRelay_429_ReturnsTrue(t *testing.T) {
c := newTestContext()
c := newTestContext(t)
taskErr := &dto.TaskError{
Error: errors.New("rate limited"),
StatusCode: http.StatusTooManyRequests,
@@ -130,7 +131,7 @@ func TestShouldRetryTaskRelay_429_ReturnsTrue(t *testing.T) {
}

func TestShouldRetryTaskRelay_400_ReturnsFalse(t *testing.T) {
c := newTestContext()
c := newTestContext(t)
taskErr := &dto.TaskError{
Error: errors.New("bad request"),
StatusCode: http.StatusBadRequest,
@@ -141,7 +142,7 @@ func TestShouldRetryTaskRelay_400_ReturnsFalse(t *testing.T) {
func TestShouldRetryTaskRelay_LocalError_ReturnsFalse(t *testing.T) {
// LocalError + 非 5xx 状态码(如 403)时,LocalError 检查生效返回 false
// 注意:5xx 分支在 LocalError 检查之前,所以 localError+5xx 仍会重试
c := newTestContext()
c := newTestContext(t)
taskErr := &dto.TaskError{
Error: errors.New("local error"),
StatusCode: http.StatusForbidden,


+ 0
- 3
model/channel_pricing.go Просмотреть файл

@@ -81,7 +81,6 @@ func (cp *ChannelPricing) Insert() error {
err := DB.Create(cp).Error
if err == nil {
setCache(getChannelPricingCacheKey(cp.ModelName, cp.ChannelId), cp)

}
return err
}
@@ -95,7 +94,6 @@ func (cp *ChannelPricing) Update() error {
Updates(cp).Error
if err == nil {
setCache(getChannelPricingCacheKey(cp.ModelName, cp.ChannelId), cp)

}
return err
}
@@ -108,7 +106,6 @@ func (cp *ChannelPricing) Delete() error {
err := DB.Delete(cp).Error
if err == nil {
removeCache(getChannelPricingCacheKey(existing.ModelName, existing.ChannelId))

}
return err
}


+ 2
- 2
router/api-router.go Просмотреть файл

@@ -77,7 +77,7 @@ func SetApiRouter(router *gin.Engine) {
selfRoute.GET("/self/groups", controller.GetUserGroups)
selfRoute.GET("/self", controller.GetSelf)
selfRoute.GET("/models", controller.GetUserModels)
selfRoute.PUT("/self", controller.UpdateSelf)
selfRoute.PUT("/self", controller.UpdateSelf)
selfRoute.DELETE("/self", controller.DeleteSelf)
selfRoute.GET("/token", controller.GenerateAccessToken)
selfRoute.GET("/passkey", controller.PasskeyStatus)
@@ -202,7 +202,7 @@ func SetApiRouter(router *gin.Engine) {
channelPricingRoute.POST("/batch", controller.BatchCreateChannelPricing)
channelPricingRoute.POST("/copy_global/:channel_id", controller.CopyGlobalPricing)
channelPricingRoute.DELETE("/:id", controller.DeleteChannelPricing)
}
}

// 用户倍率路由(管理员权限)
userChannelRatioRoute := apiRouter.Group("/user_channel_ratio")


Загрузка…
Отмена
Сохранить