gosql_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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)
  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. // })
  41. // // Note: you need to up PostgreSQL server for this test case
  42. // Context("for PostgreSQL", func() {
  43. // It("open connection, migrate and select data", func() {
  44. // db, err := gosql.Open("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable", migrationsDir, false)
  45. // Expect(err).To(Succeed())
  46. // err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
  47. // Expect(err).To(Succeed())
  48. // Expect(id).To(Equal(1))
  49. // Expect(name).To(Equal("Alice"))
  50. // err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
  51. // Expect(err).To(Succeed())
  52. // Expect(id).To(Equal(2))
  53. // Expect(name).To(Equal("Bob"))
  54. // Expect(db.Close()).To(Succeed())
  55. // })
  56. // })
  57. Context("for SQLite", func() {
  58. It("open connection, migrate and select data", func() {
  59. f, err := ioutil.TempFile("", "go-sqlite-test-")
  60. Expect(err).To(Succeed())
  61. f.Close()
  62. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, false)
  63. Expect(err).To(Succeed())
  64. err = db.QueryRow(ctx, sql, 1).Scan(&id, &name)
  65. Expect(err).To(Succeed())
  66. Expect(id).To(Equal(1))
  67. Expect(name).To(Equal("Alice"))
  68. err = db.QueryRow(ctx, sql, 2).Scan(&id, &name)
  69. Expect(err).To(Succeed())
  70. Expect(id).To(Equal(2))
  71. Expect(name).To(Equal("Bob"))
  72. Expect(db.Close()).To(Succeed())
  73. })
  74. })
  75. })
  76. })
  77. func TestSuite(t *testing.T) {
  78. RegisterFailHandler(Fail)
  79. RunSpecs(t, "gosql")
  80. }