Skip to content

Commit

Permalink
Add tests for tidb (go-xorm#1207)
Browse files Browse the repository at this point in the history
* add tests for tidb

* ignore select for update tests
  • Loading branch information
lunny authored Feb 1, 2019
1 parent 52b85c2 commit 1bcc254
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
6 changes: 5 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
SA_PASSWORD: yourStrong(!)Password
MSSQL_PID: Developer

- image: pingcap/tidb:v2.1.2

working_directory: /go/src/github.com/go-xorm/xorm
steps:
- checkout
Expand All @@ -52,6 +54,8 @@ jobs:
- run: go test -v -race -db="postgres" -conn_str="dbname=xorm_test sslmode=disable" -schema=xorm -cache=true -coverprofile=coverage5-2.txt -covermode=atomic
- run: go test -v -race -db="mssql" -conn_str="server=localhost;user id=sa;password=yourStrong(!)Password;database=xorm_test" -coverprofile=coverage6-1.txt -covermode=atomic
- run: go test -v -race -db="mssql" -conn_str="server=localhost;user id=sa;password=yourStrong(!)Password;database=xorm_test" -cache=true -coverprofile=coverage6-2.txt -covermode=atomic
- run: gocovmerge coverage1-1.txt coverage1-2.txt coverage2-1.txt coverage2-2.txt coverage3-1.txt coverage3-2.txt coverage4-1.txt coverage4-2.txt coverage5-1.txt coverage5-2.txt coverage6-1.txt coverage6-2.txt > coverage.txt
- run: go test -v -race -db="mysql" -conn_str="root:@tcp(localhost:4000)/xorm_test" -ignore_select_update=true -coverprofile=coverage7-1.txt -covermode=atomic
- run: go test -v -race -db="mysql" -conn_str="root:@tcp(localhost:4000)/xorm_test" -ignore_select_update=true -cache=true -coverprofile=coverage7-2.txt -covermode=atomic
- run: gocovmerge coverage1-1.txt coverage1-2.txt coverage2-1.txt coverage2-2.txt coverage3-1.txt coverage3-2.txt coverage4-1.txt coverage4-2.txt coverage5-1.txt coverage5-2.txt coverage6-1.txt coverage6-2.txt coverage7-1.txt coverage7-2.txt > coverage.txt

- run: bash <(curl -s https://codecov.io/bash)
2 changes: 1 addition & 1 deletion session_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func setupForUpdate(engine EngineInterface) error {
}

func TestForUpdate(t *testing.T) {
if testEngine.Dialect().DriverName() != "mysql" && testEngine.Dialect().DriverName() != "mymysql" {
if *ignoreSelectUpdate {
return
}

Expand Down
1 change: 1 addition & 0 deletions test_tidb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go test -db=mysql -conn_str="root:@tcp(localhost:4000)/xorm_test" -ignore_select_update=true
53 changes: 31 additions & 22 deletions xorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,38 @@ var (
dbType string
connString string

db = flag.String("db", "sqlite3", "the tested database")
showSQL = flag.Bool("show_sql", true, "show generated SQLs")
ptrConnStr = flag.String("conn_str", "./test.db?cache=shared&mode=rwc", "test database connection string")
mapType = flag.String("map_type", "snake", "indicate the name mapping")
cache = flag.Bool("cache", false, "if enable cache")
cluster = flag.Bool("cluster", false, "if this is a cluster")
splitter = flag.String("splitter", ";", "the splitter on connstr for cluster")
schema = flag.String("schema", "", "specify the schema")
db = flag.String("db", "sqlite3", "the tested database")
showSQL = flag.Bool("show_sql", true, "show generated SQLs")
ptrConnStr = flag.String("conn_str", "./test.db?cache=shared&mode=rwc", "test database connection string")
mapType = flag.String("map_type", "snake", "indicate the name mapping")
cache = flag.Bool("cache", false, "if enable cache")
cluster = flag.Bool("cluster", false, "if this is a cluster")
splitter = flag.String("splitter", ";", "the splitter on connstr for cluster")
schema = flag.String("schema", "", "specify the schema")
ignoreSelectUpdate = flag.Bool("ignore_select_update", false, "ignore select update if implementation difference, only for tidb")
)

func createEngine(dbType, connStr string) error {
if testEngine == nil {
var err error

if !*cluster {
// create databases if not exist
var db *sql.DB
var err error
if strings.ToLower(dbType) != core.MSSQL {
db, err = sql.Open(dbType, connStr)
} else {
db, err = sql.Open(dbType, strings.Replace(connStr, "xorm_test", "master", -1))
}

if err != nil {
return err
}

switch strings.ToLower(dbType) {
case core.MSSQL:
db, err := sql.Open(dbType, strings.Replace(connStr, "xorm_test", "master", -1))
if err != nil {
return err
}
if _, err = db.Exec("If(db_id(N'xorm_test') IS NULL) BEGIN CREATE DATABASE xorm_test; END;"); err != nil {
return fmt.Errorf("db.Exec: %v", err)
}
db.Close()
*ignoreSelectUpdate = true
case core.POSTGRES:
db, err := sql.Open(dbType, connStr)
if err != nil {
return err
}
rows, err := db.Query(fmt.Sprintf("SELECT 1 FROM pg_database WHERE datname = 'xorm_test'"))
if err != nil {
return fmt.Errorf("db.Query: %v", err)
Expand All @@ -76,16 +74,27 @@ func createEngine(dbType, connStr string) error {
return fmt.Errorf("CREATE SCHEMA: %v", err)
}
}
db.Close()
*ignoreSelectUpdate = true
case core.MYSQL:
db, err := sql.Open(dbType, strings.Replace(connStr, "xorm_test", "mysql", -1))
if err != nil {
return err
}
if _, err = db.Exec("CREATE DATABASE IF NOT EXISTS xorm_test"); err != nil {
return fmt.Errorf("db.Exec: %v", err)
}
db.Close()
default:
*ignoreSelectUpdate = true
}
db.Close()

testEngine, err = NewEngine(dbType, connStr)
} else {
testEngine, err = NewEngineGroup(dbType, strings.Split(connStr, *splitter))
if dbType != "mysql" && dbType != "mymysql" {
*ignoreSelectUpdate = true
}
}
if err != nil {
return err
Expand Down

0 comments on commit 1bcc254

Please sign in to comment.