github.com/CanonicalLtd/go-sqlite3@v1.6.0/sqlite3_go18_test.go (about) 1 // Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>. 2 // 3 // Use of this source code is governed by an MIT-style 4 // license that can be found in the LICENSE file. 5 6 // +build go1.8 7 8 package sqlite3 9 10 import ( 11 "context" 12 "database/sql" 13 "fmt" 14 "math/rand" 15 "os" 16 "testing" 17 "time" 18 ) 19 20 func TestNamedParams(t *testing.T) { 21 tempFilename := TempFilename(t) 22 defer os.Remove(tempFilename) 23 db, err := sql.Open("sqlite3", tempFilename) 24 if err != nil { 25 t.Fatal("Failed to open database:", err) 26 } 27 defer db.Close() 28 29 _, err = db.Exec(` 30 create table foo (id integer, name text, extra text); 31 `) 32 if err != nil { 33 t.Error("Failed to call db.Query:", err) 34 } 35 36 _, err = db.Exec(`insert into foo(id, name, extra) values(:id, :name, :name)`, sql.Named("name", "foo"), sql.Named("id", 1)) 37 if err != nil { 38 t.Error("Failed to call db.Exec:", err) 39 } 40 41 row := db.QueryRow(`select id, extra from foo where id = :id and extra = :extra`, sql.Named("id", 1), sql.Named("extra", "foo")) 42 if row == nil { 43 t.Error("Failed to call db.QueryRow") 44 } 45 var id int 46 var extra string 47 err = row.Scan(&id, &extra) 48 if err != nil { 49 t.Error("Failed to db.Scan:", err) 50 } 51 if id != 1 || extra != "foo" { 52 t.Error("Failed to db.QueryRow: not matched results") 53 } 54 } 55 56 var ( 57 testTableStatements = []string{ 58 `DROP TABLE IF EXISTS test_table`, 59 ` 60 CREATE TABLE IF NOT EXISTS test_table ( 61 key1 VARCHAR(64) PRIMARY KEY, 62 key_id VARCHAR(64) NOT NULL, 63 key2 VARCHAR(64) NOT NULL, 64 key3 VARCHAR(64) NOT NULL, 65 key4 VARCHAR(64) NOT NULL, 66 key5 VARCHAR(64) NOT NULL, 67 key6 VARCHAR(64) NOT NULL, 68 data BLOB NOT NULL 69 );`, 70 } 71 letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 72 ) 73 74 func randStringBytes(n int) string { 75 b := make([]byte, n) 76 for i := range b { 77 b[i] = letterBytes[rand.Intn(len(letterBytes))] 78 } 79 return string(b) 80 } 81 82 func initDatabase(t *testing.T, db *sql.DB, rowCount int64) { 83 t.Logf("Executing db initializing statements") 84 for _, query := range testTableStatements { 85 _, err := db.Exec(query) 86 if err != nil { 87 t.Fatal(err) 88 } 89 } 90 for i := int64(0); i < rowCount; i++ { 91 query := `INSERT INTO test_table 92 (key1, key_id, key2, key3, key4, key5, key6, data) 93 VALUES 94 (?, ?, ?, ?, ?, ?, ?, ?);` 95 args := []interface{}{ 96 randStringBytes(50), 97 fmt.Sprint(i), 98 randStringBytes(50), 99 randStringBytes(50), 100 randStringBytes(50), 101 randStringBytes(50), 102 randStringBytes(50), 103 randStringBytes(50), 104 randStringBytes(2048), 105 } 106 _, err := db.Exec(query, args...) 107 if err != nil { 108 t.Fatal(err) 109 } 110 } 111 } 112 113 func TestShortTimeout(t *testing.T) { 114 srcTempFilename := TempFilename(t) 115 defer os.Remove(srcTempFilename) 116 117 db, err := sql.Open("sqlite3", srcTempFilename) 118 if err != nil { 119 t.Fatal(err) 120 } 121 defer db.Close() 122 initDatabase(t, db, 100) 123 124 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond) 125 defer cancel() 126 query := `SELECT key1, key_id, key2, key3, key4, key5, key6, data 127 FROM test_table 128 ORDER BY key2 ASC` 129 _, err = db.QueryContext(ctx, query) 130 if err != nil && err != context.DeadlineExceeded { 131 t.Fatal(err) 132 } 133 if ctx.Err() != nil && ctx.Err() != context.DeadlineExceeded { 134 t.Fatal(ctx.Err()) 135 } 136 } 137 138 func TestExecCancel(t *testing.T) { 139 db, err := sql.Open("sqlite3", ":memory:") 140 if err != nil { 141 t.Fatal(err) 142 } 143 defer db.Close() 144 145 if _, err = db.Exec("create table foo (id integer primary key)"); err != nil { 146 t.Fatal(err) 147 } 148 149 for n := 0; n < 100; n++ { 150 ctx, cancel := context.WithCancel(context.Background()) 151 _, err = db.ExecContext(ctx, "insert into foo (id) values (?)", n) 152 cancel() 153 if err != nil { 154 t.Fatal(err) 155 } 156 } 157 }