common_test.go 6.4 KB

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