xorm_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package xorm
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "testing"
  7. _ "github.com/go-sql-driver/mysql"
  8. _ "github.com/lib/pq"
  9. _ "github.com/mattn/go-sqlite3"
  10. )
  11. var (
  12. testEngine *Engine
  13. connString string
  14. db = flag.String("db", "sqlite3", "the tested database")
  15. showSQL = flag.Bool("show_sql", true, "show generated SQLs")
  16. ptrConnStr = flag.String("conn_str", "", "test database connection string")
  17. mapType = flag.String("map_type", "snake", "indicate the name mapping")
  18. cache = flag.Bool("cache", false, "if enable cache")
  19. )
  20. func createEngine(dbType, connStr string) error {
  21. if testEngine == nil {
  22. var err error
  23. testEngine, err = NewEngine(dbType, connStr)
  24. if err != nil {
  25. return err
  26. }
  27. testEngine.ShowSQL(*showSQL)
  28. }
  29. tables, err := testEngine.DBMetas()
  30. if err != nil {
  31. return err
  32. }
  33. var tableNames = make([]interface{}, 0, len(tables))
  34. for _, table := range tables {
  35. tableNames = append(tableNames, table.Name)
  36. }
  37. return testEngine.DropTables(tableNames...)
  38. }
  39. func prepareEngine() error {
  40. return createEngine(*db, connString)
  41. }
  42. func TestMain(m *testing.M) {
  43. flag.Parse()
  44. if *db == "sqlite3" {
  45. if ptrConnStr == nil {
  46. connString = "./test.db"
  47. } else {
  48. connString = *ptrConnStr
  49. }
  50. } else {
  51. if ptrConnStr == nil {
  52. fmt.Println("you should indicate conn string")
  53. return
  54. }
  55. connString = *ptrConnStr
  56. }
  57. if err := prepareEngine(); err != nil {
  58. fmt.Println(err)
  59. return
  60. }
  61. os.Exit(m.Run())
  62. }
  63. func TestPing(t *testing.T) {
  64. if err := testEngine.Ping(); err != nil {
  65. t.Fatal(err)
  66. }
  67. }