Feat: memory database for testing
This commit is contained in:
parent
f46b52b6ba
commit
8dd411f5d4
2 changed files with 18 additions and 9 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/HFO4/cloudreve/pkg/conf"
|
"github.com/HFO4/cloudreve/pkg/conf"
|
||||||
"github.com/HFO4/cloudreve/pkg/util"
|
"github.com/HFO4/cloudreve/pkg/util"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -22,14 +23,21 @@ func Init() {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
if conf.DatabaseConfig.Type == "UNSET" {
|
|
||||||
db, err = gorm.Open("sqlite3", "cloudreve.db")
|
if gin.Mode() == gin.TestMode {
|
||||||
|
// 测试模式下,使用内存数据库
|
||||||
|
db, err = gorm.Open("sqlite3", ":memory:")
|
||||||
} else {
|
} else {
|
||||||
db, err = gorm.Open(conf.DatabaseConfig.Type, fmt.Sprintf("%s:%s@(%s)/%s?charset=utf8&parseTime=True&loc=Local",
|
if conf.DatabaseConfig.Type == "UNSET" {
|
||||||
conf.DatabaseConfig.User,
|
// 未指定数据库时,使用Sqlite
|
||||||
conf.DatabaseConfig.Password,
|
db, err = gorm.Open("sqlite3", "cloudreve.db")
|
||||||
conf.DatabaseConfig.Host,
|
} else {
|
||||||
conf.DatabaseConfig.Name))
|
db, err = gorm.Open(conf.DatabaseConfig.Type, fmt.Sprintf("%s:%s@(%s)/%s?charset=utf8&parseTime=True&loc=Local",
|
||||||
|
conf.DatabaseConfig.User,
|
||||||
|
conf.DatabaseConfig.Password,
|
||||||
|
conf.DatabaseConfig.Host,
|
||||||
|
conf.DatabaseConfig.Name))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理表前缀
|
// 处理表前缀
|
||||||
|
|
|
@ -22,6 +22,9 @@ var mock sqlmock.Sqlmock
|
||||||
|
|
||||||
// TestMain 初始化数据库Mock
|
// TestMain 初始化数据库Mock
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
|
// 设置gin为测试模式
|
||||||
|
gin.SetMode(gin.TestMode)
|
||||||
|
|
||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
var err error
|
var err error
|
||||||
db, mock, err = sqlmock.New()
|
db, mock, err = sqlmock.New()
|
||||||
|
@ -31,8 +34,6 @@ func TestMain(m *testing.M) {
|
||||||
model.DB, _ = gorm.Open("mysql", db)
|
model.DB, _ = gorm.Open("mysql", db)
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
// 设置gin为测试模式
|
|
||||||
gin.SetMode(gin.TestMode)
|
|
||||||
m.Run()
|
m.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue