common_test.go 6.1 KB

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