-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathgroup_flag_test.go
More file actions
141 lines (116 loc) · 4.18 KB
/
Copy pathgroup_flag_test.go
File metadata and controls
141 lines (116 loc) · 4.18 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 cmd
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
// createTestDatabase creates a valid WAL-mode test database with approximately the given size
func createTestDatabase(t *testing.T, sizeBytes int) string {
t.Helper()
if _, err := exec.LookPath("sqlite3"); err != nil {
t.Skip("sqlite3 not available, skipping test")
}
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")
// Create database with correct settings for Turso
cmd := exec.Command("sqlite3", "-list", dbPath,
"PRAGMA page_size=4096;",
"PRAGMA journal_mode=WAL;",
"CREATE TABLE data (id INTEGER PRIMARY KEY, blob BLOB);")
require.NoError(t, cmd.Run(), "failed to create test database")
// Fill with data to reach target size
if sizeBytes > 0 {
rowSize := 1000 // ~1KB per row
numRows := sizeBytes / rowSize
if numRows < 1 {
numRows = 1
}
for i := 0; i < numRows; i++ {
cmd = exec.Command("sqlite3", "-list", dbPath,
fmt.Sprintf("INSERT INTO data (blob) VALUES (randomblob(%d));", rowSize))
require.NoError(t, cmd.Run())
}
}
return dbPath
}
func TestRunQuickCheck(t *testing.T) {
if _, err := exec.LookPath("sqlite3"); err != nil {
t.Skip("sqlite3 not available, skipping test")
}
t.Run("valid database succeeds", func(t *testing.T) {
dbPath := createTestDatabase(t, 10*1024) // 10KB
err := runQuickCheck(dbPath)
require.NoError(t, err)
})
t.Run("corrupted database returns error", func(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "corrupt.db")
// Create a file with garbage data
err := os.WriteFile(dbPath, []byte("not a valid sqlite database content here"), 0644)
require.NoError(t, err)
err = runQuickCheck(dbPath)
require.Error(t, err)
require.Contains(t, err.Error(), "integrity check failed")
})
t.Run("nonexistent file returns error", func(t *testing.T) {
err := runQuickCheck("/nonexistent/path/db.sqlite")
require.Error(t, err)
})
}
func TestTursodbLogPath(t *testing.T) {
require.Equal(t, "data.db-log", tursodbLogPath("data.db"))
require.Equal(t, "data.db-log", tursodbLogPath("data.sqlite"))
require.Equal(t, "/some/dir/mydb.db-log", tursodbLogPath("/some/dir/mydb.db"))
require.Equal(t, "noext.db-log", tursodbLogPath("noext"))
}
func TestCheckSidecarEmpty(t *testing.T) {
dir := t.TempDir()
t.Run("missing sidecar is fine", func(t *testing.T) {
require.NoError(t, checkSidecarEmpty(filepath.Join(dir, "missing-wal"), "hint"))
})
t.Run("empty sidecar is fine", func(t *testing.T) {
path := filepath.Join(dir, "empty-wal")
require.NoError(t, os.WriteFile(path, nil, 0644))
require.NoError(t, checkSidecarEmpty(path, "hint"))
})
t.Run("non-empty sidecar errors with hint", func(t *testing.T) {
path := filepath.Join(dir, "full-wal")
require.NoError(t, os.WriteFile(path, []byte("frames"), 0644))
err := checkSidecarEmpty(path, "close all connections")
require.Error(t, err)
require.Contains(t, err.Error(), "importing would lose the data it holds")
require.Contains(t, err.Error(), "close all connections")
})
}
func TestCheckpointWALBeforeUpload(t *testing.T) {
if _, err := exec.LookPath("sqlite3"); err != nil {
t.Skip("sqlite3 not available, skipping test")
}
dbPath := createTestDatabase(t, 10*1024)
require.NoError(t, checkpointWALBeforeUpload(dbPath))
// after the checkpoint no data-bearing sidecars may remain
for _, suffix := range []string{"-wal", "-journal"} {
if info, err := os.Stat(dbPath + suffix); err == nil {
require.Zero(t, info.Size(), "%s must be empty after checkpoint", dbPath+suffix)
}
}
}
func TestPrepareTursoDBFile(t *testing.T) {
if _, err := exec.LookPath("tursodb"); err != nil {
t.Skip("tursodb not available, skipping test")
}
dbPath := createTestDatabase(t, 10*1024)
require.NoError(t, checkpointWALBeforeUpload(dbPath))
require.NoError(t, prepareTursoDBFile(dbPath))
format, err := sniffSQLiteFileFormat(dbPath)
require.NoError(t, err)
require.Equal(t, fileFormatMVCC, format)
for _, sidecar := range []string{dbPath + "-wal", dbPath + "-journal", tursodbLogPath(dbPath)} {
if info, err := os.Stat(sidecar); err == nil {
require.Zero(t, info.Size(), "%s must be empty after checkpoint", sidecar)
}
}
}