-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrows_test.go
248 lines (207 loc) · 6.63 KB
/
rows_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package sqle
import (
"database/sql"
"testing"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/require"
)
func TestRows(t *testing.T) {
d, err := sql.Open("sqlite3", "file::memory:")
require.NoError(t, err)
_, err = d.Exec("CREATE TABLE `rows` (`id` int , `status` tinyint,`email` varchar(50),`passwd` varchar(120), `salt` varchar(45), `created` DATETIME, PRIMARY KEY (`id`))")
require.NoError(t, err)
now := time.Now()
_, err = d.Exec("INSERT INTO `rows`(`id`,`status`,`email`,`passwd`,`salt`,`created`) VALUES(1, 1,'[email protected]','1xxxx','1zzzz', ?)", now)
require.NoError(t, err)
_, err = d.Exec("INSERT INTO `rows`(`id`,`status`,`email`,`passwd`,`salt`) VALUES(2, 2,'[email protected]','2xxxx','2zzzz')")
require.NoError(t, err)
_, err = d.Exec("INSERT INTO `rows`(`id`,`status`,`email`,`passwd`,`salt`) VALUES(3, 3,'[email protected]','3xxxx','3zzzz')")
require.NoError(t, err)
_, err = d.Exec("INSERT INTO `rows`(`id`) VALUES(4)")
require.NoError(t, err)
db := Open(d)
tests := []struct {
name string
run func(t *testing.T)
}{
{
name: "close_should_always_work",
run: func(*testing.T) {
rows := &Rows{}
rows.Close()
},
},
{
name: "bind_only_work_with_non_nil_pointer",
run: func(t *testing.T) {
rows := &Rows{}
var dest int
err := rows.Bind(dest)
require.ErrorIs(t, err, ErrMustPointer)
var dest2 *int
err = rows.Bind(dest2)
require.ErrorIs(t, err, ErrMustNotNilPointer)
},
},
{
name: "scan_on_rows_should_work",
run: func(t *testing.T) {
rows, err := db.Query("SELECT id,email FROM rows WHERE id<4")
require.NoError(t, err)
defer rows.Close()
var id int
var email string
rows.Next()
err = rows.Scan(&id, &email)
require.NoError(t, err)
require.Equal(t, 1, id)
require.Equal(t, "[email protected]", email)
rows.Next()
err = rows.Scan(&id, &email)
require.NoError(t, err)
require.Equal(t, 2, id)
require.Equal(t, "[email protected]", email)
rows.Next()
err = rows.Scan(&id, &email)
require.NoError(t, err)
require.Equal(t, 3, id)
require.Equal(t, "[email protected]", email)
},
},
{
name: "bind_slice_of_struct_should_work",
run: func(t *testing.T) {
type user struct {
ID int
Status int
Email string
Passwd string
Salt string
Created *time.Time
}
rows, err := db.Query("SELECT * FROM rows WHERE id<4")
require.NoError(t, err)
var users []user
err = rows.Bind(&users)
require.NoError(t, err)
require.Len(t, users, 3)
require.Equal(t, 1, users[0].ID)
require.Equal(t, 2, users[1].ID)
require.Equal(t, 3, users[2].ID)
users2 := make([]user, 0, 3)
rows, err = db.Query("SELECT * FROM rows WHERE id<4")
require.NoError(t, err)
err = rows.Bind(&users2)
require.NoError(t, err)
require.Len(t, users2, 3)
require.Equal(t, 1, users2[0].ID)
require.Equal(t, 2, users2[1].ID)
require.Equal(t, 3, users2[2].ID)
},
},
{
name: "bind_slice_of_map_should_work",
run: func(t *testing.T) {
rows, err := db.Query("SELECT * FROM rows WHERE id<4")
require.NoError(t, err)
var users []map[string]interface{}
err = rows.Bind(&users)
require.NoError(t, err)
require.Len(t, users, 3)
require.EqualValues(t, 1, users[0]["id"])
require.EqualValues(t, 2, users[1]["id"])
require.EqualValues(t, 3, users[2]["id"])
rows, err = db.Query("SELECT * FROM rows WHERE id<4")
require.NoError(t, err)
var strUsers []map[string]interface{}
err = rows.Bind(&strUsers)
require.NoError(t, err)
require.Len(t, users, 3)
require.EqualValues(t, int64(1), strUsers[0]["id"])
require.EqualValues(t, int64(2), strUsers[1]["id"])
require.EqualValues(t, int64(3), strUsers[2]["id"])
},
},
{
name: "bind_slice_of_primitive_should_work",
run: func(t *testing.T) {
rows, err := db.Query("SELECT id,status FROM rows WHERE id<4")
require.NoError(t, err)
var users [][]int
err = rows.Bind(&users)
require.NoError(t, err)
require.Len(t, users, 3)
require.EqualValues(t, 1, users[0][0])
require.EqualValues(t, 2, users[1][0])
require.EqualValues(t, 3, users[2][0])
var strUsers [][]string
rows, err = db.Query("SELECT email, passwd FROM rows WHERE id<4")
require.NoError(t, err)
err = rows.Bind(&strUsers)
require.NoError(t, err)
require.Len(t, users, 3)
require.EqualValues(t, "[email protected]", strUsers[0][0])
require.EqualValues(t, "[email protected]", strUsers[1][0])
require.EqualValues(t, "[email protected]", strUsers[2][0])
require.EqualValues(t, "1xxxx", strUsers[0][1])
require.EqualValues(t, "2xxxx", strUsers[1][1])
require.EqualValues(t, "3xxxx", strUsers[2][1])
},
},
{
name: "bind_slice_of_scanner_should_work",
run: func(t *testing.T) {
rows, err := db.Query("SELECT id,null as status, email FROM rows WHERE id<4")
require.NoError(t, err)
var ns [][]NullStr
err = rows.Bind(&ns)
require.NoError(t, err)
require.Len(t, ns, 3)
require.EqualValues(t, "1", ns[0][0].String)
require.EqualValues(t, "2", ns[1][0].String)
require.EqualValues(t, "3", ns[2][0].String)
require.False(t, ns[0][1].Valid)
require.False(t, ns[1][1].Valid)
require.False(t, ns[2][1].Valid)
rows, err = db.Query("SELECT id,null as status, email FROM rows WHERE id<4")
require.NoError(t, err)
var users [][]sql.NullString
err = rows.Bind(&users)
require.NoError(t, err)
require.Len(t, users, 3)
require.EqualValues(t, "1", users[0][0].String)
require.EqualValues(t, "2", users[1][0].String)
require.EqualValues(t, "3", users[2][0].String)
require.False(t, users[0][1].Valid)
require.False(t, users[1][1].Valid)
require.False(t, users[2][1].Valid)
},
},
{
name: "bind_slice_of_custom_binder_should_work",
run: func(t *testing.T) {
rows, err := db.Query("SELECT * FROM rows WHERE id<4")
require.NoError(t, err)
var users []customBinder
err = rows.Bind(&users)
require.NoError(t, err)
require.Len(t, users, 3)
require.EqualValues(t, 1, users[0].UserID)
require.EqualValues(t, 2, users[1].UserID)
require.EqualValues(t, 3, users[2].UserID)
require.EqualValues(t, 1, users[0].Status)
require.EqualValues(t, 2, users[1].Status)
require.EqualValues(t, 3, users[2].Status)
require.EqualValues(t, "[email protected]", users[0].Email)
require.EqualValues(t, "[email protected]", users[1].Email)
require.EqualValues(t, "[email protected]", users[2].Email)
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.run(t)
})
}
}