- Deleted the Token model and its repository, transitioning to a Redis-based token management system. - Updated the service layer to utilize Redis for token storage, enhancing performance and scalability. - Refactored the container to remove TokenRepository and integrate the new token service. - Cleaned up the Dockerfile and other files by removing unnecessary whitespace and comments. - Enhanced error handling and logging for Redis initialization and usage.
57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package testutil
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"carrotskin/internal/model"
|
|
"carrotskin/pkg/database"
|
|
|
|
"go.uber.org/zap"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// NewTestDB 返回基于内存的 sqlite 数据库并完成模型迁移
|
|
func NewTestDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
|
|
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("failed to open sqlite memory db: %v", err)
|
|
}
|
|
|
|
if err := db.AutoMigrate(
|
|
&model.User{},
|
|
&model.UserPointLog{},
|
|
&model.UserLoginLog{},
|
|
&model.Profile{},
|
|
&model.Texture{},
|
|
&model.UserTextureFavorite{},
|
|
&model.TextureDownloadLog{},
|
|
&model.Client{},
|
|
&model.Yggdrasil{},
|
|
&model.SystemConfig{},
|
|
&model.AuditLog{},
|
|
&model.CasbinRule{},
|
|
); err != nil {
|
|
t.Fatalf("failed to migrate models: %v", err)
|
|
}
|
|
|
|
return db
|
|
}
|
|
|
|
// NewNoopLogger 返回无输出 logger
|
|
func NewNoopLogger() *zap.Logger {
|
|
return zap.NewNop()
|
|
}
|
|
|
|
// NewTestCache 返回禁用 redis 的缓存管理器(用于单元测试)
|
|
func NewTestCache() *database.CacheManager {
|
|
return database.NewCacheManager(nil, database.CacheConfig{
|
|
Prefix: "test:",
|
|
Expiration: 1 * time.Minute,
|
|
Enabled: false,
|
|
})
|
|
}
|