xorm_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2018 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "database/sql"
  7. "flag"
  8. "fmt"
  9. "log"
  10. "os"
  11. "strings"
  12. "testing"
  13. _ "github.com/denisenkom/go-mssqldb"
  14. _ "github.com/go-sql-driver/mysql"
  15. _ "github.com/lib/pq"
  16. _ "github.com/mattn/go-sqlite3"
  17. "github.com/xormplus/core"
  18. _ "github.com/ziutek/mymysql/godrv"
  19. )
  20. var (
  21. testEngine EngineInterface
  22. dbType string
  23. connString string
  24. db = flag.String("db", "sqlite3", "the tested database")
  25. showSQL = flag.Bool("show_sql", true, "show generated SQLs")
  26. ptrConnStr = flag.String("conn_str", "./test.db?cache=shared&mode=rwc", "test database connection string")
  27. mapType = flag.String("map_type", "snake", "indicate the name mapping")
  28. cache = flag.Bool("cache", false, "if enable cache")
  29. cluster = flag.Bool("cluster", false, "if this is a cluster")
  30. splitter = flag.String("splitter", ";", "the splitter on connstr for cluster")
  31. schema = flag.String("schema", "", "specify the schema")
  32. )
  33. func createEngine(dbType, connStr string) error {
  34. if testEngine == nil {
  35. var err error
  36. if !*cluster {
  37. // create databases if not exist
  38. var db *sql.DB
  39. var err error
  40. if strings.ToLower(dbType) != core.MSSQL {
  41. db, err = sql.Open(dbType, connStr)
  42. } else {
  43. db, err = sql.Open(dbType, strings.Replace(connStr, "xorm_test", "master", -1))
  44. }
  45. if err != nil {
  46. return err
  47. }
  48. switch strings.ToLower(dbType) {
  49. case core.MSSQL:
  50. if _, err = db.Exec("If(db_id(N'xorm_test') IS NULL) BEGIN CREATE DATABASE xorm_test; END;"); err != nil {
  51. return fmt.Errorf("db.Exec: %v", err)
  52. }
  53. case core.POSTGRES:
  54. rows, err := db.Query(fmt.Sprintf("SELECT 1 FROM pg_database WHERE datname = 'xorm_test'"))
  55. if err != nil {
  56. return fmt.Errorf("db.Query: %v", err)
  57. }
  58. defer rows.Close()
  59. if !rows.Next() {
  60. if _, err = db.Exec("CREATE DATABASE xorm_test"); err != nil {
  61. return fmt.Errorf("CREATE DATABASE: %v", err)
  62. }
  63. }
  64. if *schema != "" {
  65. if _, err = db.Exec("CREATE SCHEMA IF NOT EXISTS " + *schema); err != nil {
  66. return fmt.Errorf("CREATE SCHEMA: %v", err)
  67. }
  68. }
  69. case core.MYSQL:
  70. if _, err = db.Exec("CREATE DATABASE IF NOT EXISTS xorm_test"); err != nil {
  71. return fmt.Errorf("db.Exec: %v", err)
  72. }
  73. }
  74. db.Close()
  75. testEngine, err = NewEngine(dbType, connStr)
  76. } else {
  77. testEngine, err = NewEngineGroup(dbType, strings.Split(connStr, *splitter))
  78. }
  79. if err != nil {
  80. return err
  81. }
  82. if *schema != "" {
  83. testEngine.SetSchema(*schema)
  84. }
  85. testEngine.ShowSQL(*showSQL)
  86. testEngine.SetLogLevel(core.LOG_DEBUG)
  87. if *cache {
  88. cacher := NewLRUCacher(NewMemoryStore(), 100000)
  89. testEngine.SetDefaultCacher(cacher)
  90. }
  91. if len(*mapType) > 0 {
  92. switch *mapType {
  93. case "snake":
  94. testEngine.SetMapper(core.SnakeMapper{})
  95. case "same":
  96. testEngine.SetMapper(core.SameMapper{})
  97. case "gonic":
  98. testEngine.SetMapper(core.LintGonicMapper)
  99. }
  100. }
  101. }
  102. tables, err := testEngine.DBMetas()
  103. if err != nil {
  104. return err
  105. }
  106. var tableNames = make([]interface{}, 0, len(tables))
  107. for _, table := range tables {
  108. tableNames = append(tableNames, table.Name)
  109. }
  110. if err = testEngine.DropTables(tableNames...); err != nil {
  111. return err
  112. }
  113. return nil
  114. }
  115. func prepareEngine() error {
  116. return createEngine(dbType, connString)
  117. }
  118. func TestMain(m *testing.M) {
  119. flag.Parse()
  120. dbType = *db
  121. if *db == "sqlite3" {
  122. if ptrConnStr == nil {
  123. connString = "./test.db?cache=shared&mode=rwc"
  124. } else {
  125. connString = *ptrConnStr
  126. }
  127. } else {
  128. if ptrConnStr == nil {
  129. log.Fatal("you should indicate conn string")
  130. return
  131. }
  132. connString = *ptrConnStr
  133. }
  134. dbs := strings.Split(*db, "::")
  135. conns := strings.Split(connString, "::")
  136. var res int
  137. for i := 0; i < len(dbs); i++ {
  138. dbType = dbs[i]
  139. connString = conns[i]
  140. testEngine = nil
  141. fmt.Println("testing", dbType, connString)
  142. if err := prepareEngine(); err != nil {
  143. log.Fatal(err)
  144. return
  145. }
  146. code := m.Run()
  147. if code > 0 {
  148. res = code
  149. }
  150. }
  151. os.Exit(res)
  152. }
  153. func TestPing(t *testing.T) {
  154. if err := testEngine.Ping(); err != nil {
  155. t.Fatal(err)
  156. }
  157. }