stufflog graphql server
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.

87 lines
1.4 KiB

  1. package mysqldriver
  2. import (
  3. "github.com/pressly/goose"
  4. "log"
  5. "os"
  6. "testing"
  7. "time"
  8. )
  9. var testDB *DB
  10. func TestMain(m *testing.M) {
  11. testDbConnect := os.Getenv("DB_TEST_CONNECT")
  12. if testDbConnect == "" {
  13. testDbConnect = "stufflog_test:test1234@(localhost:3306)/stufflog_test"
  14. }
  15. for i := 0; i < 10; i++ {
  16. db, err := Open(testDbConnect)
  17. if err != nil {
  18. log.Println("DB ERROR", i, err)
  19. time.Sleep(time.Second)
  20. continue
  21. }
  22. testDB = db
  23. break
  24. }
  25. if testDB == nil {
  26. log.Println("DB FAILED")
  27. os.Exit(1)
  28. return
  29. }
  30. err := goose.SetDialect("mysql")
  31. if err != nil {
  32. log.Println("GOOSE ERROR", err)
  33. os.Exit(1)
  34. return
  35. }
  36. for {
  37. err = goose.Down(testDB.db.DB, "../../../migrations/mysql")
  38. if err != nil {
  39. break
  40. }
  41. }
  42. err = goose.Up(testDB.db.DB, "../../../migrations/mysql")
  43. if err != nil && err != goose.ErrNoNextVersion && err != goose.ErrNoCurrentVersion {
  44. log.Println("UP ERROR", err)
  45. os.Exit(3)
  46. return
  47. }
  48. code := m.Run()
  49. os.Exit(code)
  50. }
  51. func clearTable(tableName string) error {
  52. // If you really want to SQL inject the test DB, go right ahead!
  53. _, err := testDB.db.Exec("DELETE FROM " + tableName + " WHERE TRUE")
  54. return err
  55. }
  56. func mustParseTime(str string) time.Time {
  57. d, err := time.Parse(time.RFC3339Nano, str)
  58. if err != nil {
  59. panic(err)
  60. }
  61. return d.UTC()
  62. }
  63. func ptrInt(v int) *int {
  64. return &v
  65. }
  66. func ptrString(v string) *string {
  67. return &v
  68. }
  69. func ptrBool(v bool) *bool {
  70. return &v
  71. }