-
Notifications
You must be signed in to change notification settings - Fork 28
/
sqlz_test.go
40 lines (34 loc) · 1.07 KB
/
sqlz_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
package sqlz
import (
"testing"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
)
type test struct {
name string
stmt SQLStmt
expectedSQL string
expectedBindings []interface{}
}
func runTests(t *testing.T, source func(dbz *DB) []test) {
db, _, err := sqlmock.New()
if err != nil {
t.Fatalf("Failed creating mock database: %s", err)
}
for _, tst := range source(New(db, "sqlmock")) {
t.Run(tst.name, func(t *testing.T) {
resultingSQL, resultingBindings := tst.stmt.ToSQL(true)
if resultingSQL != tst.expectedSQL {
t.Errorf("Failed %s: expected %s, got %s", tst.name, tst.expectedSQL, resultingSQL)
}
if len(tst.expectedBindings) != len(resultingBindings) {
t.Errorf("Failed %s: expected %d bindings, got %d", tst.name, len(tst.expectedBindings), len(resultingBindings))
} else {
for i := range tst.expectedBindings {
if tst.expectedBindings[i] != resultingBindings[i] {
t.Errorf("Failed %s: expected binding %d to be %v, got %v", tst.name, i+1, tst.expectedBindings[i], resultingBindings[i])
}
}
}
})
}
}