sqlite3_go18_test.go 3.5 KB

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