Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle https://xorm.io
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.
 
 
 
 
 

122 line
2.4 KiB

  1. package xorm
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "testing"
  8. _ "github.com/denisenkom/go-mssqldb"
  9. _ "github.com/go-sql-driver/mysql"
  10. "github.com/go-xorm/core"
  11. _ "github.com/lib/pq"
  12. _ "github.com/mattn/go-sqlite3"
  13. _ "github.com/ziutek/mymysql/godrv"
  14. )
  15. var (
  16. testEngine *Engine
  17. dbType string
  18. connString string
  19. db = flag.String("db", "sqlite3", "the tested database")
  20. showSQL = flag.Bool("show_sql", true, "show generated SQLs")
  21. ptrConnStr = flag.String("conn_str", "", "test database connection string")
  22. mapType = flag.String("map_type", "snake", "indicate the name mapping")
  23. cache = flag.Bool("cache", false, "if enable cache")
  24. )
  25. func createEngine(dbType, connStr string) error {
  26. if testEngine == nil {
  27. var err error
  28. testEngine, err = NewEngine(dbType, connStr)
  29. if err != nil {
  30. return err
  31. }
  32. testEngine.ShowSQL(*showSQL)
  33. testEngine.logger.SetLevel(core.LOG_DEBUG)
  34. if *cache {
  35. cacher := NewLRUCacher(NewMemoryStore(), 100000)
  36. testEngine.SetDefaultCacher(cacher)
  37. }
  38. if len(*mapType) > 0 {
  39. switch *mapType {
  40. case "snake":
  41. testEngine.SetMapper(core.SnakeMapper{})
  42. case "same":
  43. testEngine.SetMapper(core.SameMapper{})
  44. case "gonic":
  45. testEngine.SetMapper(core.LintGonicMapper)
  46. }
  47. }
  48. }
  49. tables, err := testEngine.DBMetas()
  50. if err != nil {
  51. return err
  52. }
  53. var tableNames = make([]interface{}, 0, len(tables))
  54. for _, table := range tables {
  55. tableNames = append(tableNames, table.Name)
  56. }
  57. if err = testEngine.DropTables(tableNames...); err != nil {
  58. return err
  59. }
  60. return nil
  61. }
  62. func prepareEngine() error {
  63. return createEngine(dbType, connString)
  64. }
  65. func TestMain(m *testing.M) {
  66. flag.Parse()
  67. dbType = *db
  68. if *db == "sqlite3" {
  69. if ptrConnStr == nil {
  70. connString = "./test.db?cache=shared&mode=rwc"
  71. } else {
  72. connString = *ptrConnStr
  73. }
  74. } else {
  75. if ptrConnStr == nil {
  76. fmt.Println("you should indicate conn string")
  77. return
  78. }
  79. connString = *ptrConnStr
  80. }
  81. dbs := strings.Split(*db, "::")
  82. conns := strings.Split(connString, "::")
  83. var res int
  84. for i := 0; i < len(dbs); i++ {
  85. dbType = dbs[i]
  86. connString = conns[i]
  87. testEngine = nil
  88. fmt.Println("testing", dbType, connString)
  89. if err := prepareEngine(); err != nil {
  90. fmt.Println(err)
  91. return
  92. }
  93. code := m.Run()
  94. if code > 0 {
  95. res = code
  96. }
  97. }
  98. os.Exit(res)
  99. }
  100. func TestPing(t *testing.T) {
  101. if err := testEngine.Ping(); err != nil {
  102. t.Fatal(err)
  103. }
  104. }