gosql_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package gosql_test
  2. import (
  3. "context"
  4. "io/ioutil"
  5. "path/filepath"
  6. "testing"
  7. . "github.com/onsi/ginkgo"
  8. . "github.com/onsi/gomega"
  9. "github.com/vladimirok5959/golang-sql/gosql"
  10. )
  11. var _ = Describe("gosql", func() {
  12. Context("Open", func() {
  13. var migrationsDir string
  14. var ctx = context.Background()
  15. var sql = "select id, name from users where id=$1"
  16. var (
  17. id int
  18. name string
  19. )
  20. BeforeEach(func() {
  21. var err error
  22. migrationsDir, err = filepath.Abs("../db/migrations")
  23. Expect(err).To(Succeed())
  24. })
  25. // // Note: you need to up MySQL server for this test case
  26. // Context("for MySQL", func() {
  27. // It("open connection, migrate and select data", func() {
  28. // db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
  29. // Expect(err).To(Succeed())
  30. // err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
  31. // Expect(err).To(Succeed())
  32. // Expect(id).To(Equal(1))
  33. // Expect(name).To(Equal("Alice"))
  34. // err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
  35. // Expect(err).To(Succeed())
  36. // Expect(id).To(Equal(2))
  37. // Expect(name).To(Equal("Bob"))
  38. // Expect(db.Close()).To(Succeed())
  39. // })
  40. // It("open connection, migrate and select by ID", func() {
  41. // db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
  42. // Expect(err).To(Succeed())
  43. // var rowUser struct {
  44. // ID int64 `field:"id" table:"users"`
  45. // Name string `field:"name"`
  46. // }
  47. // err = db.QueryRowByID(ctx, 1, &rowUser)
  48. // Expect(err).To(Succeed())
  49. // Expect(rowUser.ID).To(Equal(int64(1)))
  50. // Expect(rowUser.Name).To(Equal("Alice"))
  51. // Expect(db.Close()).To(Succeed())
  52. // })
  53. // It("open connection, migrate and check row", func() {
  54. // db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
  55. // Expect(err).To(Succeed())
  56. // var rowUser struct {
  57. // ID int64 `field:"id" table:"users"`
  58. // Name string `field:"name"`
  59. // }
  60. // Expect(db.RowExists(ctx, 1, &rowUser)).To(BeTrue())
  61. // Expect(db.RowExists(ctx, 2, &rowUser)).To(BeTrue())
  62. // Expect(db.RowExists(ctx, 3, &rowUser)).To(BeFalse())
  63. // Expect(db.RowExists(ctx, 4, &rowUser)).To(BeFalse())
  64. // Expect(db.RowExists(ctx, 5, &rowUser)).To(BeFalse())
  65. // Expect(db.Close()).To(Succeed())
  66. // })
  67. // })
  68. // // Note: you need to up PostgreSQL server for this test case
  69. // Context("for PostgreSQL", func() {
  70. // It("open connection, migrate and select data", func() {
  71. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  72. // Expect(err).To(Succeed())
  73. // err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
  74. // Expect(err).To(Succeed())
  75. // Expect(id).To(Equal(1))
  76. // Expect(name).To(Equal("Alice"))
  77. // err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
  78. // Expect(err).To(Succeed())
  79. // Expect(id).To(Equal(2))
  80. // Expect(name).To(Equal("Bob"))
  81. // Expect(db.Close()).To(Succeed())
  82. // })
  83. // It("open connection, migrate and select by ID", func() {
  84. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  85. // Expect(err).To(Succeed())
  86. // var rowUser struct {
  87. // ID int64 `field:"id" table:"users"`
  88. // Name string `field:"name"`
  89. // }
  90. // err = db.QueryRowByID(ctx, 1, &rowUser)
  91. // Expect(err).To(Succeed())
  92. // Expect(rowUser.ID).To(Equal(int64(1)))
  93. // Expect(rowUser.Name).To(Equal("Alice"))
  94. // Expect(db.Close()).To(Succeed())
  95. // })
  96. // It("open connection, migrate and check row", func() {
  97. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  98. // Expect(err).To(Succeed())
  99. // var rowUser struct {
  100. // ID int64 `field:"id" table:"users"`
  101. // Name string `field:"name"`
  102. // }
  103. // Expect(db.RowExists(ctx, 1, &rowUser)).To(BeTrue())
  104. // Expect(db.RowExists(ctx, 2, &rowUser)).To(BeTrue())
  105. // Expect(db.RowExists(ctx, 3, &rowUser)).To(BeFalse())
  106. // Expect(db.RowExists(ctx, 4, &rowUser)).To(BeFalse())
  107. // Expect(db.RowExists(ctx, 5, &rowUser)).To(BeFalse())
  108. // Expect(db.Close()).To(Succeed())
  109. // })
  110. // })
  111. Context("for SQLite", func() {
  112. It("open connection, migrate and select data", func() {
  113. f, err := ioutil.TempFile("", "go-sqlite-test-")
  114. Expect(err).To(Succeed())
  115. f.Close()
  116. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  117. Expect(err).To(Succeed())
  118. err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
  119. Expect(err).To(Succeed())
  120. Expect(id).To(Equal(1))
  121. Expect(name).To(Equal("Alice"))
  122. err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
  123. Expect(err).To(Succeed())
  124. Expect(id).To(Equal(2))
  125. Expect(name).To(Equal("Bob"))
  126. Expect(db.Close()).To(Succeed())
  127. })
  128. It("open connection, migrate and select by ID", func() {
  129. f, err := ioutil.TempFile("", "go-sqlite-test-")
  130. Expect(err).To(Succeed())
  131. f.Close()
  132. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  133. Expect(err).To(Succeed())
  134. var rowUser struct {
  135. ID int64 `field:"id" table:"users"`
  136. Name string `field:"name"`
  137. }
  138. err = db.QueryRowByID(ctx, 1, &rowUser)
  139. Expect(err).To(Succeed())
  140. Expect(rowUser.ID).To(Equal(int64(1)))
  141. Expect(rowUser.Name).To(Equal("Alice"))
  142. Expect(db.Close()).To(Succeed())
  143. })
  144. It("open connection, migrate and check row", func() {
  145. f, err := ioutil.TempFile("", "go-sqlite-test-")
  146. Expect(err).To(Succeed())
  147. f.Close()
  148. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  149. Expect(err).To(Succeed())
  150. var rowUser struct {
  151. ID int64 `field:"id" table:"users"`
  152. Name string `field:"name"`
  153. }
  154. Expect(db.RowExists(ctx, 1, &rowUser)).To(BeTrue())
  155. Expect(db.RowExists(ctx, 2, &rowUser)).To(BeTrue())
  156. Expect(db.RowExists(ctx, 3, &rowUser)).To(BeFalse())
  157. Expect(db.RowExists(ctx, 4, &rowUser)).To(BeFalse())
  158. Expect(db.RowExists(ctx, 5, &rowUser)).To(BeFalse())
  159. Expect(db.Close()).To(Succeed())
  160. })
  161. })
  162. It("open connection and skip migration", func() {
  163. f, err := ioutil.TempFile("", "go-sqlite-test-")
  164. Expect(err).To(Succeed())
  165. f.Close()
  166. db, err := gosql.Open("sqlite://"+f.Name(), "", true, false)
  167. Expect(err).To(Succeed())
  168. Expect(db.Ping(ctx)).To(Succeed())
  169. var size int
  170. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  171. Expect(err.Error()).To(Equal("no such table: users"))
  172. })
  173. })
  174. })
  175. func TestSuite(t *testing.T) {
  176. RegisterFailHandler(Fail)
  177. RunSpecs(t, "gosql")
  178. }