-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathgroup_flag_test.go
More file actions
76 lines (62 loc) · 1.95 KB
/
group_flag_test.go
File metadata and controls
76 lines (62 loc) · 1.95 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
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)
})
}