common_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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("fixQuery", func() {
  45. It("replace param for MySQL driver", func() {
  46. sql := "select id, name from users where id=$1"
  47. Expect(common.FixQuery(sql)).To(Equal("select id, name from users where id=?"))
  48. })
  49. It("replace all params for MySQL driver", func() {
  50. sql := "insert into users set name=$1 where id=$2"
  51. Expect(common.FixQuery(sql)).To(Equal("insert into users set name=? where id=?"))
  52. })
  53. })
  54. Context("ParseUrl", func() {
  55. Context("Success", func() {
  56. It("for MySQL", func() {
  57. // mysql://username:password@127.0.0.1:3306/database?parseTime=true
  58. // mysql://username:password@/database?socket=/var/run/mysqld/mysqld.sock
  59. url := "mysql://username:password@127.0.0.1:3306/database?parseTime=true"
  60. result, err := common.ParseUrl(url)
  61. Expect(err).To(Succeed())
  62. Expect(result.Scheme).To(Equal("mysql"))
  63. Expect(result.User.Username()).To(Equal("username"))
  64. password, whether := result.User.Password()
  65. Expect(password).To(Equal("password"))
  66. Expect(whether).To(BeTrue())
  67. Expect(result.Host).To(Equal("127.0.0.1:3306"))
  68. Expect(result.Path).To(Equal("/database"))
  69. Expect(result.RawQuery).To(Equal("parseTime=true"))
  70. })
  71. It("for PostgreSQL", func() {
  72. // postgres://username:password@127.0.0.1:5432/database?sslmode=disable
  73. // postgresql://username:password@127.0.0.1:5432/database?sslmode=disable
  74. url := "postgres://username:password@127.0.0.1:5432/database?sslmode=disable"
  75. result, err := common.ParseUrl(url)
  76. Expect(err).To(Succeed())
  77. Expect(result.Scheme).To(Equal("postgres"))
  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:5432"))
  83. Expect(result.Path).To(Equal("/database"))
  84. Expect(result.RawQuery).To(Equal("sslmode=disable"))
  85. })
  86. It("for SQLite", func() {
  87. // sqlite:///data/database.sqlite
  88. // sqlite3:///data/database.sqlite
  89. url := "sqlite:///data/database.sqlite"
  90. result, err := common.ParseUrl(url)
  91. Expect(err).To(Succeed())
  92. Expect(result.Scheme).To(Equal("sqlite"))
  93. Expect(result.Host).To(Equal(""))
  94. Expect(result.Path).To(Equal("/data/database.sqlite"))
  95. })
  96. })
  97. Context("Fail", func() {
  98. It("for empty", func() {
  99. _, err := common.ParseUrl("")
  100. Expect(err).NotTo(Succeed())
  101. Expect(err.Error()).To(Equal("protocol scheme is not defined"))
  102. })
  103. It("for else", func() {
  104. url := "12345"
  105. _, err := common.ParseUrl(url)
  106. Expect(err).NotTo(Succeed())
  107. Expect(err.Error()).To(Equal("protocol scheme is not defined"))
  108. })
  109. It("for not supported", func() {
  110. url := "example:///some-else"
  111. _, err := common.ParseUrl(url)
  112. Expect(err).NotTo(Succeed())
  113. Expect(err.Error()).To(Equal("unsupported protocol scheme: example"))
  114. })
  115. })
  116. })
  117. Context("OpenDB", func() {
  118. var migrationsDir string
  119. BeforeEach(func() {
  120. var err error
  121. migrationsDir, err = filepath.Abs("../../db/migrations")
  122. Expect(err).To(Succeed())
  123. })
  124. Context("Success", func() {
  125. // Note: you need to up MySQL server for this test case
  126. It("for MySQL", func() {
  127. databaseURL, err := url.Parse("mysql://root:root@127.0.0.1:3306/gosql")
  128. Expect(err).To(Succeed())
  129. db, err := common.OpenDB(databaseURL, migrationsDir)
  130. Expect(err).To(Succeed())
  131. Expect(db.Close()).To(Succeed())
  132. })
  133. // Note: you need to up PostgreSQL server for this test case
  134. It("for PostgreSQL", func() {
  135. databaseURL, err := url.Parse("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable")
  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. It("for SQLite", func() {
  142. f, err := ioutil.TempFile("", "go-sqlite3-test-")
  143. Expect(err).To(Succeed())
  144. f.Close()
  145. databaseURL, err := url.Parse("sqlite://" + f.Name())
  146. Expect(err).To(Succeed())
  147. db, err := common.OpenDB(databaseURL, migrationsDir)
  148. Expect(err).To(Succeed())
  149. Expect(db.Close()).To(Succeed())
  150. })
  151. })
  152. })
  153. })
  154. func TestSuite(t *testing.T) {
  155. RegisterFailHandler(Fail)
  156. RunSpecs(t, "gosql/common")
  157. }