common_test.go 7.0 KB

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