2024-11-14 14:55:43 +00:00
|
|
|
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{
|
2024-11-16 15:59:15 +00:00
|
|
|
Server: struct {
|
|
|
|
Port string `yaml:"port"`
|
|
|
|
Mode string `yaml:"mode"`
|
|
|
|
}{
|
2024-11-14 14:55:43 +00:00
|
|
|
Port: "8080",
|
|
|
|
Mode: "test",
|
|
|
|
},
|
|
|
|
Database: DatabaseConfig{
|
|
|
|
Path: ":memory:",
|
|
|
|
},
|
|
|
|
JWT: JWTConfig{
|
|
|
|
Secret: "test-secret",
|
|
|
|
Expire: "24h",
|
|
|
|
},
|
2024-11-16 15:59:15 +00:00
|
|
|
Security: SecurityConfig{
|
|
|
|
EncryptKey: "test-32-byte-encrypt-key-here1234567",
|
2024-11-14 14:55:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|