gosql_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. // // Reset databases
  25. // // Note: uncomment for MySQL and PostgreSQL tests
  26. // var db common.Engine
  27. // // MySQL
  28. // db, err = gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", "", true, false)
  29. // Expect(err).To(Succeed())
  30. // _, _ = db.Exec(ctx, "DROP TABLE schema_migrations, users")
  31. // // PostgreSQL
  32. // db, err = gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", "", true, false)
  33. // Expect(err).To(Succeed())
  34. // _, _ = db.Exec(ctx, "DROP TABLE schema_migrations, users")
  35. })
  36. // // Note: you need to up MySQL server for this test case
  37. // Context("for MySQL", func() {
  38. // It("open connection, migrate and select data", func() {
  39. // db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
  40. // Expect(err).To(Succeed())
  41. // err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
  42. // Expect(err).To(Succeed())
  43. // Expect(id).To(Equal(1))
  44. // Expect(name).To(Equal("Alice"))
  45. // err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
  46. // Expect(err).To(Succeed())
  47. // Expect(id).To(Equal(2))
  48. // Expect(name).To(Equal("Bob"))
  49. // Expect(db.Close()).To(Succeed())
  50. // })
  51. // It("open connection, migrate and select by ID", func() {
  52. // db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
  53. // Expect(err).To(Succeed())
  54. // var rowUser struct {
  55. // ID int64 `field:"id" table:"users"`
  56. // Name string `field:"name"`
  57. // }
  58. // err = db.QueryRowByID(ctx, 1, &rowUser)
  59. // Expect(err).To(Succeed())
  60. // Expect(rowUser.ID).To(Equal(int64(1)))
  61. // Expect(rowUser.Name).To(Equal("Alice"))
  62. // Expect(db.Close()).To(Succeed())
  63. // })
  64. // It("open connection, migrate and check row", func() {
  65. // db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
  66. // Expect(err).To(Succeed())
  67. // var rowUser struct {
  68. // ID int64 `field:"id" table:"users"`
  69. // Name string `field:"name"`
  70. // }
  71. // Expect(db.RowExists(ctx, 1, &rowUser)).To(BeTrue())
  72. // Expect(db.RowExists(ctx, 2, &rowUser)).To(BeTrue())
  73. // Expect(db.RowExists(ctx, 3, &rowUser)).To(BeFalse())
  74. // Expect(db.RowExists(ctx, 4, &rowUser)).To(BeFalse())
  75. // Expect(db.RowExists(ctx, 5, &rowUser)).To(BeFalse())
  76. // Expect(db.Close()).To(Succeed())
  77. // })
  78. // It("open connection, migrate and delete row", func() {
  79. // db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
  80. // Expect(err).To(Succeed())
  81. // var rowUser struct {
  82. // ID int64 `field:"id" table:"users"`
  83. // Name string `field:"name"`
  84. // }
  85. // var size int
  86. // Expect(db.DeleteRowByID(ctx, 2, &rowUser)).To(Succeed())
  87. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  88. // Expect(err).To(Succeed())
  89. // Expect(size).To(Equal(1))
  90. // Expect(db.DeleteRowByID(ctx, 1, &rowUser)).To(Succeed())
  91. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  92. // Expect(err).To(Succeed())
  93. // Expect(size).To(Equal(0))
  94. // Expect(db.Close()).To(Succeed())
  95. // })
  96. // })
  97. // // Note: you need to up PostgreSQL server for this test case
  98. // Context("for PostgreSQL", func() {
  99. // It("open connection, migrate and select data", func() {
  100. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  101. // Expect(err).To(Succeed())
  102. // err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
  103. // Expect(err).To(Succeed())
  104. // Expect(id).To(Equal(1))
  105. // Expect(name).To(Equal("Alice"))
  106. // err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
  107. // Expect(err).To(Succeed())
  108. // Expect(id).To(Equal(2))
  109. // Expect(name).To(Equal("Bob"))
  110. // Expect(db.Close()).To(Succeed())
  111. // })
  112. // It("open connection, migrate and select by ID", func() {
  113. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  114. // Expect(err).To(Succeed())
  115. // var rowUser struct {
  116. // ID int64 `field:"id" table:"users"`
  117. // Name string `field:"name"`
  118. // }
  119. // err = db.QueryRowByID(ctx, 1, &rowUser)
  120. // Expect(err).To(Succeed())
  121. // Expect(rowUser.ID).To(Equal(int64(1)))
  122. // Expect(rowUser.Name).To(Equal("Alice"))
  123. // Expect(db.Close()).To(Succeed())
  124. // })
  125. // It("open connection, migrate and check row", func() {
  126. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  127. // Expect(err).To(Succeed())
  128. // var rowUser struct {
  129. // ID int64 `field:"id" table:"users"`
  130. // Name string `field:"name"`
  131. // }
  132. // Expect(db.RowExists(ctx, 1, &rowUser)).To(BeTrue())
  133. // Expect(db.RowExists(ctx, 2, &rowUser)).To(BeTrue())
  134. // Expect(db.RowExists(ctx, 3, &rowUser)).To(BeFalse())
  135. // Expect(db.RowExists(ctx, 4, &rowUser)).To(BeFalse())
  136. // Expect(db.RowExists(ctx, 5, &rowUser)).To(BeFalse())
  137. // Expect(db.Close()).To(Succeed())
  138. // })
  139. // It("open connection, migrate and delete row", func() {
  140. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  141. // Expect(err).To(Succeed())
  142. // var rowUser struct {
  143. // ID int64 `field:"id" table:"users"`
  144. // Name string `field:"name"`
  145. // }
  146. // var size int
  147. // Expect(db.DeleteRowByID(ctx, 2, &rowUser)).To(Succeed())
  148. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  149. // Expect(err).To(Succeed())
  150. // Expect(size).To(Equal(1))
  151. // Expect(db.DeleteRowByID(ctx, 1, &rowUser)).To(Succeed())
  152. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  153. // Expect(err).To(Succeed())
  154. // Expect(size).To(Equal(0))
  155. // Expect(db.Close()).To(Succeed())
  156. // })
  157. // })
  158. Context("for SQLite", func() {
  159. It("open connection, migrate and select data", func() {
  160. f, err := ioutil.TempFile("", "go-sqlite-test-")
  161. Expect(err).To(Succeed())
  162. f.Close()
  163. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  164. Expect(err).To(Succeed())
  165. err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
  166. Expect(err).To(Succeed())
  167. Expect(id).To(Equal(1))
  168. Expect(name).To(Equal("Alice"))
  169. err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
  170. Expect(err).To(Succeed())
  171. Expect(id).To(Equal(2))
  172. Expect(name).To(Equal("Bob"))
  173. Expect(db.Close()).To(Succeed())
  174. })
  175. It("open connection, migrate and select by ID", func() {
  176. f, err := ioutil.TempFile("", "go-sqlite-test-")
  177. Expect(err).To(Succeed())
  178. f.Close()
  179. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  180. Expect(err).To(Succeed())
  181. var rowUser struct {
  182. ID int64 `field:"id" table:"users"`
  183. Name string `field:"name"`
  184. }
  185. err = db.QueryRowByID(ctx, 1, &rowUser)
  186. Expect(err).To(Succeed())
  187. Expect(rowUser.ID).To(Equal(int64(1)))
  188. Expect(rowUser.Name).To(Equal("Alice"))
  189. Expect(db.Close()).To(Succeed())
  190. })
  191. It("open connection, migrate and check row", func() {
  192. f, err := ioutil.TempFile("", "go-sqlite-test-")
  193. Expect(err).To(Succeed())
  194. f.Close()
  195. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  196. Expect(err).To(Succeed())
  197. var rowUser struct {
  198. ID int64 `field:"id" table:"users"`
  199. Name string `field:"name"`
  200. }
  201. Expect(db.RowExists(ctx, 1, &rowUser)).To(BeTrue())
  202. Expect(db.RowExists(ctx, 2, &rowUser)).To(BeTrue())
  203. Expect(db.RowExists(ctx, 3, &rowUser)).To(BeFalse())
  204. Expect(db.RowExists(ctx, 4, &rowUser)).To(BeFalse())
  205. Expect(db.RowExists(ctx, 5, &rowUser)).To(BeFalse())
  206. Expect(db.Close()).To(Succeed())
  207. })
  208. It("open connection, migrate and delete row", func() {
  209. f, err := ioutil.TempFile("", "go-sqlite-test-")
  210. Expect(err).To(Succeed())
  211. f.Close()
  212. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  213. Expect(err).To(Succeed())
  214. var rowUser struct {
  215. ID int64 `field:"id" table:"users"`
  216. Name string `field:"name"`
  217. }
  218. var size int
  219. Expect(db.DeleteRowByID(ctx, 2, &rowUser)).To(Succeed())
  220. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  221. Expect(err).To(Succeed())
  222. Expect(size).To(Equal(1))
  223. Expect(db.DeleteRowByID(ctx, 1, &rowUser)).To(Succeed())
  224. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  225. Expect(err).To(Succeed())
  226. Expect(size).To(Equal(0))
  227. Expect(db.Close()).To(Succeed())
  228. })
  229. })
  230. It("open connection and skip migration", func() {
  231. f, err := ioutil.TempFile("", "go-sqlite-test-")
  232. Expect(err).To(Succeed())
  233. f.Close()
  234. db, err := gosql.Open("sqlite://"+f.Name(), "", true, false)
  235. Expect(err).To(Succeed())
  236. Expect(db.Ping(ctx)).To(Succeed())
  237. var size int
  238. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  239. Expect(err.Error()).To(Equal("no such table: users"))
  240. })
  241. })
  242. })
  243. func TestSuite(t *testing.T) {
  244. RegisterFailHandler(Fail)
  245. RunSpecs(t, "gosql")
  246. }