2017-03-23 06:05:32 +00:00
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2017-04-10 15:10:59 +00:00
|
|
|
"fmt"
|
2017-03-23 06:05:32 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-10 15:10:59 +00:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2017-05-27 05:50:57 +00:00
|
|
|
"github.com/go-xorm/core"
|
2017-04-10 15:10:59 +00:00
|
|
|
_ "github.com/lib/pq"
|
2017-03-23 06:05:32 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testEngine *Engine
|
2017-04-10 15:10:59 +00:00
|
|
|
connString string
|
2017-03-23 06:05:32 +00:00
|
|
|
|
2017-04-10 15:10:59 +00:00
|
|
|
db = flag.String("db", "sqlite3", "the tested database")
|
|
|
|
showSQL = flag.Bool("show_sql", true, "show generated SQLs")
|
|
|
|
ptrConnStr = flag.String("conn_str", "", "test database connection string")
|
|
|
|
mapType = flag.String("map_type", "snake", "indicate the name mapping")
|
|
|
|
cache = flag.Bool("cache", false, "if enable cache")
|
|
|
|
)
|
2017-03-23 06:05:32 +00:00
|
|
|
|
2017-04-10 15:10:59 +00:00
|
|
|
func createEngine(dbType, connStr string) error {
|
2017-03-27 07:45:15 +00:00
|
|
|
if testEngine == nil {
|
|
|
|
var err error
|
2017-04-10 15:10:59 +00:00
|
|
|
testEngine, err = NewEngine(dbType, connStr)
|
2017-03-27 07:45:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-10 15:10:59 +00:00
|
|
|
|
2017-03-27 07:45:15 +00:00
|
|
|
testEngine.ShowSQL(*showSQL)
|
2017-05-27 05:50:57 +00:00
|
|
|
testEngine.logger.SetLevel(core.LOG_DEBUG)
|
2017-03-27 07:45:15 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 15:10:59 +00:00
|
|
|
tables, err := testEngine.DBMetas()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-03-23 06:05:32 +00:00
|
|
|
}
|
2017-04-10 15:10:59 +00:00
|
|
|
var tableNames = make([]interface{}, 0, len(tables))
|
|
|
|
for _, table := range tables {
|
|
|
|
tableNames = append(tableNames, table.Name)
|
|
|
|
}
|
|
|
|
return testEngine.DropTables(tableNames...)
|
2017-03-23 06:05:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 15:10:59 +00:00
|
|
|
func prepareEngine() error {
|
|
|
|
return createEngine(*db, connString)
|
|
|
|
}
|
2017-03-23 06:05:32 +00:00
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
flag.Parse()
|
|
|
|
|
2017-04-10 15:10:59 +00:00
|
|
|
if *db == "sqlite3" {
|
|
|
|
if ptrConnStr == nil {
|
|
|
|
connString = "./test.db"
|
|
|
|
} else {
|
|
|
|
connString = *ptrConnStr
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ptrConnStr == nil {
|
|
|
|
fmt.Println("you should indicate conn string")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
connString = *ptrConnStr
|
2017-03-23 06:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := prepareEngine(); err != nil {
|
2017-04-10 15:10:59 +00:00
|
|
|
fmt.Println(err)
|
|
|
|
return
|
2017-03-23 06:05:32 +00:00
|
|
|
}
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPing(t *testing.T) {
|
|
|
|
if err := testEngine.Ping(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|