OSDN Git Service

add sqlite vendor (#48)
[bytom/vapor.git] / vendor / github.com / mattn / go-sqlite3 / sqlite3_go18_test.go
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         for _, query := range testTableStatements {
84                 _, err := db.Exec(query)
85                 if err != nil {
86                         t.Fatal(err)
87                 }
88         }
89         for i := int64(0); i < rowCount; i++ {
90                 query := `INSERT INTO test_table
91                         (key1, key_id, key2, key3, key4, key5, key6, data)
92                         VALUES
93                         (?, ?, ?, ?, ?, ?, ?, ?);`
94                 args := []interface{}{
95                         randStringBytes(50),
96                         fmt.Sprint(i),
97                         randStringBytes(50),
98                         randStringBytes(50),
99                         randStringBytes(50),
100                         randStringBytes(50),
101                         randStringBytes(50),
102                         randStringBytes(50),
103                         randStringBytes(2048),
104                 }
105                 _, err := db.Exec(query, args...)
106                 if err != nil {
107                         t.Fatal(err)
108                 }
109         }
110 }
111
112 func TestShortTimeout(t *testing.T) {
113         srcTempFilename := TempFilename(t)
114         defer os.Remove(srcTempFilename)
115
116         db, err := sql.Open("sqlite3", srcTempFilename)
117         if err != nil {
118                 t.Fatal(err)
119         }
120         defer db.Close()
121         initDatabase(t, db, 100)
122
123         ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond)
124         defer cancel()
125         query := `SELECT key1, key_id, key2, key3, key4, key5, key6, data
126                 FROM test_table
127                 ORDER BY key2 ASC`
128         _, err = db.QueryContext(ctx, query)
129         if err != nil && err != context.DeadlineExceeded {
130                 t.Fatal(err)
131         }
132         if ctx.Err() != nil && ctx.Err() != context.DeadlineExceeded {
133                 t.Fatal(ctx.Err())
134         }
135 }
136
137 func TestExecCancel(t *testing.T) {
138         db, err := sql.Open("sqlite3", ":memory:")
139         if err != nil {
140                 t.Fatal(err)
141         }
142         defer db.Close()
143
144         if _, err = db.Exec("create table foo (id integer primary key)"); err != nil {
145                 t.Fatal(err)
146         }
147
148         for n := 0; n < 100; n++ {
149                 ctx, cancel := context.WithCancel(context.Background())
150                 _, err = db.ExecContext(ctx, "insert into foo (id) values (?)", n)
151                 cancel()
152                 if err != nil {
153                         t.Fatal(err)
154                 }
155         }
156 }