xorm/xorm_test.go

122 lines
2.4 KiB
Go
Raw Normal View History

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"
2017-05-27 09:31:39 +00:00
"strings"
2017-03-23 06:05:32 +00:00
"testing"
_ "github.com/denisenkom/go-mssqldb"
2017-04-10 15:10:59 +00:00
_ "github.com/go-sql-driver/mysql"
"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"
2017-07-25 09:51:20 +00:00
_ "github.com/ziutek/mymysql/godrv"
2017-03-23 06:05:32 +00:00
)
var (
testEngine *Engine
2017-05-27 09:31:39 +00:00
dbType string
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")
2017-09-10 06:29:02 +00:00
ptrConnStr = flag.String("conn_str", "./test.db?cache=shared&mode=rwc", "test database connection string")
2017-04-10 15:10:59 +00:00
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)
testEngine.logger.SetLevel(core.LOG_DEBUG)
if *cache {
cacher := NewLRUCacher(NewMemoryStore(), 100000)
testEngine.SetDefaultCacher(cacher)
}
if len(*mapType) > 0 {
switch *mapType {
case "snake":
testEngine.SetMapper(core.SnakeMapper{})
case "same":
testEngine.SetMapper(core.SameMapper{})
case "gonic":
testEngine.SetMapper(core.LintGonicMapper)
}
}
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)
}
if err = testEngine.DropTables(tableNames...); err != nil {
return err
}
return nil
2017-03-23 06:05:32 +00:00
}
2017-04-10 15:10:59 +00:00
func prepareEngine() error {
2017-05-27 09:31:39 +00:00
return createEngine(dbType, connString)
2017-04-10 15:10:59 +00:00
}
2017-03-23 06:05:32 +00:00
func TestMain(m *testing.M) {
flag.Parse()
2017-05-27 09:31:39 +00:00
dbType = *db
2017-04-10 15:10:59 +00:00
if *db == "sqlite3" {
if ptrConnStr == nil {
connString = "./test.db?cache=shared&mode=rwc"
2017-04-10 15:10:59 +00:00
} 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
}
dbs := strings.Split(*db, "::")
conns := strings.Split(connString, "::")
2017-05-27 09:31:39 +00:00
var res int
for i := 0; i < len(dbs); i++ {
dbType = dbs[i]
connString = conns[i]
testEngine = nil
fmt.Println("testing", dbType, connString)
2017-05-27 09:31:39 +00:00
if err := prepareEngine(); err != nil {
fmt.Println(err)
return
}
code := m.Run()
if code > 0 {
res = code
}
2017-03-23 06:05:32 +00:00
}
2017-05-27 09:31:39 +00:00
os.Exit(res)
2017-03-23 06:05:32 +00:00
}
func TestPing(t *testing.T) {
if err := testEngine.Ping(); err != nil {
t.Fatal(err)
}
}