-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
sqlx_test.go
75 lines (62 loc) · 1.43 KB
/
sqlx_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
package sqlx
/*
import (
"reflect"
"testing"
sqlmock "github.com/DATA-DOG/go-sqlmock"
)
type food struct {
ID string
Name string
Presenter bool `db:"-"`
}
func TestTableBind(t *testing.T) {
Register("foods", food{})
db, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
mock.ExpectQuery("SELECT .* FROM foods WHERE id = ?").
WithArgs("42").
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).
AddRow("42", "banana").
AddRow("43", "broccoli"))
rows, err := db.Query("SELECT .* FROM foods WHERE id = ? LIMIT 1", "42")
if err != nil {
t.Fatal(err)
}
var f food
err = Bind(&f, rows)
if err != nil {
t.Fatal(err)
}
expectedSingle := food{"42", "banana", false}
if !reflect.DeepEqual(f, expectedSingle) {
t.Fatalf("expected value: %#+v but got: %#+v", expectedSingle, f)
}
mock.ExpectQuery("SELECT .* FROM foods").
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).
AddRow("42", "banana").
AddRow("43", "broccoli").
AddRow("44", "chicken"))
rows, err = db.Query("SELECT .* FROM foods")
if err != nil {
t.Fatal(err)
}
var foods []food
err = Bind(&foods, rows)
if err != nil {
t.Fatal(err)
}
expectedMany := []food{
{"42", "banana", false},
{"43", "broccoli", false},
{"44", "chicken", false},
}
for i := range foods {
if !reflect.DeepEqual(foods[i], expectedMany[i]) {
t.Fatalf("[%d] expected: %#+v but got: %#+v", i, expectedMany[i], foods[i])
}
}
}
*/