common_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package common_test
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/url"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. . "github.com/onsi/ginkgo"
  10. . "github.com/onsi/gomega"
  11. "github.com/vladimirok5959/golang-sql/gosql/common"
  12. )
  13. var _ = Describe("common", func() {
  14. Context("log", func() {
  15. Context("time", func() {
  16. It("calculate one second", func() {
  17. str := common.Log("[func Exec]", time.Now().Add(time.Second*-1), nil, false, "")
  18. Expect(str).To(Equal("\x1b[0;33m[SQL] [func Exec] (empty) (nil) 1.000 ms\x1b[0m\n"))
  19. })
  20. })
  21. Context("format", func() {
  22. It("with func name", func() {
  23. str := common.Log("[func Exec]", time.Now(), nil, false, "")
  24. Expect(str).To(Equal("\x1b[0;33m[SQL] [func Exec] (empty) (nil) 0.000 ms\x1b[0m\n"))
  25. })
  26. It("with sql query", func() {
  27. str := common.Log("[func Exec]", time.Now(), nil, false, "select * from users")
  28. Expect(str).To(Equal("\x1b[0;33m[SQL] [func Exec] select * from users (empty) (nil) 0.000 ms\x1b[0m\n"))
  29. })
  30. It("with error message", func() {
  31. str := common.Log("[func Exec]", time.Now(), fmt.Errorf("Exec error"), false, "select * from users")
  32. Expect(str).To(Equal("\x1b[0;33m[SQL] [func Exec] select * from users (empty) \x1b[0m\x1b[0;31m(Exec error) 0.000 ms\x1b[0m\n"))
  33. })
  34. It("with transaction flag", func() {
  35. str := common.Log("[func Exec]", time.Now(), fmt.Errorf("Exec error"), true, "select * from users")
  36. Expect(str).To(Equal("\x1b[1;33m[SQL] [TX] [func Exec] select * from users (empty) \x1b[0m\x1b[0;31m(Exec error) 0.000 ms\x1b[0m\n"))
  37. })
  38. It("with sql query arguments", func() {
  39. str := common.Log("[func Exec]", time.Now(), fmt.Errorf("Exec error"), true, "select * from users where id=$1", 100)
  40. Expect(str).To(Equal("\x1b[1;33m[SQL] [TX] [func Exec] select * from users where id=$1 ([100]) \x1b[0m\x1b[0;31m(Exec error) 0.000 ms\x1b[0m\n"))
  41. })
  42. })
  43. })
  44. Context("ParseUrl", func() {
  45. Context("Success", func() {
  46. It("for MySQL", func() {
  47. // mysql://username:password@127.0.0.1:3306/database?parseTime=true
  48. // mysql://username:password@/database?socket=/var/run/mysqld/mysqld.sock
  49. url := "mysql://username:password@127.0.0.1:3306/database?parseTime=true"
  50. result, err := common.ParseUrl(url)
  51. Expect(err).To(Succeed())
  52. Expect(result.Scheme).To(Equal("mysql"))
  53. Expect(result.User.Username()).To(Equal("username"))
  54. password, whether := result.User.Password()
  55. Expect(password).To(Equal("password"))
  56. Expect(whether).To(BeTrue())
  57. Expect(result.Host).To(Equal("127.0.0.1:3306"))
  58. Expect(result.Path).To(Equal("/database"))
  59. Expect(result.RawQuery).To(Equal("parseTime=true"))
  60. })
  61. It("for PostgreSQL", func() {
  62. // postgres://username:password@127.0.0.1:5432/database?sslmode=disable
  63. // postgresql://username:password@127.0.0.1:5432/database?sslmode=disable
  64. url := "postgres://username:password@127.0.0.1:5432/database?sslmode=disable"
  65. result, err := common.ParseUrl(url)
  66. Expect(err).To(Succeed())
  67. Expect(result.Scheme).To(Equal("postgres"))
  68. Expect(result.User.Username()).To(Equal("username"))
  69. password, whether := result.User.Password()
  70. Expect(password).To(Equal("password"))
  71. Expect(whether).To(BeTrue())
  72. Expect(result.Host).To(Equal("127.0.0.1:5432"))
  73. Expect(result.Path).To(Equal("/database"))
  74. Expect(result.RawQuery).To(Equal("sslmode=disable"))
  75. })
  76. It("for SQLite", func() {
  77. // sqlite:///data/database.sqlite
  78. // sqlite3:///data/database.sqlite
  79. url := "sqlite:///data/database.sqlite"
  80. result, err := common.ParseUrl(url)
  81. Expect(err).To(Succeed())
  82. Expect(result.Scheme).To(Equal("sqlite"))
  83. Expect(result.Host).To(Equal(""))
  84. Expect(result.Path).To(Equal("/data/database.sqlite"))
  85. })
  86. })
  87. Context("Fail", func() {
  88. It("for empty", func() {
  89. _, err := common.ParseUrl("")
  90. Expect(err).NotTo(Succeed())
  91. Expect(err.Error()).To(Equal("protocol scheme is not defined"))
  92. })
  93. It("for else", func() {
  94. url := "12345"
  95. _, err := common.ParseUrl(url)
  96. Expect(err).NotTo(Succeed())
  97. Expect(err.Error()).To(Equal("protocol scheme is not defined"))
  98. })
  99. It("for not supported", func() {
  100. url := "example:///some-else"
  101. _, err := common.ParseUrl(url)
  102. Expect(err).NotTo(Succeed())
  103. Expect(err.Error()).To(Equal("unsupported protocol scheme: example"))
  104. })
  105. })
  106. })
  107. Context("OpenDB", func() {
  108. var migrationsDir string
  109. BeforeEach(func() {
  110. var err error
  111. migrationsDir, err = filepath.Abs("../../db/migrations")
  112. Expect(err).To(Succeed())
  113. })
  114. Context("Success", func() {
  115. // Note: you need to up MySQL server for this test case
  116. It("for MySQL", func() {
  117. databaseURL, err := url.Parse("mysql://root:root@127.0.0.1:3306/gosql")
  118. Expect(err).To(Succeed())
  119. db, err := common.OpenDB(databaseURL, migrationsDir)
  120. Expect(err).To(Succeed())
  121. Expect(db.Close()).To(Succeed())
  122. })
  123. // Note: you need to up PostgreSQL server for this test case
  124. It("for PostgreSQL", func() {
  125. databaseURL, err := url.Parse("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable")
  126. Expect(err).To(Succeed())
  127. db, err := common.OpenDB(databaseURL, migrationsDir)
  128. Expect(err).To(Succeed())
  129. Expect(db.Close()).To(Succeed())
  130. })
  131. It("for SQLite", func() {
  132. f, err := ioutil.TempFile("", "go-sqlite3-test-")
  133. Expect(err).To(Succeed())
  134. f.Close()
  135. databaseURL, err := url.Parse("sqlite://" + f.Name())
  136. Expect(err).To(Succeed())
  137. db, err := common.OpenDB(databaseURL, migrationsDir)
  138. Expect(err).To(Succeed())
  139. Expect(db.Close()).To(Succeed())
  140. })
  141. })
  142. })
  143. })
  144. func TestSuite(t *testing.T) {
  145. RegisterFailHandler(Fail)
  146. RunSpecs(t, "gosql/common")
  147. }