定义静音日志对象
This commit is contained in:
@@ -17,8 +17,6 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
"go.uber.org/zap"
|
|
||||||
"go.uber.org/zap/zapcore"
|
|
||||||
"gorm.io/datatypes"
|
"gorm.io/datatypes"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
@@ -82,17 +80,6 @@ func (m *MockDeviceRepository) Delete(id uint) error {
|
|||||||
return args.Error(0)
|
return args.Error(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getSilentLogger 创建一个不输出日志的 logs.Logger 实例,用于测试
|
|
||||||
func getSilentLogger() *logs.Logger {
|
|
||||||
discardSyncer := zapcore.AddSync(io.Discard)
|
|
||||||
encoderConfig := zap.NewProductionEncoderConfig()
|
|
||||||
encoder := zapcore.NewConsoleEncoder(encoderConfig)
|
|
||||||
core := zapcore.NewCore(encoder, discardSyncer, zap.DebugLevel)
|
|
||||||
zapLogger := zap.New(core)
|
|
||||||
sugaredLogger := zapLogger.Sugar()
|
|
||||||
return &logs.Logger{SugaredLogger: sugaredLogger}
|
|
||||||
}
|
|
||||||
|
|
||||||
// testCase 结构体定义了所有测试用例的通用参数
|
// testCase 结构体定义了所有测试用例的通用参数
|
||||||
type testCase struct {
|
type testCase struct {
|
||||||
name string
|
name string
|
||||||
@@ -309,7 +296,7 @@ func TestCreateDevice(t *testing.T) {
|
|||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
runTest(t, tc, func(ctx *gin.Context, repo *MockDeviceRepository) {
|
runTest(t, tc, func(ctx *gin.Context, repo *MockDeviceRepository) {
|
||||||
device.NewController(repo, getSilentLogger()).CreateDevice(ctx)
|
device.NewController(repo, logs.NewSilentLogger()).CreateDevice(ctx)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -394,7 +381,7 @@ func TestGetDevice(t *testing.T) {
|
|||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
runTest(t, tc, func(ctx *gin.Context, repo *MockDeviceRepository) {
|
runTest(t, tc, func(ctx *gin.Context, repo *MockDeviceRepository) {
|
||||||
device.NewController(repo, getSilentLogger()).GetDevice(ctx)
|
device.NewController(repo, logs.NewSilentLogger()).GetDevice(ctx)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -492,7 +479,7 @@ func TestListDevices(t *testing.T) {
|
|||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
runTest(t, tc, func(ctx *gin.Context, repo *MockDeviceRepository) {
|
runTest(t, tc, func(ctx *gin.Context, repo *MockDeviceRepository) {
|
||||||
device.NewController(repo, getSilentLogger()).ListDevices(ctx)
|
device.NewController(repo, logs.NewSilentLogger()).ListDevices(ctx)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -681,7 +668,7 @@ func TestUpdateDevice(t *testing.T) {
|
|||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
runTest(t, tc, func(ctx *gin.Context, repo *MockDeviceRepository) {
|
runTest(t, tc, func(ctx *gin.Context, repo *MockDeviceRepository) {
|
||||||
device.NewController(repo, getSilentLogger()).UpdateDevice(ctx)
|
device.NewController(repo, logs.NewSilentLogger()).UpdateDevice(ctx)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -747,7 +734,7 @@ func TestDeleteDevice(t *testing.T) {
|
|||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
runTest(t, tc, func(ctx *gin.Context, repo *MockDeviceRepository) {
|
runTest(t, tc, func(ctx *gin.Context, repo *MockDeviceRepository) {
|
||||||
device.NewController(repo, getSilentLogger()).DeleteDevice(ctx)
|
device.NewController(repo, logs.NewSilentLogger()).DeleteDevice(ctx)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,8 +17,6 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
"go.uber.org/zap"
|
|
||||||
"go.uber.org/zap/zapcore"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -77,13 +75,7 @@ func TestCreateUser(t *testing.T) {
|
|||||||
gin.SetMode(gin.TestMode) // 设置 Gin 为测试模式
|
gin.SetMode(gin.TestMode) // 设置 Gin 为测试模式
|
||||||
|
|
||||||
// 创建一个不输出日志的真实 logs.Logger 实例
|
// 创建一个不输出日志的真实 logs.Logger 实例
|
||||||
discardSyncer := zapcore.AddSync(io.Discard)
|
silentLogger := logs.NewSilentLogger()
|
||||||
encoderConfig := zap.NewProductionEncoderConfig()
|
|
||||||
encoder := zapcore.NewConsoleEncoder(encoderConfig)
|
|
||||||
core := zapcore.NewCore(encoder, discardSyncer, zap.DebugLevel) // 设置为 DebugLevel 以确保所有日志都被处理(并丢弃)
|
|
||||||
zapLogger := zap.New(core)
|
|
||||||
sugaredLogger := zapLogger.Sugar()
|
|
||||||
silentLogger := &logs.Logger{SugaredLogger: sugaredLogger}
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -248,13 +240,7 @@ func TestLogin(t *testing.T) {
|
|||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
|
||||||
// 创建一个不输出日志的真实 logs.Logger 实例
|
// 创建一个不输出日志的真实 logs.Logger 实例
|
||||||
discardSyncer := zapcore.AddSync(io.Discard)
|
silentLogger := logs.NewSilentLogger()
|
||||||
encoderConfig := zap.NewProductionEncoderConfig()
|
|
||||||
encoder := zapcore.NewConsoleEncoder(encoderConfig)
|
|
||||||
core := zapcore.NewCore(encoder, discardSyncer, zap.DebugLevel) // 设置为 DebugLevel 以确保所有日志都被处理(并丢弃)
|
|
||||||
zapLogger := zap.New(core)
|
|
||||||
sugaredLogger := zapLogger.Sugar()
|
|
||||||
silentLogger := &logs.Logger{SugaredLogger: sugaredLogger}
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package logs
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -163,3 +164,15 @@ func (g *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql
|
|||||||
g.ZapLogger.With(fields...).Debugf("[GORM] trace")
|
g.ZapLogger.With(fields...).Debugf("[GORM] trace")
|
||||||
// --- 逻辑修复结束 ---
|
// --- 逻辑修复结束 ---
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewSilentLogger 创建一个不输出任何日志的 Logger 实例, 用于测试中屏蔽日志
|
||||||
|
func NewSilentLogger() *Logger {
|
||||||
|
// 创建一个不输出日志的真实 logs.Logger 实例
|
||||||
|
discardSyncer := zapcore.AddSync(io.Discard)
|
||||||
|
encoderConfig := zap.NewProductionEncoderConfig()
|
||||||
|
encoder := zapcore.NewConsoleEncoder(encoderConfig)
|
||||||
|
core := zapcore.NewCore(encoder, discardSyncer, zap.DebugLevel) // 设置为 DebugLevel 以确保所有日志都被处理(并丢弃)
|
||||||
|
zapLogger := zap.New(core)
|
||||||
|
sugaredLogger := zapLogger.Sugar()
|
||||||
|
return &Logger{SugaredLogger: sugaredLogger}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user