69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
package utils
|
|
|
|
import (
|
|
"licserver/internal/model"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// TestDB 创建测试数据库连接
|
|
func TestDB(t *testing.T) *gorm.DB {
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
assert.NoError(t, err)
|
|
|
|
// 迁移测试表
|
|
err = db.AutoMigrate(
|
|
&model.User{},
|
|
&model.Device{},
|
|
&model.DeviceModel{},
|
|
&model.LicenseCode{},
|
|
&model.LicenseLog{},
|
|
&model.AccessToken{},
|
|
&model.TokenLog{},
|
|
&model.Captcha{},
|
|
&model.PasswordResetToken{},
|
|
&model.FileUpload{},
|
|
&model.UploadChunk{},
|
|
)
|
|
assert.NoError(t, err)
|
|
|
|
return db
|
|
}
|
|
|
|
// TestConfig 创建测试配置
|
|
func TestConfig() *Config {
|
|
return &Config{
|
|
Server: ServerConfig{
|
|
Port: "8080",
|
|
Mode: "test",
|
|
},
|
|
Database: DatabaseConfig{
|
|
Type: "sqlite3",
|
|
Path: ":memory:",
|
|
},
|
|
JWT: JWTConfig{
|
|
Secret: "test-secret",
|
|
Expire: "24h",
|
|
},
|
|
Email: EmailConfig{
|
|
Host: "smtp.example.com",
|
|
Port: 587,
|
|
Username: "test@example.com",
|
|
Password: "test-password",
|
|
},
|
|
Upload: UploadConfig{
|
|
Path: "./test-uploads",
|
|
},
|
|
Site: SiteConfig{
|
|
Title: "Test Site",
|
|
Description: "Test Description",
|
|
BaseURL: "http://localhost:8080",
|
|
ICP: "Test ICP",
|
|
Copyright: "Test Copyright",
|
|
},
|
|
}
|
|
}
|