You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package routers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
|
|
model "github.com/HFO4/cloudreve/models"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var mock sqlmock.Sqlmock
|
|
|
|
var memDB *gorm.DB
|
|
|
|
var mockDB *gorm.DB
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
mockDB, _ = gorm.Open("mysql", db)
|
|
|
|
model.DB = memDB
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
m.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func switchToMemDB() {
|
|
|
|
model.DB = memDB
|
|
|
|
}
|
|
|
|
|
|
|
|
func switchToMockDB() {
|
|
|
|
model.DB = mockDB
|
|
|
|
}
|