-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinmemory_test.go
More file actions
141 lines (129 loc) · 3.6 KB
/
Copy pathinmemory_test.go
File metadata and controls
141 lines (129 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package sqlite_test
import (
"database/sql"
"path/filepath"
"testing"
sqlite "gosqlite.org"
)
// TestInMemoryConstantIsValidDSN: the bare constant must work with
// every entry point — database/sql, the typed sqlite.Open, and the
// helper sqlite.OpenInMemory().
func TestInMemoryConstantIsValidDSN(t *testing.T) {
if sqlite.InMemory != ":memory:" {
t.Errorf("sqlite.InMemory=%q, want \":memory:\"", sqlite.InMemory)
}
db, err := sql.Open(sqlite.DriverName, sqlite.InMemory)
if err != nil {
t.Fatalf("sql.Open(sqlite.InMemory): %v", err)
}
defer db.Close()
var n int
if err := db.QueryRow(`SELECT 1`).Scan(&n); err != nil {
t.Errorf("ping via sql.Open: %v", err)
}
}
func TestOpenInMemory(t *testing.T) {
db, err := sqlite.OpenInMemory()
if err != nil {
t.Fatalf("sqlite.OpenInMemory: %v", err)
}
defer db.Close()
if _, err := db.Exec(`CREATE TABLE t(v INT)`); err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`INSERT INTO t VALUES (1), (2), (3)`); err != nil {
t.Fatal(err)
}
var n int
if err := db.QueryRow(`SELECT COUNT(*) FROM t`).Scan(&n); err != nil {
t.Fatal(err)
}
if n != 3 {
t.Errorf("count=%d, want 3", n)
}
}
// TestOpenWAL: production preset applies. WAL mode propagates as a
// DB-file attribute so we can verify it via PRAGMA on any conn.
func TestOpenWAL(t *testing.T) {
dir := t.TempDir()
db, err := sqlite.OpenWAL(filepath.Join(dir, "wal.db"))
if err != nil {
t.Fatalf("OpenWAL: %v", err)
}
defer db.Close()
var mode string
if err := db.QueryRow(`PRAGMA journal_mode`).Scan(&mode); err != nil {
t.Fatal(err)
}
if mode != "wal" {
t.Errorf("journal_mode=%q, want \"wal\"", mode)
}
var fk int
if err := db.QueryRow(`PRAGMA foreign_keys`).Scan(&fk); err != nil {
t.Fatal(err)
}
if fk != 1 {
t.Errorf("foreign_keys=%d, want 1", fk)
}
}
// TestOpenReadOnly: refuses to create a missing file (matches
// mode=ro semantics) and refuses writes once attached to an
// existing file.
func TestOpenReadOnly(t *testing.T) {
dir := t.TempDir()
missing := filepath.Join(dir, "nope.db")
if _, err := sqlite.OpenReadOnly(missing); err == nil {
t.Error("OpenReadOnly on missing file: want error, got nil")
}
// Seed an existing file via OpenWAL, then reopen RO.
existing := filepath.Join(dir, "seed.db")
seed, err := sqlite.OpenWAL(existing)
if err != nil {
t.Fatal(err)
}
if _, err := seed.Exec(`CREATE TABLE t(v INT); INSERT INTO t VALUES (42)`); err != nil {
t.Fatal(err)
}
seed.Close()
ro, err := sqlite.OpenReadOnly(existing)
if err != nil {
t.Fatalf("OpenReadOnly on existing file: %v", err)
}
defer ro.Close()
var v int
if err := ro.QueryRow(`SELECT v FROM t`).Scan(&v); err != nil {
t.Fatal(err)
}
if v != 42 {
t.Errorf("v=%d, want 42", v)
}
if _, err := ro.Exec(`INSERT INTO t VALUES (99)`); err == nil {
t.Error("write to read-only DB: want error, got nil")
}
}
// TestOpenShared: two distinct *sql.DB handles opened against the
// same name see the same rows — the test that pins the
// shared-cache contract OpenShared exists for.
func TestOpenShared(t *testing.T) {
const name = "shortcuts-shared-test"
a, err := sqlite.OpenShared(name)
if err != nil {
t.Fatalf("OpenShared (a): %v", err)
}
defer a.Close()
if _, err := a.Exec(`CREATE TABLE t(v INT); INSERT INTO t VALUES (1), (2)`); err != nil {
t.Fatal(err)
}
b, err := sqlite.OpenShared(name)
if err != nil {
t.Fatalf("OpenShared (b): %v", err)
}
defer b.Close()
var n int
if err := b.QueryRow(`SELECT COUNT(*) FROM t`).Scan(&n); err != nil {
t.Fatalf("b sees no rows: %v", err)
}
if n != 2 {
t.Errorf("b count=%d, want 2 (shared cache should expose a's writes)", n)
}
}