OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [database/] [sql/] [sql_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package sql
6
 
7
import (
8
        "fmt"
9
        "reflect"
10
        "strings"
11
        "testing"
12
        "time"
13
)
14
 
15
const fakeDBName = "foo"
16
 
17
var chrisBirthday = time.Unix(123456789, 0)
18
 
19
func newTestDB(t *testing.T, name string) *DB {
20
        db, err := Open("test", fakeDBName)
21
        if err != nil {
22
                t.Fatalf("Open: %v", err)
23
        }
24
        if _, err := db.Exec("WIPE"); err != nil {
25
                t.Fatalf("exec wipe: %v", err)
26
        }
27
        if name == "people" {
28
                exec(t, db, "CREATE|people|name=string,age=int32,photo=blob,dead=bool,bdate=datetime")
29
                exec(t, db, "INSERT|people|name=Alice,age=?,photo=APHOTO", 1)
30
                exec(t, db, "INSERT|people|name=Bob,age=?,photo=BPHOTO", 2)
31
                exec(t, db, "INSERT|people|name=Chris,age=?,photo=CPHOTO,bdate=?", 3, chrisBirthday)
32
        }
33
        return db
34
}
35
 
36
func exec(t *testing.T, db *DB, query string, args ...interface{}) {
37
        _, err := db.Exec(query, args...)
38
        if err != nil {
39
                t.Fatalf("Exec of %q: %v", query, err)
40
        }
41
}
42
 
43
func closeDB(t *testing.T, db *DB) {
44
        err := db.Close()
45
        if err != nil {
46
                t.Fatalf("error closing DB: %v", err)
47
        }
48
}
49
 
50
func TestQuery(t *testing.T) {
51
        db := newTestDB(t, "people")
52
        defer closeDB(t, db)
53
        rows, err := db.Query("SELECT|people|age,name|")
54
        if err != nil {
55
                t.Fatalf("Query: %v", err)
56
        }
57
        type row struct {
58
                age  int
59
                name string
60
        }
61
        got := []row{}
62
        for rows.Next() {
63
                var r row
64
                err = rows.Scan(&r.age, &r.name)
65
                if err != nil {
66
                        t.Fatalf("Scan: %v", err)
67
                }
68
                got = append(got, r)
69
        }
70
        err = rows.Err()
71
        if err != nil {
72
                t.Fatalf("Err: %v", err)
73
        }
74
        want := []row{
75
                {age: 1, name: "Alice"},
76
                {age: 2, name: "Bob"},
77
                {age: 3, name: "Chris"},
78
        }
79
        if !reflect.DeepEqual(got, want) {
80
                t.Errorf("mismatch.\n got: %#v\nwant: %#v", got, want)
81
        }
82
 
83
        // And verify that the final rows.Next() call, which hit EOF,
84
        // also closed the rows connection.
85
        if n := len(db.freeConn); n != 1 {
86
                t.Errorf("free conns after query hitting EOF = %d; want 1", n)
87
        }
88
}
89
 
90
func TestByteOwnership(t *testing.T) {
91
        db := newTestDB(t, "people")
92
        defer closeDB(t, db)
93
        rows, err := db.Query("SELECT|people|name,photo|")
94
        if err != nil {
95
                t.Fatalf("Query: %v", err)
96
        }
97
        type row struct {
98
                name  []byte
99
                photo RawBytes
100
        }
101
        got := []row{}
102
        for rows.Next() {
103
                var r row
104
                err = rows.Scan(&r.name, &r.photo)
105
                if err != nil {
106
                        t.Fatalf("Scan: %v", err)
107
                }
108
                got = append(got, r)
109
        }
110
        corruptMemory := []byte("\xffPHOTO")
111
        want := []row{
112
                {name: []byte("Alice"), photo: corruptMemory},
113
                {name: []byte("Bob"), photo: corruptMemory},
114
                {name: []byte("Chris"), photo: corruptMemory},
115
        }
116
        if !reflect.DeepEqual(got, want) {
117
                t.Errorf("mismatch.\n got: %#v\nwant: %#v", got, want)
118
        }
119
 
120
        var photo RawBytes
121
        err = db.QueryRow("SELECT|people|photo|name=?", "Alice").Scan(&photo)
122
        if err == nil {
123
                t.Error("want error scanning into RawBytes from QueryRow")
124
        }
125
}
126
 
127
func TestRowsColumns(t *testing.T) {
128
        db := newTestDB(t, "people")
129
        defer closeDB(t, db)
130
        rows, err := db.Query("SELECT|people|age,name|")
131
        if err != nil {
132
                t.Fatalf("Query: %v", err)
133
        }
134
        cols, err := rows.Columns()
135
        if err != nil {
136
                t.Fatalf("Columns: %v", err)
137
        }
138
        want := []string{"age", "name"}
139
        if !reflect.DeepEqual(cols, want) {
140
                t.Errorf("got %#v; want %#v", cols, want)
141
        }
142
}
143
 
144
func TestQueryRow(t *testing.T) {
145
        db := newTestDB(t, "people")
146
        defer closeDB(t, db)
147
        var name string
148
        var age int
149
        var birthday time.Time
150
 
151
        err := db.QueryRow("SELECT|people|age,name|age=?", 3).Scan(&age)
152
        if err == nil || !strings.Contains(err.Error(), "expected 2 destination arguments") {
153
                t.Errorf("expected error from wrong number of arguments; actually got: %v", err)
154
        }
155
 
156
        err = db.QueryRow("SELECT|people|bdate|age=?", 3).Scan(&birthday)
157
        if err != nil || !birthday.Equal(chrisBirthday) {
158
                t.Errorf("chris birthday = %v, err = %v; want %v", birthday, err, chrisBirthday)
159
        }
160
 
161
        err = db.QueryRow("SELECT|people|age,name|age=?", 2).Scan(&age, &name)
162
        if err != nil {
163
                t.Fatalf("age QueryRow+Scan: %v", err)
164
        }
165
        if name != "Bob" {
166
                t.Errorf("expected name Bob, got %q", name)
167
        }
168
        if age != 2 {
169
                t.Errorf("expected age 2, got %d", age)
170
        }
171
 
172
        err = db.QueryRow("SELECT|people|age,name|name=?", "Alice").Scan(&age, &name)
173
        if err != nil {
174
                t.Fatalf("name QueryRow+Scan: %v", err)
175
        }
176
        if name != "Alice" {
177
                t.Errorf("expected name Alice, got %q", name)
178
        }
179
        if age != 1 {
180
                t.Errorf("expected age 1, got %d", age)
181
        }
182
 
183
        var photo []byte
184
        err = db.QueryRow("SELECT|people|photo|name=?", "Alice").Scan(&photo)
185
        if err != nil {
186
                t.Fatalf("photo QueryRow+Scan: %v", err)
187
        }
188
        want := []byte("APHOTO")
189
        if !reflect.DeepEqual(photo, want) {
190
                t.Errorf("photo = %q; want %q", photo, want)
191
        }
192
}
193
 
194
func TestStatementErrorAfterClose(t *testing.T) {
195
        db := newTestDB(t, "people")
196
        defer closeDB(t, db)
197
        stmt, err := db.Prepare("SELECT|people|age|name=?")
198
        if err != nil {
199
                t.Fatalf("Prepare: %v", err)
200
        }
201
        err = stmt.Close()
202
        if err != nil {
203
                t.Fatalf("Close: %v", err)
204
        }
205
        var name string
206
        err = stmt.QueryRow("foo").Scan(&name)
207
        if err == nil {
208
                t.Errorf("expected error from QueryRow.Scan after Stmt.Close")
209
        }
210
}
211
 
212
func TestStatementQueryRow(t *testing.T) {
213
        db := newTestDB(t, "people")
214
        defer closeDB(t, db)
215
        stmt, err := db.Prepare("SELECT|people|age|name=?")
216
        if err != nil {
217
                t.Fatalf("Prepare: %v", err)
218
        }
219
        var age int
220
        for n, tt := range []struct {
221
                name string
222
                want int
223
        }{
224
                {"Alice", 1},
225
                {"Bob", 2},
226
                {"Chris", 3},
227
        } {
228
                if err := stmt.QueryRow(tt.name).Scan(&age); err != nil {
229
                        t.Errorf("%d: on %q, QueryRow/Scan: %v", n, tt.name, err)
230
                } else if age != tt.want {
231
                        t.Errorf("%d: age=%d, want %d", n, age, tt.want)
232
                }
233
        }
234
 
235
}
236
 
237
// just a test of fakedb itself
238
func TestBogusPreboundParameters(t *testing.T) {
239
        db := newTestDB(t, "foo")
240
        defer closeDB(t, db)
241
        exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool")
242
        _, err := db.Prepare("INSERT|t1|name=?,age=bogusconversion")
243
        if err == nil {
244
                t.Fatalf("expected error")
245
        }
246
        if err.Error() != `fakedb: invalid conversion to int32 from "bogusconversion"` {
247
                t.Errorf("unexpected error: %v", err)
248
        }
249
}
250
 
251
func TestExec(t *testing.T) {
252
        db := newTestDB(t, "foo")
253
        defer closeDB(t, db)
254
        exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool")
255
        stmt, err := db.Prepare("INSERT|t1|name=?,age=?")
256
        if err != nil {
257
                t.Errorf("Stmt, err = %v, %v", stmt, err)
258
        }
259
 
260
        type execTest struct {
261
                args    []interface{}
262
                wantErr string
263
        }
264
        execTests := []execTest{
265
                // Okay:
266
                {[]interface{}{"Brad", 31}, ""},
267
                {[]interface{}{"Brad", int64(31)}, ""},
268
                {[]interface{}{"Bob", "32"}, ""},
269
                {[]interface{}{7, 9}, ""},
270
 
271
                // Invalid conversions:
272
                {[]interface{}{"Brad", int64(0xFFFFFFFF)}, "sql: converting Exec argument #1's type: sql/driver: value 4294967295 overflows int32"},
273
                {[]interface{}{"Brad", "strconv fail"}, "sql: converting Exec argument #1's type: sql/driver: value \"strconv fail\" can't be converted to int32"},
274
 
275
                // Wrong number of args:
276
                {[]interface{}{}, "sql: expected 2 arguments, got 0"},
277
                {[]interface{}{1, 2, 3}, "sql: expected 2 arguments, got 3"},
278
        }
279
        for n, et := range execTests {
280
                _, err := stmt.Exec(et.args...)
281
                errStr := ""
282
                if err != nil {
283
                        errStr = err.Error()
284
                }
285
                if errStr != et.wantErr {
286
                        t.Errorf("stmt.Execute #%d: for %v, got error %q, want error %q",
287
                                n, et.args, errStr, et.wantErr)
288
                }
289
        }
290
}
291
 
292
func TestTxStmt(t *testing.T) {
293
        db := newTestDB(t, "")
294
        defer closeDB(t, db)
295
        exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool")
296
        stmt, err := db.Prepare("INSERT|t1|name=?,age=?")
297
        if err != nil {
298
                t.Fatalf("Stmt, err = %v, %v", stmt, err)
299
        }
300
        tx, err := db.Begin()
301
        if err != nil {
302
                t.Fatalf("Begin = %v", err)
303
        }
304
        _, err = tx.Stmt(stmt).Exec("Bobby", 7)
305
        if err != nil {
306
                t.Fatalf("Exec = %v", err)
307
        }
308
        err = tx.Commit()
309
        if err != nil {
310
                t.Fatalf("Commit = %v", err)
311
        }
312
}
313
 
314
// Issue: http://golang.org/issue/2784
315
// This test didn't fail before because we got luckly with the fakedb driver.
316
// It was failing, and now not, in github.com/bradfitz/go-sql-test
317
func TestTxQuery(t *testing.T) {
318
        db := newTestDB(t, "")
319
        defer closeDB(t, db)
320
        exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool")
321
        exec(t, db, "INSERT|t1|name=Alice")
322
 
323
        tx, err := db.Begin()
324
        if err != nil {
325
                t.Fatal(err)
326
        }
327
        defer tx.Rollback()
328
 
329
        r, err := tx.Query("SELECT|t1|name|")
330
        if err != nil {
331
                t.Fatal(err)
332
        }
333
 
334
        if !r.Next() {
335
                if r.Err() != nil {
336
                        t.Fatal(r.Err())
337
                }
338
                t.Fatal("expected one row")
339
        }
340
 
341
        var x string
342
        err = r.Scan(&x)
343
        if err != nil {
344
                t.Fatal(err)
345
        }
346
}
347
 
348
// Tests fix for issue 2542, that we release a lock when querying on
349
// a closed connection.
350
func TestIssue2542Deadlock(t *testing.T) {
351
        db := newTestDB(t, "people")
352
        closeDB(t, db)
353
        for i := 0; i < 2; i++ {
354
                _, err := db.Query("SELECT|people|age,name|")
355
                if err == nil {
356
                        t.Fatalf("expected error")
357
                }
358
        }
359
}
360
 
361
// Tests fix for issue 2788, that we bind nil to a []byte if the
362
// value in the column is sql null
363
func TestNullByteSlice(t *testing.T) {
364
        db := newTestDB(t, "")
365
        defer closeDB(t, db)
366
        exec(t, db, "CREATE|t|id=int32,name=nullstring")
367
        exec(t, db, "INSERT|t|id=10,name=?", nil)
368
 
369
        var name []byte
370
 
371
        err := db.QueryRow("SELECT|t|name|id=?", 10).Scan(&name)
372
        if err != nil {
373
                t.Fatal(err)
374
        }
375
        if name != nil {
376
                t.Fatalf("name []byte should be nil for null column value, got: %#v", name)
377
        }
378
 
379
        exec(t, db, "INSERT|t|id=11,name=?", "bob")
380
        err = db.QueryRow("SELECT|t|name|id=?", 11).Scan(&name)
381
        if err != nil {
382
                t.Fatal(err)
383
        }
384
        if string(name) != "bob" {
385
                t.Fatalf("name []byte should be bob, got: %q", string(name))
386
        }
387
}
388
 
389
func TestQueryRowClosingStmt(t *testing.T) {
390
        db := newTestDB(t, "people")
391
        defer closeDB(t, db)
392
        var name string
393
        var age int
394
        err := db.QueryRow("SELECT|people|age,name|age=?", 3).Scan(&age, &name)
395
        if err != nil {
396
                t.Fatal(err)
397
        }
398
        if len(db.freeConn) != 1 {
399
                t.Fatalf("expected 1 free conn")
400
        }
401
        fakeConn := db.freeConn[0].(*fakeConn)
402
        if made, closed := fakeConn.stmtsMade, fakeConn.stmtsClosed; made != closed {
403
                t.Errorf("statement close mismatch: made %d, closed %d", made, closed)
404
        }
405
}
406
 
407
type nullTestRow struct {
408
        nullParam    interface{}
409
        notNullParam interface{}
410
        scanNullVal  interface{}
411
}
412
 
413
type nullTestSpec struct {
414
        nullType    string
415
        notNullType string
416
        rows        [6]nullTestRow
417
}
418
 
419
func TestNullStringParam(t *testing.T) {
420
        spec := nullTestSpec{"nullstring", "string", [6]nullTestRow{
421
                nullTestRow{NullString{"aqua", true}, "", NullString{"aqua", true}},
422
                nullTestRow{NullString{"brown", false}, "", NullString{"", false}},
423
                nullTestRow{"chartreuse", "", NullString{"chartreuse", true}},
424
                nullTestRow{NullString{"darkred", true}, "", NullString{"darkred", true}},
425
                nullTestRow{NullString{"eel", false}, "", NullString{"", false}},
426
                nullTestRow{"foo", NullString{"black", false}, nil},
427
        }}
428
        nullTestRun(t, spec)
429
}
430
 
431
func TestNullInt64Param(t *testing.T) {
432
        spec := nullTestSpec{"nullint64", "int64", [6]nullTestRow{
433
                nullTestRow{NullInt64{31, true}, 1, NullInt64{31, true}},
434
                nullTestRow{NullInt64{-22, false}, 1, NullInt64{0, false}},
435
                nullTestRow{22, 1, NullInt64{22, true}},
436
                nullTestRow{NullInt64{33, true}, 1, NullInt64{33, true}},
437
                nullTestRow{NullInt64{222, false}, 1, NullInt64{0, false}},
438
                nullTestRow{0, NullInt64{31, false}, nil},
439
        }}
440
        nullTestRun(t, spec)
441
}
442
 
443
func TestNullFloat64Param(t *testing.T) {
444
        spec := nullTestSpec{"nullfloat64", "float64", [6]nullTestRow{
445
                nullTestRow{NullFloat64{31.2, true}, 1, NullFloat64{31.2, true}},
446
                nullTestRow{NullFloat64{13.1, false}, 1, NullFloat64{0, false}},
447
                nullTestRow{-22.9, 1, NullFloat64{-22.9, true}},
448
                nullTestRow{NullFloat64{33.81, true}, 1, NullFloat64{33.81, true}},
449
                nullTestRow{NullFloat64{222, false}, 1, NullFloat64{0, false}},
450
                nullTestRow{10, NullFloat64{31.2, false}, nil},
451
        }}
452
        nullTestRun(t, spec)
453
}
454
 
455
func TestNullBoolParam(t *testing.T) {
456
        spec := nullTestSpec{"nullbool", "bool", [6]nullTestRow{
457
                nullTestRow{NullBool{false, true}, true, NullBool{false, true}},
458
                nullTestRow{NullBool{true, false}, false, NullBool{false, false}},
459
                nullTestRow{true, true, NullBool{true, true}},
460
                nullTestRow{NullBool{true, true}, false, NullBool{true, true}},
461
                nullTestRow{NullBool{true, false}, true, NullBool{false, false}},
462
                nullTestRow{true, NullBool{true, false}, nil},
463
        }}
464
        nullTestRun(t, spec)
465
}
466
 
467
func nullTestRun(t *testing.T, spec nullTestSpec) {
468
        db := newTestDB(t, "")
469
        defer closeDB(t, db)
470
        exec(t, db, fmt.Sprintf("CREATE|t|id=int32,name=string,nullf=%s,notnullf=%s", spec.nullType, spec.notNullType))
471
 
472
        // Inserts with db.Exec:
473
        exec(t, db, "INSERT|t|id=?,name=?,nullf=?,notnullf=?", 1, "alice", spec.rows[0].nullParam, spec.rows[0].notNullParam)
474
        exec(t, db, "INSERT|t|id=?,name=?,nullf=?,notnullf=?", 2, "bob", spec.rows[1].nullParam, spec.rows[1].notNullParam)
475
 
476
        // Inserts with a prepared statement:
477
        stmt, err := db.Prepare("INSERT|t|id=?,name=?,nullf=?,notnullf=?")
478
        if err != nil {
479
                t.Fatalf("prepare: %v", err)
480
        }
481
        if _, err := stmt.Exec(3, "chris", spec.rows[2].nullParam, spec.rows[2].notNullParam); err != nil {
482
                t.Errorf("exec insert chris: %v", err)
483
        }
484
        if _, err := stmt.Exec(4, "dave", spec.rows[3].nullParam, spec.rows[3].notNullParam); err != nil {
485
                t.Errorf("exec insert dave: %v", err)
486
        }
487
        if _, err := stmt.Exec(5, "eleanor", spec.rows[4].nullParam, spec.rows[4].notNullParam); err != nil {
488
                t.Errorf("exec insert eleanor: %v", err)
489
        }
490
 
491
        // Can't put null val into non-null col
492
        if _, err := stmt.Exec(6, "bob", spec.rows[5].nullParam, spec.rows[5].notNullParam); err == nil {
493
                t.Errorf("expected error inserting nil val with prepared statement Exec")
494
        }
495
 
496
        _, err = db.Exec("INSERT|t|id=?,name=?,nullf=?", 999, nil, nil)
497
        if err == nil {
498
                // TODO: this test fails, but it's just because
499
                // fakeConn implements the optional Execer interface,
500
                // so arguably this is the correct behavior.  But
501
                // maybe I should flesh out the fakeConn.Exec
502
                // implementation so this properly fails.
503
                // t.Errorf("expected error inserting nil name with Exec")
504
        }
505
 
506
        paramtype := reflect.TypeOf(spec.rows[0].nullParam)
507
        bindVal := reflect.New(paramtype).Interface()
508
 
509
        for i := 0; i < 5; i++ {
510
                id := i + 1
511
                if err := db.QueryRow("SELECT|t|nullf|id=?", id).Scan(bindVal); err != nil {
512
                        t.Errorf("id=%d Scan: %v", id, err)
513
                }
514
                bindValDeref := reflect.ValueOf(bindVal).Elem().Interface()
515
                if !reflect.DeepEqual(bindValDeref, spec.rows[i].scanNullVal) {
516
                        t.Errorf("id=%d got %#v, want %#v", id, bindValDeref, spec.rows[i].scanNullVal)
517
                }
518
        }
519
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.