xorm_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. )
  14. var (
  15. testEngine *Engine
  16. dbType string
  17. connString string
  18. db = flag.String("db", "sqlite3", "the tested database")
  19. showSQL = flag.Bool("show_sql", true, "show generated SQLs")
  20. ptrConnStr = flag.String("conn_str", "", "test database connection string")
  21. mapType = flag.String("map_type", "snake", "indicate the name mapping")
  22. cache = flag.Bool("cache", false, "if enable cache")
  23. )
  24. func createEngine(dbType, connStr string) error {
  25. if testEngine == nil {
  26. var err error
  27. testEngine, err = NewEngine(dbType, connStr)
  28. if err != nil {
  29. return err
  30. }
  31. testEngine.ShowSQL(*showSQL)
  32. testEngine.logger.SetLevel(core.LOG_DEBUG)
  33. }
  34. tables, err := testEngine.DBMetas()
  35. if err != nil {
  36. return err
  37. }
  38. var tableNames = make([]interface{}, 0, len(tables))
  39. for _, table := range tables {
  40. tableNames = append(tableNames, table.Name)
  41. }
  42. if err = testEngine.DropTables(tableNames...); err != nil {
  43. return err
  44. }
  45. return nil
  46. }
  47. func prepareEngine() error {
  48. return createEngine(dbType, connString)
  49. }
  50. func TestMain(m *testing.M) {
  51. flag.Parse()
  52. dbType = *db
  53. if *db == "sqlite3" {
  54. if ptrConnStr == nil {
  55. connString = "./test.db?cache=shared&mode=rwc"
  56. } else {
  57. connString = *ptrConnStr
  58. }
  59. } else {
  60. if ptrConnStr == nil {
  61. fmt.Println("you should indicate conn string")
  62. return
  63. }
  64. connString = *ptrConnStr
  65. }
  66. dbs := strings.Split(*db, "::")
  67. conns := strings.Split(connString, "::")
  68. var res int
  69. for i := 0; i < len(dbs); i++ {
  70. dbType = dbs[i]
  71. connString = conns[i]
  72. testEngine = nil
  73. fmt.Println("testing", dbType, connString)
  74. if err := prepareEngine(); err != nil {
  75. fmt.Println(err)
  76. return
  77. }
  78. code := m.Run()
  79. if code > 0 {
  80. res = code
  81. }
  82. }
  83. os.Exit(res)
  84. }
  85. func TestPing(t *testing.T) {
  86. if err := testEngine.Ping(); err != nil {
  87. t.Fatal(err)
  88. }
  89. }