Cloudreve/routers/main_test.go

48 lines
799 B
Go
Raw Normal View History

2019-11-23 02:09:46 -05:00
package routers
import (
"database/sql"
"testing"
2019-11-23 02:09:46 -05:00
"github.com/DATA-DOG/go-sqlmock"
model "github.com/cloudreve/Cloudreve/v3/models"
2019-11-23 02:09:46 -05:00
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
)
var mock sqlmock.Sqlmock
var memDB *gorm.DB
2019-11-30 10:22:57 -05:00
var mockDB *gorm.DB
2019-11-23 02:09:46 -05:00
// TestMain 初始化数据库Mock
func TestMain(m *testing.M) {
// 设置gin为测试模式
gin.SetMode(gin.TestMode)
// 初始化sqlmock
var db *sql.DB
var err error
db, mock, err = sqlmock.New()
if err != nil {
panic("An error was not expected when opening a stub database connection")
}
// 初始话内存数据库
model.Init()
memDB = model.DB
2019-11-30 10:22:57 -05:00
mockDB, _ = gorm.Open("mysql", db)
model.DB = memDB
2019-11-23 02:09:46 -05:00
defer db.Close()
m.Run()
}
func switchToMemDB() {
model.DB = memDB
}
2019-11-30 10:22:57 -05:00
func switchToMockDB() {
model.DB = mockDB
}