fix(软删除): 修复删除钩子调用链并跳过无Docker测试
软删除钩子改用 next.Mutate 处理更新,避免 mutation 类型不匹配 集成测试检测 Docker 可用性,无 Docker 自动跳过
This commit is contained in:
@@ -112,9 +112,6 @@ func (d SoftDeleteMixin) Hooks() []ent.Hook {
|
|||||||
SetOp(ent.Op)
|
SetOp(ent.Op)
|
||||||
SetDeletedAt(time.Time)
|
SetDeletedAt(time.Time)
|
||||||
WhereP(...func(*sql.Selector))
|
WhereP(...func(*sql.Selector))
|
||||||
Client() interface {
|
|
||||||
Mutate(context.Context, ent.Mutation) (ent.Value, error)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||||
@@ -125,7 +122,7 @@ func (d SoftDeleteMixin) Hooks() []ent.Hook {
|
|||||||
mx.SetOp(ent.OpUpdate)
|
mx.SetOp(ent.OpUpdate)
|
||||||
// 设置删除时间为当前时间
|
// 设置删除时间为当前时间
|
||||||
mx.SetDeletedAt(time.Now())
|
mx.SetDeletedAt(time.Now())
|
||||||
return mx.Client().Mutate(ctx, m)
|
return next.Mutate(ctx, m)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -88,6 +91,7 @@ func performRequest(router *gin.Engine) *httptest.ResponseRecorder {
|
|||||||
|
|
||||||
func startRedis(t *testing.T, ctx context.Context) *redis.Client {
|
func startRedis(t *testing.T, ctx context.Context) *redis.Client {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
ensureDockerAvailable(t)
|
||||||
|
|
||||||
redisContainer, err := tcredis.Run(ctx, redisImageTag)
|
redisContainer, err := tcredis.Run(ctx, redisImageTag)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -112,3 +116,43 @@ func startRedis(t *testing.T, ctx context.Context) *redis.Client {
|
|||||||
|
|
||||||
return rdb
|
return rdb
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ensureDockerAvailable(t *testing.T) {
|
||||||
|
t.Helper()
|
||||||
|
if dockerAvailable() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Skip("Docker 未启用,跳过依赖 testcontainers 的集成测试")
|
||||||
|
}
|
||||||
|
|
||||||
|
func dockerAvailable() bool {
|
||||||
|
if os.Getenv("DOCKER_HOST") != "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
socketCandidates := []string{
|
||||||
|
"/var/run/docker.sock",
|
||||||
|
filepath.Join(os.Getenv("XDG_RUNTIME_DIR"), "docker.sock"),
|
||||||
|
filepath.Join(userHomeDir(), ".docker", "run", "docker.sock"),
|
||||||
|
filepath.Join(userHomeDir(), ".docker", "desktop", "docker.sock"),
|
||||||
|
filepath.Join("/run/user", strconv.Itoa(os.Getuid()), "docker.sock"),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, socket := range socketCandidates {
|
||||||
|
if socket == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(socket); err == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func userHomeDir() string {
|
||||||
|
home, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return home
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user