-
Notifications
You must be signed in to change notification settings - Fork 2
/
testing_test.go
54 lines (46 loc) · 1.05 KB
/
testing_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
package gosql_test
import (
"database/sql"
"os"
"reflect"
"strings"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/twharmon/gosql"
)
type fataler interface {
Fatal(...interface{})
}
func equals(t *testing.T, a interface{}, b interface{}) {
if reflect.TypeOf(a).Kind() == reflect.Ptr && reflect.TypeOf(b).Kind() == reflect.Ptr {
a = reflect.ValueOf(a).Elem().Interface()
b = reflect.ValueOf(b).Elem().Interface()
}
if a != b {
t.Fatalf("expected %v to equal %v", a, b)
}
}
func contains(t *testing.T, str string, substr string) {
if !strings.Contains(str, substr) {
t.Fatalf("expected %s to contain %s", str, substr)
}
}
func check(f fataler, err error) {
if err != nil {
f.Fatal(err)
}
}
func getMockDB() (*gosql.DB, sqlmock.Sqlmock, error) {
db, mock, err := sqlmock.New()
if err != nil {
panic(err)
}
return gosql.New(db), mock, err
}
func getSQLiteDB(f fataler, q string) *gosql.DB {
os.Remove("/tmp/foo.db")
sqliteDB, err := sql.Open("sqlite3", "/tmp/foo.db")
check(f, err)
sqliteDB.Exec(q)
return gosql.New(sqliteDB)
}