gosql_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. // It("open connection, migrate and update row", func() {
  134. // db, err := gosql.Open("mysql://root:root@127.0.0.1:3306/gosql", migrationsDir, false, false)
  135. // Expect(err).To(Succeed())
  136. // var rowUser struct {
  137. // ID int64 `field:"id" table:"users"`
  138. // Name string `field:"name"`
  139. // }
  140. // _, err = db.Exec(ctx, "INSERT INTO users (id, name) VALUES ('3', 'Patrik')")
  141. // Expect(err).To(Succeed())
  142. // rowUser.ID = 3
  143. // rowUser.Name = "Robert"
  144. // Expect(db.UpdateRow(ctx, &rowUser)).To(Succeed())
  145. // err = db.QueryRowByID(ctx, 3, &rowUser)
  146. // Expect(err).To(Succeed())
  147. // Expect(rowUser.ID).To(Equal(int64(3)))
  148. // Expect(rowUser.Name).To(Equal("Robert"))
  149. // Expect(db.Close()).To(Succeed())
  150. // })
  151. // })
  152. // // Note: you need to up PostgreSQL server for this test case
  153. // Context("for PostgreSQL", func() {
  154. // It("open connection, migrate and select data", func() {
  155. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  156. // Expect(err).To(Succeed())
  157. // err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
  158. // Expect(err).To(Succeed())
  159. // Expect(id).To(Equal(1))
  160. // Expect(name).To(Equal("Alice"))
  161. // err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
  162. // Expect(err).To(Succeed())
  163. // Expect(id).To(Equal(2))
  164. // Expect(name).To(Equal("Bob"))
  165. // Expect(db.Close()).To(Succeed())
  166. // })
  167. // It("open connection, migrate and select by ID", func() {
  168. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  169. // Expect(err).To(Succeed())
  170. // var rowUser struct {
  171. // ID int64 `field:"id" table:"users"`
  172. // Name string `field:"name"`
  173. // }
  174. // err = db.QueryRowByID(ctx, 1, &rowUser)
  175. // Expect(err).To(Succeed())
  176. // Expect(rowUser.ID).To(Equal(int64(1)))
  177. // Expect(rowUser.Name).To(Equal("Alice"))
  178. // Expect(db.Close()).To(Succeed())
  179. // })
  180. // It("open connection, migrate and check row", func() {
  181. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  182. // Expect(err).To(Succeed())
  183. // var rowUser struct {
  184. // ID int64 `field:"id" table:"users"`
  185. // Name string `field:"name"`
  186. // }
  187. // Expect(db.RowExists(ctx, 1, &rowUser)).To(BeTrue())
  188. // Expect(db.RowExists(ctx, 2, &rowUser)).To(BeTrue())
  189. // Expect(db.RowExists(ctx, 3, &rowUser)).To(BeFalse())
  190. // Expect(db.RowExists(ctx, 4, &rowUser)).To(BeFalse())
  191. // Expect(db.RowExists(ctx, 5, &rowUser)).To(BeFalse())
  192. // Expect(db.Close()).To(Succeed())
  193. // })
  194. // It("open connection, migrate and delete 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. // Expect(db.DeleteRowByID(ctx, 2, &rowUser)).To(Succeed())
  203. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  204. // Expect(err).To(Succeed())
  205. // Expect(size).To(Equal(1))
  206. // Expect(db.DeleteRowByID(ctx, 1, &rowUser)).To(Succeed())
  207. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  208. // Expect(err).To(Succeed())
  209. // Expect(size).To(Equal(0))
  210. // Expect(db.Close()).To(Succeed())
  211. // })
  212. // It("open connection, migrate and insert row", func() {
  213. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  214. // Expect(err).To(Succeed())
  215. // var rowUser struct {
  216. // ID int64 `field:"id" table:"users"`
  217. // Name string `field:"name"`
  218. // }
  219. // var size int
  220. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  221. // Expect(err).To(Succeed())
  222. // Expect(size).To(Equal(2))
  223. // rowUser.Name = "James"
  224. // Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
  225. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  226. // Expect(err).To(Succeed())
  227. // Expect(size).To(Equal(3))
  228. // rowUser.Name = "Robert"
  229. // Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
  230. // err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  231. // Expect(err).To(Succeed())
  232. // Expect(size).To(Equal(4))
  233. // names := []string{}
  234. // err = db.Each(
  235. // ctx,
  236. // "SELECT 0, name FROM users ORDER BY name ASC",
  237. // func(ctx context.Context, rows *gosql.Rows) error {
  238. // if err := rows.Scans(&rowUser); err != nil {
  239. // return err
  240. // }
  241. // names = append(names, rowUser.Name)
  242. // return nil
  243. // },
  244. // )
  245. // Expect(err).To(Succeed())
  246. // Expect(names).To(Equal([]string{"Alice", "Bob", "James", "Robert"}))
  247. // Expect(db.Close()).To(Succeed())
  248. // })
  249. // It("open connection, migrate and update row", func() {
  250. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  251. // Expect(err).To(Succeed())
  252. // var rowUser struct {
  253. // ID int64 `field:"id" table:"users"`
  254. // Name string `field:"name"`
  255. // }
  256. // _, err = db.Exec(ctx, "INSERT INTO users (id, name) VALUES ('3', 'Patrik')")
  257. // Expect(err).To(Succeed())
  258. // rowUser.ID = 3
  259. // rowUser.Name = "Robert"
  260. // Expect(db.UpdateRow(ctx, &rowUser)).To(Succeed())
  261. // err = db.QueryRowByID(ctx, 3, &rowUser)
  262. // Expect(err).To(Succeed())
  263. // Expect(rowUser.ID).To(Equal(int64(3)))
  264. // Expect(rowUser.Name).To(Equal("Robert"))
  265. // Expect(db.Close()).To(Succeed())
  266. // })
  267. // })
  268. Context("for SQLite", func() {
  269. It("open connection, migrate and select data", func() {
  270. f, err := ioutil.TempFile("", "go-sqlite-test-")
  271. Expect(err).To(Succeed())
  272. f.Close()
  273. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  274. Expect(err).To(Succeed())
  275. err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
  276. Expect(err).To(Succeed())
  277. Expect(id).To(Equal(1))
  278. Expect(name).To(Equal("Alice"))
  279. err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
  280. Expect(err).To(Succeed())
  281. Expect(id).To(Equal(2))
  282. Expect(name).To(Equal("Bob"))
  283. Expect(db.Close()).To(Succeed())
  284. })
  285. It("open connection, migrate and select by ID", func() {
  286. f, err := ioutil.TempFile("", "go-sqlite-test-")
  287. Expect(err).To(Succeed())
  288. f.Close()
  289. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  290. Expect(err).To(Succeed())
  291. var rowUser struct {
  292. ID int64 `field:"id" table:"users"`
  293. Name string `field:"name"`
  294. }
  295. err = db.QueryRowByID(ctx, 1, &rowUser)
  296. Expect(err).To(Succeed())
  297. Expect(rowUser.ID).To(Equal(int64(1)))
  298. Expect(rowUser.Name).To(Equal("Alice"))
  299. Expect(db.Close()).To(Succeed())
  300. })
  301. It("open connection, migrate and check row", func() {
  302. f, err := ioutil.TempFile("", "go-sqlite-test-")
  303. Expect(err).To(Succeed())
  304. f.Close()
  305. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  306. Expect(err).To(Succeed())
  307. var rowUser struct {
  308. ID int64 `field:"id" table:"users"`
  309. Name string `field:"name"`
  310. }
  311. Expect(db.RowExists(ctx, 1, &rowUser)).To(BeTrue())
  312. Expect(db.RowExists(ctx, 2, &rowUser)).To(BeTrue())
  313. Expect(db.RowExists(ctx, 3, &rowUser)).To(BeFalse())
  314. Expect(db.RowExists(ctx, 4, &rowUser)).To(BeFalse())
  315. Expect(db.RowExists(ctx, 5, &rowUser)).To(BeFalse())
  316. Expect(db.Close()).To(Succeed())
  317. })
  318. It("open connection, migrate and delete row", func() {
  319. f, err := ioutil.TempFile("", "go-sqlite-test-")
  320. Expect(err).To(Succeed())
  321. f.Close()
  322. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  323. Expect(err).To(Succeed())
  324. var rowUser struct {
  325. ID int64 `field:"id" table:"users"`
  326. Name string `field:"name"`
  327. }
  328. var size int
  329. Expect(db.DeleteRowByID(ctx, 2, &rowUser)).To(Succeed())
  330. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  331. Expect(err).To(Succeed())
  332. Expect(size).To(Equal(1))
  333. Expect(db.DeleteRowByID(ctx, 1, &rowUser)).To(Succeed())
  334. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  335. Expect(err).To(Succeed())
  336. Expect(size).To(Equal(0))
  337. Expect(db.Close()).To(Succeed())
  338. })
  339. It("open connection, migrate and insert row", func() {
  340. f, err := ioutil.TempFile("", "go-sqlite-test-")
  341. Expect(err).To(Succeed())
  342. f.Close()
  343. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  344. Expect(err).To(Succeed())
  345. var rowUser struct {
  346. ID int64 `field:"id" table:"users"`
  347. Name string `field:"name"`
  348. }
  349. var size int
  350. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  351. Expect(err).To(Succeed())
  352. Expect(size).To(Equal(2))
  353. rowUser.Name = "James"
  354. Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
  355. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  356. Expect(err).To(Succeed())
  357. Expect(size).To(Equal(3))
  358. rowUser.Name = "Robert"
  359. Expect(db.InsertRow(ctx, &rowUser)).To(Succeed())
  360. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  361. Expect(err).To(Succeed())
  362. Expect(size).To(Equal(4))
  363. names := []string{}
  364. err = db.Each(
  365. ctx,
  366. "SELECT 0, name FROM users ORDER BY name ASC",
  367. func(ctx context.Context, rows *gosql.Rows) error {
  368. if err := rows.Scans(&rowUser); err != nil {
  369. return err
  370. }
  371. names = append(names, rowUser.Name)
  372. return nil
  373. },
  374. )
  375. Expect(err).To(Succeed())
  376. Expect(names).To(Equal([]string{"Alice", "Bob", "James", "Robert"}))
  377. Expect(db.Close()).To(Succeed())
  378. })
  379. It("open connection, migrate and update row", func() {
  380. f, err := ioutil.TempFile("", "go-sqlite-test-")
  381. Expect(err).To(Succeed())
  382. f.Close()
  383. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  384. Expect(err).To(Succeed())
  385. var rowUser struct {
  386. ID int64 `field:"id" table:"users"`
  387. Name string `field:"name"`
  388. }
  389. _, err = db.Exec(ctx, "INSERT INTO users (id, name) VALUES ('3', 'Patrik')")
  390. Expect(err).To(Succeed())
  391. rowUser.ID = 3
  392. rowUser.Name = "Robert"
  393. Expect(db.UpdateRow(ctx, &rowUser)).To(Succeed())
  394. err = db.QueryRowByID(ctx, 3, &rowUser)
  395. Expect(err).To(Succeed())
  396. Expect(rowUser.ID).To(Equal(int64(3)))
  397. Expect(rowUser.Name).To(Equal("Robert"))
  398. Expect(db.Close()).To(Succeed())
  399. })
  400. })
  401. It("open connection and skip migration", func() {
  402. f, err := ioutil.TempFile("", "go-sqlite-test-")
  403. Expect(err).To(Succeed())
  404. f.Close()
  405. db, err := gosql.Open("sqlite://"+f.Name(), "", true, false)
  406. Expect(err).To(Succeed())
  407. Expect(db.Ping(ctx)).To(Succeed())
  408. var size int
  409. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  410. Expect(err.Error()).To(Equal("no such table: users"))
  411. })
  412. })
  413. })
  414. func TestSuite(t *testing.T) {
  415. RegisterFailHandler(Fail)
  416. RunSpecs(t, "gosql")
  417. }