a love letter to tangled (android, iOS, and a search API)
1package store
2
3import (
4 "database/sql"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 _ "modernc.org/sqlite"
10)
11
12func TestExecMigrationRunsLegacySQLiteFTSForLocalFallback(t *testing.T) {
13 db, err := sql.Open("sqlite", ":memory:")
14 if err != nil {
15 t.Fatalf("open sqlite: %v", err)
16 }
17 t.Cleanup(func() { _ = db.Close() })
18
19 err = execMigration(db, "003_documents_fts5.sql", "CREATE VIRTUAL TABLE documents_fts USING fts5(title);", migrationMode{
20 backend: BackendSQLite,
21 targetDescription: "local SQLite",
22 })
23 if err != nil {
24 t.Fatalf("expected local SQLite migration to create FTS5 table: %v", err)
25 }
26}
27
28func TestExecMigrationFailsForRemoteWhenNativeFTSUnavailable(t *testing.T) {
29 db, err := sql.Open("sqlite", ":memory:")
30 if err != nil {
31 t.Fatalf("open sqlite: %v", err)
32 }
33 t.Cleanup(func() { _ = db.Close() })
34
35 err = execMigration(db, "003_documents_fts5.sql", "CREATE VIRTUAL TABLE documents_fts USING fts5(", migrationMode{
36 backend: BackendSQLite,
37 targetDescription: "local SQLite",
38 })
39 if err == nil {
40 t.Fatal("expected SQLite migration to fail when FTS5 is unavailable")
41 }
42 if !strings.Contains(err.Error(), "SQLite FTS5 statement failed") {
43 t.Fatalf("unexpected error: %v", err)
44 }
45}
46
47func TestOpenLocalSQLiteAppliesPragmasAndPoolLimits(t *testing.T) {
48 path := filepath.Join(t.TempDir(), "local.db")
49 db, err := Open("file:" + path)
50 if err != nil {
51 t.Fatalf("open local sqlite: %v", err)
52 }
53 t.Cleanup(func() { _ = db.Close() })
54
55 if got := db.Stats().MaxOpenConnections; got != 1 {
56 t.Fatalf("max open conns: got %d, want 1", got)
57 }
58
59 var mode string
60 if err := db.QueryRow(`PRAGMA journal_mode`).Scan(&mode); err != nil {
61 t.Fatalf("pragma journal_mode: %v", err)
62 }
63 if !strings.EqualFold(mode, "wal") {
64 t.Fatalf("journal_mode: got %q, want wal", mode)
65 }
66
67 var timeout int
68 if err := db.QueryRow(`PRAGMA busy_timeout`).Scan(&timeout); err != nil {
69 t.Fatalf("pragma busy_timeout: %v", err)
70 }
71 if timeout != 5000 {
72 t.Fatalf("busy_timeout: got %d, want 5000", timeout)
73 }
74}