|
1 | 1 | package tests
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "os" |
5 | 4 | "path/filepath"
|
6 | 5 | "testing"
|
7 | 6 |
|
8 | 7 | "github.com/ncruces/go-sqlite3"
|
| 8 | + "github.com/ncruces/go-sqlite3/driver" |
9 | 9 | _ "github.com/ncruces/go-sqlite3/embed"
|
10 | 10 | _ "github.com/ncruces/go-sqlite3/internal/testcfg"
|
11 | 11 | "github.com/ncruces/go-sqlite3/vfs"
|
@@ -52,26 +52,55 @@ func TestWAL_readonly(t *testing.T) {
|
52 | 52 | }
|
53 | 53 | t.Parallel()
|
54 | 54 |
|
55 |
| - tmp := filepath.Join(t.TempDir(), "test.db") |
56 |
| - err := os.WriteFile(tmp, walDB, 0666) |
| 55 | + tmp := filepath.ToSlash(filepath.Join(t.TempDir(), "test.db")) |
| 56 | + |
| 57 | + db1, err := driver.Open("file:"+tmp+"?_pragma=journal_mode(wal)&_txlock=immediate", nil) |
57 | 58 | if err != nil {
|
58 | 59 | t.Fatal(err)
|
59 | 60 | }
|
| 61 | + defer db1.Close() |
60 | 62 |
|
61 |
| - db, err := sqlite3.OpenFlags(tmp, sqlite3.OPEN_READONLY) |
| 63 | + db2, err := driver.Open("file:"+tmp+"?_pragma=journal_mode(wal)&mode=ro", nil) |
62 | 64 | if err != nil {
|
63 | 65 | t.Fatal(err)
|
64 | 66 | }
|
65 |
| - defer db.Close() |
| 67 | + defer db2.Close() |
| 68 | + |
| 69 | + // Create the table using the first (writable) connection. |
| 70 | + _, err = db1.Exec(` |
| 71 | + CREATE TABLE t(id INTEGER PRIMARY KEY, name TEXT); |
| 72 | + INSERT INTO t(name) VALUES('alice'); |
| 73 | + `) |
| 74 | + if err != nil { |
| 75 | + t.Fatal(err) |
| 76 | + } |
| 77 | + |
| 78 | + // Select the data using the second (readonly) connection. |
| 79 | + var name string |
| 80 | + err = db2.QueryRow("SELECT name FROM t").Scan(&name) |
| 81 | + if err != nil { |
| 82 | + t.Fatal(err) |
| 83 | + } |
| 84 | + if name != "alice" { |
| 85 | + t.Errorf("got %q want alice", name) |
| 86 | + } |
66 | 87 |
|
67 |
| - stmt, _, err := db.Prepare(`SELECT * FROM sqlite_master`) |
| 88 | + // Update table. |
| 89 | + _, err = db1.Exec(` |
| 90 | + DELETE FROM t; |
| 91 | + INSERT INTO t(name) VALUES('bob'); |
| 92 | + `) |
68 | 93 | if err != nil {
|
69 | 94 | t.Fatal(err)
|
70 | 95 | }
|
71 |
| - defer stmt.Close() |
72 | 96 |
|
73 |
| - if stmt.Step() { |
74 |
| - t.Error("want no rows") |
| 97 | + // Select the data using the second (readonly) connection. |
| 98 | + err = db2.QueryRow("SELECT name FROM t").Scan(&name) |
| 99 | + if err != nil { |
| 100 | + t.Fatal(err) |
| 101 | + } |
| 102 | + if name != "bob" { |
| 103 | + t.Errorf("got %q want bob", name) |
75 | 104 | }
|
76 | 105 | }
|
77 | 106 |
|
|
0 commit comments