gosql_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. // })
  54. // // Note: you need to up PostgreSQL server for this test case
  55. // Context("for PostgreSQL", func() {
  56. // It("open connection, migrate and select data", func() {
  57. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  58. // Expect(err).To(Succeed())
  59. // err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
  60. // Expect(err).To(Succeed())
  61. // Expect(id).To(Equal(1))
  62. // Expect(name).To(Equal("Alice"))
  63. // err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
  64. // Expect(err).To(Succeed())
  65. // Expect(id).To(Equal(2))
  66. // Expect(name).To(Equal("Bob"))
  67. // Expect(db.Close()).To(Succeed())
  68. // })
  69. // It("open connection, migrate and select by ID", func() {
  70. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false, false)
  71. // Expect(err).To(Succeed())
  72. // var rowUser struct {
  73. // ID int64 `field:"id" table:"users"`
  74. // Name string `field:"name"`
  75. // }
  76. // err = db.QueryRowByID(ctx, 1, &rowUser)
  77. // Expect(err).To(Succeed())
  78. // Expect(rowUser.ID).To(Equal(int64(1)))
  79. // Expect(rowUser.Name).To(Equal("Alice"))
  80. // Expect(db.Close()).To(Succeed())
  81. // })
  82. // })
  83. Context("for SQLite", func() {
  84. It("open connection, migrate and select data", func() {
  85. f, err := ioutil.TempFile("", "go-sqlite-test-")
  86. Expect(err).To(Succeed())
  87. f.Close()
  88. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  89. Expect(err).To(Succeed())
  90. err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
  91. Expect(err).To(Succeed())
  92. Expect(id).To(Equal(1))
  93. Expect(name).To(Equal("Alice"))
  94. err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
  95. Expect(err).To(Succeed())
  96. Expect(id).To(Equal(2))
  97. Expect(name).To(Equal("Bob"))
  98. Expect(db.Close()).To(Succeed())
  99. })
  100. It("open connection, migrate and select by ID", func() {
  101. f, err := ioutil.TempFile("", "go-sqlite-test-")
  102. Expect(err).To(Succeed())
  103. f.Close()
  104. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false, false)
  105. Expect(err).To(Succeed())
  106. var rowUser struct {
  107. ID int64 `field:"id" table:"users"`
  108. Name string `field:"name"`
  109. }
  110. err = db.QueryRowByID(ctx, 1, &rowUser)
  111. Expect(err).To(Succeed())
  112. Expect(rowUser.ID).To(Equal(int64(1)))
  113. Expect(rowUser.Name).To(Equal("Alice"))
  114. Expect(db.Close()).To(Succeed())
  115. })
  116. })
  117. It("open connection and skip migration", func() {
  118. f, err := ioutil.TempFile("", "go-sqlite-test-")
  119. Expect(err).To(Succeed())
  120. f.Close()
  121. db, err := gosql.Open("sqlite://"+f.Name(), "", true, false)
  122. Expect(err).To(Succeed())
  123. Expect(db.Ping(ctx)).To(Succeed())
  124. var size int
  125. err = db.QueryRow(ctx, "select count(*) from users").Scan(&size)
  126. Expect(err.Error()).To(Equal("no such table: users"))
  127. })
  128. })
  129. })
  130. func TestSuite(t *testing.T) {
  131. RegisterFailHandler(Fail)
  132. RunSpecs(t, "gosql")
  133. }