xorm_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. }
  35. tables, err := testEngine.DBMetas()
  36. if err != nil {
  37. return err
  38. }
  39. var tableNames = make([]interface{}, 0, len(tables))
  40. for _, table := range tables {
  41. tableNames = append(tableNames, table.Name)
  42. }
  43. if err = testEngine.DropTables(tableNames...); err != nil {
  44. return err
  45. }
  46. return nil
  47. }
  48. func prepareEngine() error {
  49. return createEngine(dbType, connString)
  50. }
  51. func TestMain(m *testing.M) {
  52. flag.Parse()
  53. dbType = *db
  54. if *db == "sqlite3" {
  55. if ptrConnStr == nil {
  56. connString = "./test.db?cache=shared&mode=rwc"
  57. } else {
  58. connString = *ptrConnStr
  59. }
  60. } else {
  61. if ptrConnStr == nil {
  62. fmt.Println("you should indicate conn string")
  63. return
  64. }
  65. connString = *ptrConnStr
  66. }
  67. dbs := strings.Split(*db, "::")
  68. conns := strings.Split(connString, "::")
  69. var res int
  70. for i := 0; i < len(dbs); i++ {
  71. dbType = dbs[i]
  72. connString = conns[i]
  73. testEngine = nil
  74. fmt.Println("testing", dbType, connString)
  75. if err := prepareEngine(); err != nil {
  76. fmt.Println(err)
  77. return
  78. }
  79. code := m.Run()
  80. if code > 0 {
  81. res = code
  82. }
  83. }
  84. os.Exit(res)
  85. }
  86. func TestPing(t *testing.T) {
  87. if err := testEngine.Ping(); err != nil {
  88. t.Fatal(err)
  89. }
  90. }