common_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package common_test
  2. import (
  3. "io/ioutil"
  4. "net/url"
  5. "path/filepath"
  6. "testing"
  7. . "github.com/onsi/ginkgo"
  8. . "github.com/onsi/gomega"
  9. "github.com/vladimirok5959/golang-sql/gosql/common"
  10. )
  11. var _ = Describe("common", func() {
  12. Context("ParseUrl", func() {
  13. Context("Success", func() {
  14. It("for MySQL", func() {
  15. // mysql://username:password@127.0.0.1:3306/database?parseTime=true
  16. // mysql://username:password@/database?socket=/var/run/mysqld/mysqld.sock
  17. url := "mysql://username:password@127.0.0.1:3306/database?parseTime=true"
  18. result, err := common.ParseUrl(url)
  19. Expect(err).To(Succeed())
  20. Expect(result.Scheme).To(Equal("mysql"))
  21. Expect(result.User.Username()).To(Equal("username"))
  22. password, whether := result.User.Password()
  23. Expect(password).To(Equal("password"))
  24. Expect(whether).To(BeTrue())
  25. Expect(result.Host).To(Equal("127.0.0.1:3306"))
  26. Expect(result.Path).To(Equal("/database"))
  27. Expect(result.RawQuery).To(Equal("parseTime=true"))
  28. })
  29. It("for PostgreSQL", func() {
  30. // postgres://username:password@127.0.0.1:5432/database?sslmode=disable
  31. // postgresql://username:password@127.0.0.1:5432/database?sslmode=disable
  32. url := "postgres://username:password@127.0.0.1:5432/database?sslmode=disable"
  33. result, err := common.ParseUrl(url)
  34. Expect(err).To(Succeed())
  35. Expect(result.Scheme).To(Equal("postgres"))
  36. Expect(result.User.Username()).To(Equal("username"))
  37. password, whether := result.User.Password()
  38. Expect(password).To(Equal("password"))
  39. Expect(whether).To(BeTrue())
  40. Expect(result.Host).To(Equal("127.0.0.1:5432"))
  41. Expect(result.Path).To(Equal("/database"))
  42. Expect(result.RawQuery).To(Equal("sslmode=disable"))
  43. })
  44. It("for SQLite", func() {
  45. // sqlite:///data/database.sqlite
  46. // sqlite3:///data/database.sqlite
  47. url := "sqlite:///data/database.sqlite"
  48. result, err := common.ParseUrl(url)
  49. Expect(err).To(Succeed())
  50. Expect(result.Scheme).To(Equal("sqlite"))
  51. Expect(result.Host).To(Equal(""))
  52. Expect(result.Path).To(Equal("/data/database.sqlite"))
  53. })
  54. })
  55. Context("Fail", func() {
  56. It("for empty", func() {
  57. _, err := common.ParseUrl("")
  58. Expect(err).NotTo(Succeed())
  59. Expect(err.Error()).To(Equal("protocol scheme is not defined"))
  60. })
  61. It("for else", func() {
  62. url := "12345"
  63. _, err := common.ParseUrl(url)
  64. Expect(err).NotTo(Succeed())
  65. Expect(err.Error()).To(Equal("protocol scheme is not defined"))
  66. })
  67. It("for not supported", func() {
  68. url := "example:///some-else"
  69. _, err := common.ParseUrl(url)
  70. Expect(err).NotTo(Succeed())
  71. Expect(err.Error()).To(Equal("unsupported protocol scheme: example"))
  72. })
  73. })
  74. })
  75. Context("OpenDB", func() {
  76. var migrationsDir string
  77. BeforeEach(func() {
  78. var err error
  79. migrationsDir, err = filepath.Abs("../../db/migrations")
  80. Expect(err).To(Succeed())
  81. })
  82. Context("Success", func() {
  83. // Note: you need to up MySQL server for this test case
  84. It("for MySQL", func() {
  85. databaseURL, err := url.Parse("mysql://root:root@127.0.0.1:3306/gosql")
  86. Expect(err).To(Succeed())
  87. db, err := common.OpenDB(databaseURL, migrationsDir)
  88. Expect(err).To(Succeed())
  89. Expect(db.Close()).To(Succeed())
  90. })
  91. // Note: you need to up PostgreSQL server for this test case
  92. It("for PostgreSQL", func() {
  93. databaseURL, err := url.Parse("postgres://root:root@127.0.0.1:5432/gosql?sslmode=disable")
  94. Expect(err).To(Succeed())
  95. db, err := common.OpenDB(databaseURL, migrationsDir)
  96. Expect(err).To(Succeed())
  97. Expect(db.Close()).To(Succeed())
  98. })
  99. It("for SQLite", func() {
  100. f, err := ioutil.TempFile("", "go-sqlite3-test-")
  101. Expect(err).To(Succeed())
  102. f.Close()
  103. databaseURL, err := url.Parse("sqlite://" + f.Name())
  104. Expect(err).To(Succeed())
  105. db, err := common.OpenDB(databaseURL, migrationsDir)
  106. Expect(err).To(Succeed())
  107. Expect(db.Close()).To(Succeed())
  108. })
  109. })
  110. })
  111. })
  112. func TestSuite(t *testing.T) {
  113. RegisterFailHandler(Fail)
  114. RunSpecs(t, "gosql/common")
  115. }