gosql_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. // It("open connection, migrate and insert row", func() {
  97. // db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", 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. // var size int
  104. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  105. // Expect(err).To(Succeed())
  106. // Expect(size).To(Equal(2))
  107. // rowUser.Name = "James"
  108. // Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
  109. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  110. // Expect(err).To(Succeed())
  111. // Expect(size).To(Equal(3))
  112. // rowUser.Name = "Robert"
  113. // Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
  114. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  115. // Expect(err).To(Succeed())
  116. // Expect(size).To(Equal(4))
  117. // names := []string{}
  118. // err = db.Each(
  119. // ctx,
  120. // "SELECT 0, name FROM users ORDER BY name ASC",
  121. // func(ctx context.Context, rows *gosql.Rows) error {
  122. // if err := rows.Scans(&rowUser); err != nil {
  123. // return err
  124. // }
  125. // names = append(names, rowUser.Name)
  126. // return nil
  127. // },
  128. // )
  129. // Expect(err).To(Succeed())
  130. // Expect(names).To(Equal([]string{"Alice", "Bob", "James", "Robert"}))
  131. // Expect(db.Close()).To(Succeed())
  132. // })
  133. // })
  134. // // Note: you need to up PostgreSQL server for this test case
  135. // Context("for PostgreSQL", func() {
  136. // It("open connection, migrate and select data", func() {
  137. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  138. // Expect(err).To(Succeed())
  139. // err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
  140. // Expect(err).To(Succeed())
  141. // Expect(id).To(Equal(1))
  142. // Expect(name).To(Equal("Alice"))
  143. // err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
  144. // Expect(err).To(Succeed())
  145. // Expect(id).To(Equal(2))
  146. // Expect(name).To(Equal("Bob"))
  147. // Expect(db.Close()).To(Succeed())
  148. // })
  149. // It("open connection, migrate and select by ID", func() {
  150. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  151. // Expect(err).To(Succeed())
  152. // var rowUser struct {
  153. // ID int64 `field:"id" table:"users"`
  154. // Name string `field:"name"`
  155. // }
  156. // err = db.QueryRowByID(ctx, 1, &rowUser)
  157. // Expect(err).To(Succeed())
  158. // Expect(rowUser.ID).To(Equal(int64(1)))
  159. // Expect(rowUser.Name).To(Equal("Alice"))
  160. // Expect(db.Close()).To(Succeed())
  161. // })
  162. // It("open connection, migrate and check row", func() {
  163. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  164. // Expect(err).To(Succeed())
  165. // var rowUser struct {
  166. // ID int64 `field:"id" table:"users"`
  167. // Name string `field:"name"`
  168. // }
  169. // Expect(db.RowExists(ctx, 1, &rowUser)).To(BeTrue())
  170. // Expect(db.RowExists(ctx, 2, &rowUser)).To(BeTrue())
  171. // Expect(db.RowExists(ctx, 3, &rowUser)).To(BeFalse())
  172. // Expect(db.RowExists(ctx, 4, &rowUser)).To(BeFalse())
  173. // Expect(db.RowExists(ctx, 5, &rowUser)).To(BeFalse())
  174. // Expect(db.Close()).To(Succeed())
  175. // })
  176. // It("open connection, migrate and delete row", func() {
  177. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  178. // Expect(err).To(Succeed())
  179. // var rowUser struct {
  180. // ID int64 `field:"id" table:"users"`
  181. // Name string `field:"name"`
  182. // }
  183. // var size int
  184. // Expect(db.DeleteRowByID(ctx, 2, &rowUser)).To(Succeed())
  185. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  186. // Expect(err).To(Succeed())
  187. // Expect(size).To(Equal(1))
  188. // Expect(db.DeleteRowByID(ctx, 1, &rowUser)).To(Succeed())
  189. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  190. // Expect(err).To(Succeed())
  191. // Expect(size).To(Equal(0))
  192. // Expect(db.Close()).To(Succeed())
  193. // })
  194. // It("open connection, migrate and insert row", func() {
  195. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", 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. // var size int
  202. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  203. // Expect(err).To(Succeed())
  204. // Expect(size).To(Equal(2))
  205. // rowUser.Name = "James"
  206. // Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
  207. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  208. // Expect(err).To(Succeed())
  209. // Expect(size).To(Equal(3))
  210. // rowUser.Name = "Robert"
  211. // Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
  212. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  213. // Expect(err).To(Succeed())
  214. // Expect(size).To(Equal(4))
  215. // names := []string{}
  216. // err = db.Each(
  217. // ctx,
  218. // "SELECT 0, name FROM users ORDER BY name ASC",
  219. // func(ctx context.Context, rows *gosql.Rows) error {
  220. // if err := rows.Scans(&rowUser); err != nil {
  221. // return err
  222. // }
  223. // names = append(names, rowUser.Name)
  224. // return nil
  225. // },
  226. // )
  227. // Expect(err).To(Succeed())
  228. // Expect(names).To(Equal([]string{"Alice", "Bob", "James", "Robert"}))
  229. // Expect(db.Close()).To(Succeed())
  230. // })
  231. // })
  232. Context("for SQLite", func() {
  233. It("open connection, migrate and select data", func() {
  234. f, err := ioutil.TempFile("", "go-sqlite-test-")
  235. Expect(err).To(Succeed())
  236. f.Close()
  237. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  238. Expect(err).To(Succeed())
  239. err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
  240. Expect(err).To(Succeed())
  241. Expect(id).To(Equal(1))
  242. Expect(name).To(Equal("Alice"))
  243. err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
  244. Expect(err).To(Succeed())
  245. Expect(id).To(Equal(2))
  246. Expect(name).To(Equal("Bob"))
  247. Expect(db.Close()).To(Succeed())
  248. })
  249. It("open connection, migrate and select by ID", func() {
  250. f, err := ioutil.TempFile("", "go-sqlite-test-")
  251. Expect(err).To(Succeed())
  252. f.Close()
  253. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  254. Expect(err).To(Succeed())
  255. var rowUser struct {
  256. ID int64 `field:"id" table:"users"`
  257. Name string `field:"name"`
  258. }
  259. err = db.QueryRowByID(ctx, 1, &rowUser)
  260. Expect(err).To(Succeed())
  261. Expect(rowUser.ID).To(Equal(int64(1)))
  262. Expect(rowUser.Name).To(Equal("Alice"))
  263. Expect(db.Close()).To(Succeed())
  264. })
  265. It("open connection, migrate and check row", func() {
  266. f, err := ioutil.TempFile("", "go-sqlite-test-")
  267. Expect(err).To(Succeed())
  268. f.Close()
  269. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  270. Expect(err).To(Succeed())
  271. var rowUser struct {
  272. ID int64 `field:"id" table:"users"`
  273. Name string `field:"name"`
  274. }
  275. Expect(db.RowExists(ctx, 1, &rowUser)).To(BeTrue())
  276. Expect(db.RowExists(ctx, 2, &rowUser)).To(BeTrue())
  277. Expect(db.RowExists(ctx, 3, &rowUser)).To(BeFalse())
  278. Expect(db.RowExists(ctx, 4, &rowUser)).To(BeFalse())
  279. Expect(db.RowExists(ctx, 5, &rowUser)).To(BeFalse())
  280. Expect(db.Close()).To(Succeed())
  281. })
  282. It("open connection, migrate and delete row", func() {
  283. f, err := ioutil.TempFile("", "go-sqlite-test-")
  284. Expect(err).To(Succeed())
  285. f.Close()
  286. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  287. Expect(err).To(Succeed())
  288. var rowUser struct {
  289. ID int64 `field:"id" table:"users"`
  290. Name string `field:"name"`
  291. }
  292. var size int
  293. Expect(db.DeleteRowByID(ctx, 2, &rowUser)).To(Succeed())
  294. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  295. Expect(err).To(Succeed())
  296. Expect(size).To(Equal(1))
  297. Expect(db.DeleteRowByID(ctx, 1, &rowUser)).To(Succeed())
  298. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  299. Expect(err).To(Succeed())
  300. Expect(size).To(Equal(0))
  301. Expect(db.Close()).To(Succeed())
  302. })
  303. It("open connection, migrate and insert row", func() {
  304. f, err := ioutil.TempFile("", "go-sqlite-test-")
  305. Expect(err).To(Succeed())
  306. f.Close()
  307. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  308. Expect(err).To(Succeed())
  309. var rowUser struct {
  310. ID int64 `field:"id" table:"users"`
  311. Name string `field:"name"`
  312. }
  313. var size int
  314. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  315. Expect(err).To(Succeed())
  316. Expect(size).To(Equal(2))
  317. rowUser.Name = "James"
  318. Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
  319. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  320. Expect(err).To(Succeed())
  321. Expect(size).To(Equal(3))
  322. rowUser.Name = "Robert"
  323. Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
  324. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  325. Expect(err).To(Succeed())
  326. Expect(size).To(Equal(4))
  327. names := []string{}
  328. err = db.Each(
  329. ctx,
  330. "SELECT 0, name FROM users ORDER BY name ASC",
  331. func(ctx context.Context, rows *gosql.Rows) error {
  332. if err := rows.Scans(&rowUser); err != nil {
  333. return err
  334. }
  335. names = append(names, rowUser.Name)
  336. return nil
  337. },
  338. )
  339. Expect(err).To(Succeed())
  340. Expect(names).To(Equal([]string{"Alice", "Bob", "James", "Robert"}))
  341. Expect(db.Close()).To(Succeed())
  342. })
  343. })
  344. It("open connection and skip migration", func() {
  345. f, err := ioutil.TempFile("", "go-sqlite-test-")
  346. Expect(err).To(Succeed())
  347. f.Close()
  348. db, err := gosql.Open("sqlite://"+f.Name(), "", true, false)
  349. Expect(err).To(Succeed())
  350. Expect(db.Ping(ctx)).To(Succeed())
  351. var size int
  352. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  353. Expect(err.Error()).To(Equal("no such table: users"))
  354. })
  355. })
  356. })
  357. func TestSuite(t *testing.T) {
  358. RegisterFailHandler(Fail)
  359. RunSpecs(t, "gosql")
  360. }