common_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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("queryRowByIDString", func() {
  70. It("convert struct to select SQL query", func() {
  71. var row struct {
  72. ID int64 `field:"id" table:"users"`
  73. Name string `field:"name"`
  74. Value string `field:"value"`
  75. }
  76. Expect(common.QueryRowByIDString(&row)).To(Equal(`SELECT id, name, value FROM users WHERE id = $1 LIMIT 1`))
  77. })
  78. })
  79. Context("ParseUrl", func() {
  80. Context("Success", func() {
  81. It("for MySQL", func() {
  82. // mysql://username:password@127.0.0.1:3306/database?parseTime=true
  83. // mysql://username:password@/database?socket=/var/run/mysqld/mysqld.sock
  84. url := "mysql://username:password@127.0.0.1:3306/database?parseTime=true"
  85. result, err := common.ParseUrl(url)
  86. Expect(err).To(Succeed())
  87. Expect(result.Scheme).To(Equal("mysql"))
  88. Expect(result.User.Username()).To(Equal("username"))
  89. password, whether := result.User.Password()
  90. Expect(password).To(Equal("password"))
  91. Expect(whether).To(BeTrue())
  92. Expect(result.Host).To(Equal("127.0.0.1:3306"))
  93. Expect(result.Path).To(Equal("/database"))
  94. Expect(result.RawQuery).To(Equal("parseTime=true"))
  95. })
  96. It("for PostgreSQL", func() {
  97. // postgres://username:password@127.0.0.1:5432/database?sslmode=disable
  98. // postgresql://username:password@127.0.0.1:5432/database?sslmode=disable
  99. url := "postgres://username:password@127.0.0.1:5432/database?sslmode=disable"
  100. result, err := common.ParseUrl(url)
  101. Expect(err).To(Succeed())
  102. Expect(result.Scheme).To(Equal("postgres"))
  103. Expect(result.User.Username()).To(Equal("username"))
  104. password, whether := result.User.Password()
  105. Expect(password).To(Equal("password"))
  106. Expect(whether).To(BeTrue())
  107. Expect(result.Host).To(Equal("127.0.0.1:5432"))
  108. Expect(result.Path).To(Equal("/database"))
  109. Expect(result.RawQuery).To(Equal("sslmode=disable"))
  110. })
  111. It("for SQLite", func() {
  112. // sqlite:///data/database.sqlite
  113. // sqlite3:///data/database.sqlite
  114. url := "sqlite:///data/database.sqlite"
  115. result, err := common.ParseUrl(url)
  116. Expect(err).To(Succeed())
  117. Expect(result.Scheme).To(Equal("sqlite"))
  118. Expect(result.Host).To(Equal(""))
  119. Expect(result.Path).To(Equal("/data/database.sqlite"))
  120. })
  121. })
  122. Context("Fail", func() {
  123. It("for empty", func() {
  124. _, err := common.ParseUrl("")
  125. Expect(err).NotTo(Succeed())
  126. Expect(err.Error()).To(Equal("protocol scheme is not defined"))
  127. })
  128. It("for else", func() {
  129. url := "12345"
  130. _, err := common.ParseUrl(url)
  131. Expect(err).NotTo(Succeed())
  132. Expect(err.Error()).To(Equal("protocol scheme is not defined"))
  133. })
  134. It("for not supported", func() {
  135. url := "example:///some-else"
  136. _, err := common.ParseUrl(url)
  137. Expect(err).NotTo(Succeed())
  138. Expect(err.Error()).To(Equal("unsupported protocol scheme: example"))
  139. })
  140. })
  141. })
  142. Context("OpenDB", func() {
  143. var migrationsDir string
  144. BeforeEach(func() {
  145. var err error
  146. migrationsDir, err = filepath.Abs("../../db/migrations")
  147. Expect(err).To(Succeed())
  148. })
  149. Context("Success", func() {
  150. // // Note: you need to up MySQL server for this test case
  151. // It("for MySQL", func() {
  152. // databaseURL, err := url.Parse("mysql://root:root@127.0.0.1:3306/gosql")
  153. // Expect(err).To(Succeed())
  154. // db, err := common.OpenDB(databaseURL, migrationsDir, false, false)
  155. // Expect(err).To(Succeed())
  156. // Expect(db.Close()).To(Succeed())
  157. // })
  158. // // Note: you need to up PostgreSQL server for this test case
  159. // It("for PostgreSQL", func() {
  160. // databaseURL, err := url.Parse("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable")
  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. It("for SQLite", func() {
  167. f, err := ioutil.TempFile("", "go-sqlite3-test-")
  168. Expect(err).To(Succeed())
  169. f.Close()
  170. databaseURL, err := url.Parse("sqlite://" + f.Name())
  171. Expect(err).To(Succeed())
  172. db, err := common.OpenDB(databaseURL, migrationsDir, false, false)
  173. Expect(err).To(Succeed())
  174. Expect(db.Close()).To(Succeed())
  175. })
  176. })
  177. })
  178. })
  179. func TestSuite(t *testing.T) {
  180. RegisterFailHandler(Fail)
  181. RunSpecs(t, "gosql/common")
  182. }