-
Notifications
You must be signed in to change notification settings - Fork 15
/
conn_test.go
325 lines (296 loc) · 8 KB
/
conn_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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package csvq
import (
"context"
"database/sql"
"reflect"
"testing"
"github.com/mithrandie/csvq/lib/query"
)
func TestConn_BeginTx(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), waitTimeoutForTests)
defer cancel()
db, _ := sql.Open("csvq", TestDir)
defer func() {
_ = db.Close()
}()
txOptions := &sql.TxOptions{
Isolation: sql.LevelReadUncommitted,
ReadOnly: false,
}
expectErr := "csvq does not support non-default isolation level"
_, err := db.BeginTx(ctx, txOptions)
if err == nil {
t.Fatalf("no error, want error %q", expectErr)
}
if err.Error() != expectErr {
t.Fatalf("error = %q, want error %q", err.Error(), expectErr)
}
txOptions = &sql.TxOptions{
Isolation: sql.LevelDefault,
ReadOnly: true,
}
expectErr = "csvq does not support read-only transactions"
_, err = db.BeginTx(ctx, txOptions)
if err == nil {
t.Fatalf("no error, want error %q", expectErr)
}
if err.Error() != expectErr {
t.Fatalf("error = %q, want error %q", err.Error(), expectErr)
}
txOptions = &sql.TxOptions{
Isolation: sql.LevelDefault,
ReadOnly: false,
}
tx, err := db.BeginTx(ctx, txOptions)
if err != nil {
t.Fatalf("unexpected error %q", err.Error())
}
err = tx.Rollback()
if err != nil {
t.Fatalf("unexpected error %q", err.Error())
}
}
func TestConn_QueryContext(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), waitTimeoutForTests)
defer cancel()
db, _ := sql.Open("csvq", TestDir)
defer func() {
_ = db.Close()
}()
queryString := "SELECT FROM `notexist.csv`"
expectErr := "[L:1 C:8] syntax error: unexpected token \"FROM\""
err := matchRows(ctx, db, nil, queryString)
if err == nil {
t.Fatalf("no error, want error %q", expectErr)
}
if _, ok := err.(query.Error); !ok {
t.Fatal("error type is not a query.Error")
}
if err.Error() != expectErr {
t.Fatalf("error = %q, want error %q", err.Error(), expectErr)
}
queryString = "SELECT INTEGER(col1) AS col1, col2 FROM `notexist.csv`"
expectErr = "[L:1 C:41] file `notexist.csv` does not exist"
err = matchRows(ctx, db, nil, queryString)
if err == nil {
t.Fatalf("no error, want error %q", expectErr)
}
if _, ok := err.(query.Error); !ok {
t.Fatal("error type is not a query.Error")
}
if err.Error() != expectErr {
t.Fatalf("error = %q, want error %q", err.Error(), expectErr)
}
queryString = "SELECT INTEGER(col1) AS col1, col2 FROM `table_q.csv`"
expectColumns := []string{"col1", "col2"}
expectResult := [][]interface{}{
{1, "str1"},
{2, "str2"},
{3, "str3"},
}
rs3, err := db.QueryContext(ctx, queryString)
if err != nil {
t.Fatalf("unexpected error %q", err.Error())
}
defer func() {
if err := rs3.Close(); err != nil {
println(err.Error())
}
}()
columns, err := rs3.Columns()
if err != nil {
t.Fatalf("unexpected error %q", err.Error())
}
if !reflect.DeepEqual(columns, expectColumns) {
t.Fatalf("columns = %s, want %s", columns, expectColumns)
}
result, err := scanRows(rs3)
if err != nil {
t.Fatalf("unexpected error %q", err.Error())
}
if !reflect.DeepEqual(result, expectResult) {
t.Fatalf("result = %s, want %s", result, expectResult)
}
queryString = "SELECT INTEGER(col1) AS col1, col2 FROM `table_q.csv` WHERE col1 = 'notexist'"
expectError := sql.ErrNoRows
r := db.QueryRowContext(ctx, queryString)
_, err = scanRow(r)
if err == nil {
t.Fatalf("no error, want error %q", expectErr)
}
if err != expectError {
t.Fatalf("error = %q, want error %q", err, expectError)
}
queryString = "SELECT INTEGER(col1) AS col1, col2 FROM `table_q.csv` WHERE col1 = 2"
expectRowResult := []interface{}{2, "str2"}
r2 := db.QueryRowContext(ctx, queryString)
rowResult, err := scanRow(r2)
if err != nil {
t.Fatalf("unexpected error %q", err.Error())
}
if !reflect.DeepEqual(rowResult, expectRowResult) {
t.Fatalf("result = %s, want %s", rowResult, expectRowResult)
}
queryString = "SELECT INTEGER(col1) AS col1, col2 FROM `table_q.csv` WHERE col1 = ?"
args := []interface{}{1}
expectResult = [][]interface{}{
{1, "str1"},
}
err = matchRows(ctx, db, expectResult, queryString, args...)
if err != nil {
t.Fatal(err)
}
}
func TestConn_ExecContext(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), waitTimeoutForTests)
defer cancel()
db, _ := sql.Open("csvq", TestDir)
defer func() {
_ = db.Close()
}()
queryString := "UPDATE `table_u.csv` WHERE col1 = 2"
expectErr := "[L:1 C:22] syntax error: unexpected token \"WHERE\""
result, err := db.ExecContext(ctx, queryString)
if err == nil {
t.Fatalf("no error, want error %q", expectErr)
}
if _, ok := err.(query.Error); !ok {
t.Fatal("error type is not a query.Error")
}
if err.Error() != expectErr {
t.Fatalf("error = %q, want error %q", err.Error(), expectErr)
}
queryString = "UPDATE `table_u.csv` SET col2 = 'updated' WHERE col1 = 2"
expectLastInsertIdErr := "csvq does not support LastInsertId()"
expectAffectedRows := int64(1)
result, err = db.ExecContext(ctx, queryString)
if err != nil {
t.Fatalf("unexpected error %q", err.Error())
}
_, err = result.LastInsertId()
if err == nil {
t.Fatalf("no error, want error %q", expectLastInsertIdErr)
}
if err.Error() != expectLastInsertIdErr {
t.Fatalf("error = %q, want error %q", err.Error(), expectLastInsertIdErr)
}
affected, err := result.RowsAffected()
if err != nil {
t.Fatalf("unexpected error %q", err.Error())
}
if affected != expectAffectedRows {
t.Fatalf("affected rows = %d, want %d", affected, expectAffectedRows)
}
queryString = "SELECT INTEGER(col1) AS col1, col2 FROM `table_u.csv`"
expectQueryResult := [][]interface{}{
{1, "str1"},
{2, "updated"},
{3, "str3"},
}
err = matchRows(ctx, db, expectQueryResult, queryString)
if err != nil {
t.Fatal(err)
}
}
var parseDSNTests = []struct {
DSN string
Result DSN
HasError bool
}{
{
DSN: "/path/to/data/directory",
Result: DSN{
repository: "/path/to/data/directory",
timezone: "Local",
datetimeFormat: "",
ansiQuotes: false,
},
HasError: false,
},
{
DSN: "/path/to/data/directory?",
Result: DSN{
repository: "/path/to/data/directory",
timezone: "Local",
datetimeFormat: "",
ansiQuotes: false,
},
HasError: false,
},
{
DSN: "/path/to/data/directory?Timezone=UTC&DatetimeFormat=[\"%d%m%Y\"]&AnsiQuotes=true",
Result: DSN{
repository: "/path/to/data/directory",
timezone: "UTC",
datetimeFormat: "[\"%d%m%Y\"]",
ansiQuotes: true,
},
HasError: false,
},
{
DSN: "/path/to/data/directory?timezone=UTC&datetimeformat=[\"%d%m%Y\"]&ansiquotes=true",
Result: DSN{
repository: "/path/to/data/directory",
timezone: "UTC",
datetimeFormat: "[\"%d%m%Y\"]",
ansiQuotes: true,
},
HasError: false,
},
{
DSN: "/path/to/data/directory?datetimeformat=[\"?%d%m\\\"%Y=&\"]",
Result: DSN{
repository: "/path/to/data/directory",
timezone: "Local",
datetimeFormat: "[\"?%d%m\\\"%Y=&\"]",
ansiQuotes: false,
},
HasError: false,
},
{
DSN: "/path/to/data/directory?timezone&datetimeformat&ansiquotes",
Result: DSN{
repository: "/path/to/data/directory",
timezone: "Local",
datetimeFormat: "",
ansiQuotes: false,
},
HasError: false,
},
{
DSN: "/path/to/data/directory?timezone&datetimeformat&ansiquotes=true&",
Result: DSN{
repository: "/path/to/data/directory",
timezone: "Local",
datetimeFormat: "",
ansiQuotes: true,
},
HasError: false,
},
{
DSN: "/path/to/data/directory?timezone&datetimeformat&ansiquotes=err&",
HasError: true,
},
{
DSN: "/path/to/data/directory?Timezone=UTC&IncorrectParam=true",
HasError: true,
},
}
func TestParseDSN(t *testing.T) {
for _, v := range parseDSNTests {
result, err := ParseDSN(v.DSN)
if v.HasError {
if err == nil {
t.Errorf("%s: no error has returned", v.DSN)
}
continue
}
if err != nil {
t.Errorf("%s: unexpected error", v.DSN)
continue
}
if !reflect.DeepEqual(result, v.Result) {
t.Errorf("%s: DSN is %v, want %v", v.DSN, result, v.Result)
}
}
}