sqlite3_go18_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. t.Logf("Executing db initializing statements")
  74. for _, query := range testTableStatements {
  75. _, err := db.Exec(query)
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. }
  80. for i := int64(0); i < rowCount; i++ {
  81. query := `INSERT INTO test_table
  82. (key1, key_id, key2, key3, key4, key5, key6, data)
  83. VALUES
  84. (?, ?, ?, ?, ?, ?, ?, ?);`
  85. args := []interface{}{
  86. randStringBytes(50),
  87. fmt.Sprint(i),
  88. randStringBytes(50),
  89. randStringBytes(50),
  90. randStringBytes(50),
  91. randStringBytes(50),
  92. randStringBytes(50),
  93. randStringBytes(50),
  94. randStringBytes(2048),
  95. }
  96. _, err := db.Exec(query, args...)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. }
  101. }
  102. func TestShortTimeout(t *testing.T) {
  103. srcTempFilename := TempFilename(t)
  104. defer os.Remove(srcTempFilename)
  105. db, err := sql.Open("sqlite3", srcTempFilename)
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. defer db.Close()
  110. initDatabase(t, db, 100)
  111. ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond)
  112. defer cancel()
  113. query := `SELECT key1, key_id, key2, key3, key4, key5, key6, data
  114. FROM test_table
  115. ORDER BY key2 ASC`
  116. _, err = db.QueryContext(ctx, query)
  117. if err != nil && err != context.DeadlineExceeded {
  118. t.Fatal(err)
  119. }
  120. if ctx.Err() != nil && ctx.Err() != context.DeadlineExceeded {
  121. t.Fatal(ctx.Err())
  122. }
  123. }
  124. func TestExecCancel(t *testing.T) {
  125. db, err := sql.Open("sqlite3", ":memory:")
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. defer db.Close()
  130. if _, err = db.Exec("create table foo (id integer primary key)"); err != nil {
  131. t.Fatal(err)
  132. }
  133. for n := 0; n < 100; n++ {
  134. ctx, cancel := context.WithCancel(context.Background())
  135. _, err = db.ExecContext(ctx, "insert into foo (id) values (?)", n)
  136. cancel()
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. }
  141. }