This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstmterrs_test.go
387 lines (359 loc) · 9.46 KB
/
stmterrs_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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package sqlr
// tests for statement error conditions
import (
"context"
"database/sql"
"errors"
"testing"
)
type FakeDB struct {
execErr error
rowsAffected int64
rowsAffectedErr error
lastInsertId int64
lastInsertIdErr error
queryErr error
}
func (db *FakeDB) Exec(query string, args ...interface{}) (sql.Result, error) {
if db.execErr != nil {
return nil, db.execErr
}
return db, nil
}
func (db *FakeDB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
return db.Exec(query, args...)
}
func (db *FakeDB) LastInsertId() (int64, error) {
return db.lastInsertId, db.lastInsertIdErr
}
func (db *FakeDB) RowsAffected() (int64, error) {
return db.rowsAffected, db.rowsAffectedErr
}
func (db *FakeDB) Query(query string, args ...interface{}) (*sql.Rows, error) {
return nil, db.queryErr
}
func (db *FakeDB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
return db.Query(query, args...)
}
func TestSelectStmt1Errors(t *testing.T) {
type Row struct {
ID int64 `sql:"primary key autoincrement"`
Name string
}
type NotARow struct {
ID int `sql:"primary key autoincrement"`
SomethingElse int
}
tests := []struct {
row interface{}
sql string
errPrepare string
errExec string
}{
{
row: Row{},
sql: "tablename",
errExec: "expected rows to be *[]github.com/jjeffery/sqlr.Row, *[]*github.com/jjeffery/sqlr.Row, or *github.com/jjeffery/sqlr.Row",
},
{
row: nil,
sql: "tablename",
errExec: "nil pointer",
},
{
row: (*Row)(nil),
sql: "tablename",
errExec: "nil pointer",
},
{
row: &NotARow{},
sql: "tablename",
errExec: "expected rows to be *[]github.com/jjeffery/sqlr.Row, *[]*github.com/jjeffery/sqlr.Row, or *github.com/jjeffery/sqlr.Row",
},
{
row: Row{},
sql: "select {} from {} where {}",
errPrepare: `cannot expand "{}" in "select from" clause`,
},
{
row: Row{},
sql: "select {dodgy¥} from xx where {}",
errPrepare: `cannot expand "dodgy¥" in "select columns" clause: unrecognised input near "¥"`,
},
}
for i, tt := range tests {
schema := NewSchema()
ctx := context.Background()
stmt, err := schema.Prepare(Row{}, tt.sql)
if tt.errPrepare == "" {
if err != nil {
t.Errorf("%d: expected no error, got %v", i, err)
}
} else {
if err == nil {
t.Errorf("%d: expected %q, got nil", i, tt.errPrepare)
} else if err.Error() != tt.errPrepare {
t.Errorf("%d: expected %q, got %v", i, tt.errPrepare, err)
}
}
db := &FakeDB{}
if err == nil {
_, err = stmt.selectRows(ctx, db, tt.row)
if err == nil || err.Error() != tt.errExec {
t.Errorf("test case %d:\nwant=%q\ngot=%q", i, tt.errExec, err)
}
}
}
}
func TestSelectStmt2Errors(t *testing.T) {
type Row struct {
ID int64 `sql:"primary key autoincrement"`
Name string
}
type NotARow struct {
ID int `sql:"primary key autoincrement"`
SomethingElse int
}
const errorTypePtr = "expected rows to be " +
"*[]github.com/jjeffery/sqlr.Row, " +
"*[]*github.com/jjeffery/sqlr.Row, or " +
"*github.com/jjeffery/sqlr.Row"
var invalidSlice []NotARow
var validRows []Row
tests := []struct {
dest interface{}
sql string
args []interface{}
queryErr error
errText string
errPrepare string
}{
{
dest: []Row{},
errText: errorTypePtr,
},
{
dest: make([]Row, 0),
errText: errorTypePtr,
},
{
dest: nil,
errText: "nil pointer",
},
{
dest: (*Row)(nil),
errText: "nil pointer",
},
{
dest: &NotARow{},
errText: errorTypePtr,
},
{
dest: &invalidSlice,
errText: errorTypePtr,
},
{
dest: &validRows,
sql: "select {} from table {} where {}",
errPrepare: `cannot expand "{}" in "select from" clause`,
args: []interface{}{},
},
{
dest: &validRows,
sql: "select {} from table where name=?",
queryErr: errors.New("test query error"),
errText: `test query error`,
args: []interface{}{"somevalue"},
},
}
for i, tt := range tests {
schema := NewSchema()
ctx := context.Background()
stmt, err := schema.Prepare(Row{}, tt.sql)
if tt.errPrepare == "" {
if err != nil {
t.Errorf("%d: expected no error, got %q", i, err)
}
} else {
if err == nil {
t.Errorf("%d: expected %q, got no error", i, tt.errPrepare)
} else if err.Error() != tt.errPrepare {
t.Errorf("%d:\nexpected %q,\ngot %q", i, tt.errPrepare, err)
}
}
if err != nil {
continue
}
db := &FakeDB{queryErr: tt.queryErr}
_, err = stmt.selectRows(ctx, db, tt.dest, tt.args...)
if err == nil || err.Error() != tt.errText {
t.Errorf("%d: want=%q\ngot=%q", i, tt.errText, err)
}
}
}
func TestInsertRowStmtErrors(t *testing.T) {
type Row struct {
ID int64 `sql:"primary key autoincrement"`
Name string
}
type NotARow struct {
ID int `sql:"primary key autoincrement"`
SomethingElse int
}
tests := []struct {
sql string
row interface{}
execErr error
lastInsertIdErr error
errText string
}{
{
sql: "insert into tablename({}) values({})",
row: &Row{},
execErr: errors.New("test error condition"),
errText: "test error condition",
},
{
sql: "insert into table values {}",
row: &Row{},
errText: `cannot expand "insert values" clause because "insert columns" clause is missing`,
},
{
sql: "insert into table({}) values({all})",
row: &Row{},
errText: `columns for "insert values" clause must match the "insert columns" clause`,
},
}
for i, tt := range tests {
schema := NewSchema()
ctx := context.Background()
db := &FakeDB{
execErr: tt.execErr,
lastInsertIdErr: tt.lastInsertIdErr,
}
sess := NewSession(ctx, db, schema)
_, err := sess.Row(tt.row).Exec(tt.sql)
if err == nil || err.Error() != tt.errText {
t.Errorf("%d: expected=%q, actual=%v", i, tt.errText, err)
}
}
}
func TestExecRowStmtErrors(t *testing.T) {
type Row struct {
ID int64 `sql:"primary key autoincrement"`
Name string
}
type NotARow struct {
ID int `sql:"primary key autoincrement"`
SomethingElse int
}
tests := []struct {
sql string
row interface{}
execErr error
errText string
}{
{
sql: "update tablename set {} where {}",
row: &Row{},
execErr: errors.New("test error condition"),
errText: "test error condition",
},
{
sql: "update table {}",
row: &Row{},
errText: `cannot expand "{}" in "update table" clause`,
},
}
for i, tt := range tests {
schema := NewSchema()
ctx := context.Background()
db := &FakeDB{
execErr: tt.execErr,
}
sess := NewSession(ctx, db, schema)
_, err := sess.Row(tt.row).Exec(tt.sql)
if err == nil || err.Error() != tt.errText {
t.Errorf("%d: expected=%q, actual=%q", i, tt.errText, err)
}
}
}
func TestInvalidStmts(t *testing.T) {
ctx := context.Background()
type Row struct {
ID int64 `sql:"primary key"`
Name string
Number int
}
db, _ := sql.Open("sqlite3", ":memory:")
defer db.Close()
schema := NewSchema(ForDB(db))
sess := NewSession(ctx, db, schema)
defer sess.Close()
var row Row
var notRow int
tests := []struct {
fn func() (interface{}, error)
want string
}{
{
fn: func() (interface{}, error) { return sess.Row(¬Row).Exec("insert into rows({}) values({})") },
want: `expected row type to be a struct, found int`,
},
{
fn: func() (interface{}, error) { return sess.Row(&row).Exec("insert into xyz values({})") },
want: `cannot expand "insert values" clause because "insert columns" clause is missing`,
},
{
fn: func() (interface{}, error) { return sess.Row(&row).Exec("insert into xyz({}) values({pk})") },
want: `columns for "insert values" clause must match the "insert columns" clause`,
},
{
fn: func() (interface{}, error) { return sess.Row(&row).Exec("update {} this is not valid SQL") },
want: `cannot expand "{}" in "update table" clause`,
},
{
fn: func() (interface{}, error) { return sess.Row(&row).Exec("update rows set {} where {} and number=?") },
want: `expected arg count=1, actual=0`,
},
{
fn: func() (interface{}, error) { return sess.Row(&row).Exec("delete from rows where {}") },
want: `no such table: rows`,
},
{
fn: func() (interface{}, error) { return sess.Row(&row).Exec("select {alias} from rows") },
want: `cannot expand "alias" in "select columns" clause: missing ident after 'alias'`,
},
{
fn: func() (interface{}, error) { return sess.Select(&row, "select {'col1} from rows") },
want: `cannot expand "'col1" in "select columns" clause: unrecognised input near "'col1"`,
},
{
fn: func() (interface{}, error) { return sess.Select(¬Row, "select {} from rows") },
want: `expected row type to be a struct, found int`,
},
}
for i, tt := range tests {
_, err := tt.fn()
if err != nil {
if tt.want != err.Error() {
t.Errorf("%d: want %s, got %v", i, tt.want, err.Error())
}
continue
}
t.Errorf("%d: want %s, got nil", i, tt.want)
}
}
func TestInvalidPrepare(t *testing.T) {
schema := NewSchema(WithDialect(ANSISQL))
var notRow []int
_, err := schema.Prepare(notRow, "select {} from rows")
want := `expected row type to be a struct, found int`
if err != nil {
if want != err.Error() {
t.Errorf("want %s, got %v", want, err)
}
} else {
t.Errorf("want %s, got nil", want)
}
}