-
Notifications
You must be signed in to change notification settings - Fork 9
/
driver_test.go
132 lines (112 loc) · 3.91 KB
/
driver_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package hotload_test
import (
"database/sql"
"database/sql/driver"
"os"
"github.com/DATA-DOG/go-sqlmock"
"github.com/infobloxopen/hotload"
"github.com/infobloxopen/hotload/fsnotify"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func getDriverFromSqlMock() driver.Driver {
littleBuddy, mock, _ := sqlmock.NewWithDSN("user=pqgotest dbname=pqgotest sslmode=verify-full")
mockDriver = mock
return littleBuddy.Driver()
}
func getRandomDriver() driver.Driver {
db, _, _ := sqlmock.New()
return db.Driver()
}
var mockDriver sqlmock.Sqlmock
var configFile string
var configFileDir string
var _ = Describe("Driver", func() {
BeforeSuite(func() {
driver := getDriverFromSqlMock()
if driver == nil {
Fail("driver is nil, boo!")
}
hotload.RegisterSQLDriver("sqlmock", driver)
Expect(hotload.SQLDrivers()).To(ContainElement("sqlmock"))
var err error
configFile, err = os.Getwd()
Expect(err).ToNot(HaveOccurred())
configFileDir = configFile + "/testdata/"
configFile += "/testdata/myconfig.txt"
})
Context("RegisterSQLDriver", func() {
It("Should panic when registering the same driver twice", func() {
driver := getRandomDriver()
Expect(func() { hotload.RegisterSQLDriver("sqlmock", driver) }).
To(PanicWith(MatchRegexp("Register called twice for driver")))
})
It("Should panic on nil driver", func() {
Expect(func() { hotload.RegisterSQLDriver("", nil) }).
To(PanicWith(MatchRegexp("Register driver is nil")))
})
})
Context("RegisterStrategy", func() {
It("Should panic when registering the same strategy twice", func() {
strat := fsnotify.NewStrategy()
Expect(func() { hotload.RegisterStrategy("fsnotify", strat) }).
To(PanicWith(MatchRegexp("RegisterStrategy called twice for strategy")))
})
It("Should panic on nil driver", func() {
Expect(func() { hotload.RegisterStrategy("", nil) }).
To(PanicWith(MatchRegexp("strategy is nil")))
})
})
Context("Open", func() {
It("Should throw an error with unknown driver", func() {
db, err := sql.Open("hotload", "fsnotify://sqlmaybe?"+configFile)
Expect(err).ToNot(HaveOccurred())
err = db.Ping()
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(hotload.ErrUnknownDriver))
})
It("Should not throw an error with a registered driver and strategy", func() {
db, err := sql.Open("hotload", "fsnotify://sqlmock"+configFile)
Expect(err).ToNot(HaveOccurred())
Expect(db.Ping()).ToNot(HaveOccurred())
})
It("Should throw an unsupported strategy error", func() {
db, err := sql.Open("hotload", "fstransmogrify://sqlmock/"+configFile)
err = db.Ping()
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(hotload.ErrUnsupportedStrategy))
})
It("Should throw an error if it can't find the config file", func() {
db, err := sql.Open("hotload", "fsnotify://sqlmock/temple/run/2021-edition")
err = db.Ping()
Expect(err).To(HaveOccurred())
})
It("Should throw an error the url is unparseable", func() {
db, err := sql.Open("hotload", "://")
err = db.Ping()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("missing protocol scheme"))
})
//It("Should close my connection when the connection information changes", func() {
// db, err := sql.Open("hotload", "fsnotify://sqlmock"+configFileDir+"urconfig.txt")
// Expect(err).ToNot(HaveOccurred())
//
// Expect(db.Ping()).ToNot(HaveOccurred())
// // Open dat
// // Do a thing
// // change connection file
// mockDriver.ExpectBegin()
// mockDriver.ExpectExec("SELECT 1")
// mockDriver.ExpectCommit()
// tx, err := db.Begin()
// Expect(err).ToNot(HaveOccurred())
// tx.Exec("SELECT 1")
// err = ioutil.WriteFile(configFileDir+"urconfig.txt", []byte("user=pqgotest dbname=pqgotestorooni sslmode=verify-full"), 0644)
// Expect(err).ToNot(HaveOccurred())
// go func () {
// tx.Commit()
// Expect(mockDriver.ExpectationsWereMet()).ToNot(HaveOccurred())
// }()
//})
})
})