-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathload_test.go
193 lines (171 loc) · 5.08 KB
/
load_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package dbr
import (
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/mailru/dbr/dialect"
"github.com/stretchr/testify/assert"
)
func TestLoad(t *testing.T) {
type testStruct struct {
A string
}
testcases := []struct {
columns []string
expected interface{}
}{
{[]string{"a"}, "a"},
{[]string{"a"}, []string{"a"}},
{[]string{"a"}, testStruct{"a"}},
{[]string{"a"}, &testStruct{"a"}},
{[]string{"a"}, []testStruct{{"a"}}},
{[]string{"a"}, []*testStruct{{"a"}}},
{[]string{"a", "b"}, map[string]interface{}{"a": "a", "b": "b"}},
{[]string{"a", "b"}, &map[string]interface{}{"a": "a", "b": "b"}},
{[]string{"a", "b"}, []map[string]interface{}{{"a": "a", "b": "b"}}},
}
for _, tc := range testcases {
var values []driver.Value
session, dbmock := newSessionMock()
for _, c := range tc.columns {
values = append(values, c)
}
rows := sqlmock.NewRows(tc.columns).AddRow(values...)
dbmock.ExpectQuery("SELECT .+").WillReturnRows(rows)
v := reflect.New(reflect.TypeOf(tc.expected)).Elem().Addr().Interface()
session.Select(tc.columns...).From("table").Load(v)
assert.Equal(t, tc.expected, reflect.Indirect(reflect.ValueOf(v)).Interface())
}
}
func TestLoadWithBytesValue(t *testing.T) {
var values []driver.Value
columns := []string{"fieldname"}
value := []byte("fieldvalue")
session, dbmock := newSessionMock()
values = append(values, value)
rows := sqlmock.NewRows(columns).AddRow(values...)
dbmock.ExpectQuery("SELECT .+").WillReturnRows(rows)
v := reflect.New(reflect.TypeOf(map[string]interface{}(nil))).Elem().Addr().Interface()
session.Select(columns...).From("table").Load(v)
value[0] = byte('a')
assert.Equal(t, map[string]interface{}{"fieldname": []byte("fieldvalue")},
reflect.Indirect(reflect.ValueOf(v)).Interface())
}
func BenchmarkLoad(b *testing.B) {
session, dbmock := newSessionMock()
rows := sqlmock.NewRows([]string{"a", "b", "c"})
for i := 0; i < 100; i++ {
rows = rows.AddRow(1, 2, 3)
}
dbmock.ExpectQuery("SELECT a, b, c FROM table").WillReturnRows(rows)
b.ResetTimer()
for i := 0; i < b.N; i++ {
r := make([]struct {
A int `db:"a"`
B int `db:"b"`
C int
D int `db:"-"`
e int
F int `db:"f"`
G int `db:"g"`
H int `db:"h"`
i int
j int
}, 0, 100)
session.Select("a", "b", "c").From("table").LoadStructs(&r)
}
}
func newSessionMock() (SessionRunner, sqlmock.Sqlmock) {
db, m, err := sqlmock.New()
if err != nil {
panic(err)
}
conn := Connection{DBConn: db, Dialect: dialect.MySQL, EventReceiver: nullReceiver}
return conn.NewSession(nil), m
}
func Test_Load_Scalar(t *testing.T) {
t.Parallel()
var res int
_, err := Load(sqlRows(t, sqlmock.NewRows([]string{"cnt"}).AddRow(123)), &res)
assert.NoError(t, err)
assert.EqualValues(t, 123, res)
}
func Test_Load_ScalarPtr(t *testing.T) {
t.Parallel()
var res *int
_, err := Load(sqlRows(t, sqlmock.NewRows([]string{"cnt"}).AddRow(123)), &res)
assert.NoError(t, err)
expected := new(int)
*expected = 123
assert.EqualValues(t, expected, res)
}
func Test_Load_ScalarSlice(t *testing.T) {
t.Parallel()
var res []int
_, err := Load(sqlRows(t, sqlmock.NewRows([]string{"cnt"}).AddRow(111).AddRow(222).AddRow(333)), &res)
assert.NoError(t, err)
assert.EqualValues(t, []int{111, 222, 333}, res)
}
func Test_Load_ScalarSlicePtr(t *testing.T) {
t.Parallel()
var expected, actual []*int
_, err := Load(sqlRows(t, sqlmock.NewRows([]string{"cnt"}).AddRow(0).AddRow(1).AddRow(2)), &actual)
assert.NoError(t, err)
for k := range make([]int, 3) {
k := k
expected = append(expected, &k)
}
assert.EqualValues(t, expected, actual)
}
type testObj struct {
Field1 string
Field2 int
}
func Test_Load_Struct(t *testing.T) {
t.Parallel()
var res testObj
_, err := Load(sqlRows(t, sqlmock.NewRows([]string{"field1", "field2"}).AddRow("111", 222)), &res)
assert.NoError(t, err)
assert.EqualValues(t, testObj{"111", 222}, res)
}
func Test_Load_StructPtr(t *testing.T) {
t.Parallel()
res := &testObj{}
_, err := Load(sqlRows(t, sqlmock.NewRows([]string{"field1", "field2"}).AddRow("111", 222)), &res)
assert.NoError(t, err)
assert.EqualValues(t, &testObj{"111", 222}, res)
}
func Test_Load_StructSlice(t *testing.T) {
t.Parallel()
var res []testObj
_, err := Load(sqlRows(t, sqlmock.NewRows([]string{"field1", "field2"}).AddRow("111", 222).AddRow("222", 333)), &res)
assert.NoError(t, err)
assert.EqualValues(t, []testObj{{"111", 222}, {"222", 333}}, res)
}
func Test_Load_StructSlicePtr(t *testing.T) {
t.Parallel()
var expected, actual []*testObj
_, err := Load(sqlRows(t, sqlmock.NewRows([]string{"field1", "field2"}).AddRow("0", 0).AddRow("1", 1)), &actual)
assert.NoError(t, err)
for k := range make([]int, 2) {
k := k
expected = append(expected, &testObj{fmt.Sprint(k), k})
}
assert.EqualValues(t, expected, actual)
}
func sqlRows(t *testing.T, mockedRows *sqlmock.Rows) *sql.Rows {
t.Helper()
db, dbmock, err := sqlmock.New()
if err != nil {
t.Error(err)
}
dbmock.ExpectQuery("select").WillReturnRows(mockedRows)
rows, err := db.Query("select")
if err != nil {
t.Error(err)
}
return rows
}