Error messages, test contexts.

This commit is contained in:
Nuno Cruces
2025-09-23 12:51:36 +01:00
parent 4c24bd0cb6
commit b588d5f991
10 changed files with 41 additions and 64 deletions

View File

@@ -163,7 +163,7 @@ func TestConn_SetInterrupt(t *testing.T) {
}
defer db.Close()
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
db.SetInterrupt(ctx)
// Interrupt doesn't interrupt this.
@@ -202,9 +202,7 @@ func TestConn_SetInterrupt(t *testing.T) {
t.Errorf("got %v, want sqlite3.INTERRUPT", err)
}
ctx, cancel = context.WithCancel(context.Background())
defer cancel()
db.SetInterrupt(ctx)
db.SetInterrupt(t.Context())
// Interrupting can be cleared.
err = db.Exec(`SELECT 1`)
@@ -212,9 +210,8 @@ func TestConn_SetInterrupt(t *testing.T) {
t.Fatal(err)
}
db.SetInterrupt(ctx)
if got := db.GetInterrupt(); got != ctx {
t.Errorf("got %v, want %v", got, ctx)
if got := db.GetInterrupt(); got != t.Context() {
t.Errorf("got %v, want %v", got, t.Context())
}
}

View File

@@ -1,7 +1,6 @@
package tests
import (
"context"
"testing"
"github.com/ncruces/go-sqlite3"
@@ -15,9 +14,6 @@ func TestDriver(t *testing.T) {
t.Parallel()
tmp := memdb.TestDB(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, err := driver.Open(tmp, nil, func(c *sqlite3.Conn) error {
return c.Exec(`PRAGMA optimize`)
})
@@ -26,13 +22,13 @@ func TestDriver(t *testing.T) {
}
defer db.Close()
conn, err := db.Conn(ctx)
conn, err := db.Conn(t.Context())
if err != nil {
t.Fatal(err)
}
defer conn.Close()
res, err := conn.ExecContext(ctx,
res, err := conn.ExecContext(t.Context(),
`CREATE TABLE users (id INTEGER PRIMARY KEY NOT NULL, name VARCHAR(10))`)
if err != nil {
t.Fatal(err)
@@ -52,7 +48,7 @@ func TestDriver(t *testing.T) {
t.Errorf("got %d want 0", changes)
}
res, err = conn.ExecContext(ctx,
res, err = conn.ExecContext(t.Context(),
`INSERT INTO users (id, name) VALUES (0, 'go'), (1, 'zig'), (2, 'whatever')`)
if err != nil {
t.Fatal(err)
@@ -65,7 +61,7 @@ func TestDriver(t *testing.T) {
t.Errorf("got %d want 3", changes)
}
stmt, err := conn.PrepareContext(context.Background(),
stmt, err := conn.PrepareContext(t.Context(),
`SELECT id, name FROM users`)
if err != nil {
t.Fatal(err)

View File

@@ -1,7 +1,6 @@
package tests
import (
"context"
"encoding/json"
"math"
"testing"
@@ -19,29 +18,26 @@ func TestJSON(t *testing.T) {
t.Parallel()
tmp := memdb.TestDB(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, err := driver.Open(tmp)
if err != nil {
t.Fatal(err)
}
defer db.Close()
conn, err := db.Conn(ctx)
conn, err := db.Conn(t.Context())
if err != nil {
t.Fatal(err)
}
defer conn.Close()
_, err = conn.ExecContext(ctx, `CREATE TABLE test (col)`)
_, err = conn.ExecContext(t.Context(), `CREATE TABLE test (col)`)
if err != nil {
t.Fatal(err)
}
reference := time.Date(2013, 10, 7, 4, 23, 19, 120_000_000, time.FixedZone("", -4*3600))
_, err = conn.ExecContext(ctx,
_, err = conn.ExecContext(t.Context(),
`INSERT INTO test (col) VALUES (?), (?), (?), (?)`,
nil, 1, math.Pi, reference,
)
@@ -49,7 +45,7 @@ func TestJSON(t *testing.T) {
t.Fatal(err)
}
_, err = conn.ExecContext(ctx,
_, err = conn.ExecContext(t.Context(),
`INSERT INTO test (col) VALUES (?), (?), (?), (?)`,
sqlite3.JSON(math.Pi), sqlite3.JSON(false),
julianday.Format(reference), sqlite3.JSON([]string{}))
@@ -57,7 +53,7 @@ func TestJSON(t *testing.T) {
t.Fatal(err)
}
rows, err := conn.QueryContext(ctx, "SELECT * FROM test")
rows, err := conn.QueryContext(t.Context(), "SELECT * FROM test")
if err != nil {
t.Fatal(err)
}

View File

@@ -1,7 +1,6 @@
package tests
import (
"context"
"fmt"
"reflect"
"testing"
@@ -136,35 +135,34 @@ func TestTimeFormat_Scanner(t *testing.T) {
t.Parallel()
tmp := memdb.TestDB(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, err := driver.Open(tmp)
if err != nil {
t.Fatal(err)
}
defer db.Close()
conn, err := db.Conn(ctx)
conn, err := db.Conn(t.Context())
if err != nil {
t.Fatal(err)
}
defer conn.Close()
_, err = conn.ExecContext(ctx, `CREATE TABLE test (col)`)
_, err = conn.ExecContext(t.Context(), `CREATE TABLE test (col)`)
if err != nil {
t.Fatal(err)
}
reference := time.Date(2013, 10, 7, 4, 23, 19, 120_000_000, time.FixedZone("", -4*3600))
_, err = conn.ExecContext(ctx, `INSERT INTO test VALUES (?)`, sqlite3.TimeFormat7TZ.Encode(reference))
_, err = conn.ExecContext(t.Context(), `INSERT INTO test VALUES (?)`,
sqlite3.TimeFormat7TZ.Encode(reference))
if err != nil {
t.Fatal(err)
}
var got time.Time
err = conn.QueryRowContext(ctx, "SELECT * FROM test").Scan(sqlite3.TimeFormatAuto.Scanner(&got))
err = conn.QueryRowContext(t.Context(), "SELECT * FROM test").
Scan(sqlite3.TimeFormatAuto.Scanner(&got))
if err != nil {
t.Fatal(err)
}

View File

@@ -170,7 +170,7 @@ func TestConn_Transaction_interrupt(t *testing.T) {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
db.SetInterrupt(ctx)
tx, err = db.BeginExclusive()
@@ -203,7 +203,7 @@ func TestConn_Transaction_interrupt(t *testing.T) {
t.Errorf("got %v, want sqlite3.INTERRUPT", err)
}
db.SetInterrupt(context.Background())
db.SetInterrupt(t.Context())
stmt, _, err := db.Prepare(`SELECT count(*) FROM test`)
if err != nil {
t.Fatal(err)
@@ -226,7 +226,7 @@ func TestConn_Transaction_interrupted(t *testing.T) {
}
defer db.Close()
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
db.SetInterrupt(ctx)
cancel()
@@ -274,7 +274,7 @@ func TestConn_Transaction_busy(t *testing.T) {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
db2.SetInterrupt(ctx)
go cancel()
@@ -493,7 +493,7 @@ func TestConn_Savepoint_interrupt(t *testing.T) {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
db.SetInterrupt(ctx)
savept1 := db.Savepoint()
@@ -530,7 +530,7 @@ func TestConn_Savepoint_interrupt(t *testing.T) {
t.Errorf("got %v, want sqlite3.INTERRUPT", err)
}
db.SetInterrupt(context.Background())
db.SetInterrupt(t.Context())
stmt, _, err := db.Prepare(`SELECT count(*) FROM test`)
if err != nil {
t.Fatal(err)