- Add pure Go SQLite driver (modernc.org/sqlite) to avoid CGO dependency - Implement database connection management with WAL mode - Add write mutex for serializing concurrent writes - Create schema for all tables matching DESIGN.md specifications - Implement repository pattern for all database entities: - Files, FileChunks, Chunks, Blobs, BlobChunks, ChunkFiles, Snapshots - Add transaction support with proper rollback handling - Add fatal error handling for database integrity issues - Add snapshot fields for tracking file sizes and compression ratios - Make index path configurable via VAULTIK_INDEX_PATH environment variable - Add comprehensive test coverage for all repositories - Add format check to Makefile to ensure code formatting
97 lines
2.1 KiB
Go
97 lines
2.1 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestDatabase(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := filepath.Join(t.TempDir(), "test.db")
|
|
|
|
db, err := New(ctx, dbPath)
|
|
if err != nil {
|
|
t.Fatalf("failed to create database: %v", err)
|
|
}
|
|
defer func() {
|
|
if err := db.Close(); err != nil {
|
|
t.Errorf("failed to close database: %v", err)
|
|
}
|
|
}()
|
|
|
|
// Test connection
|
|
if db.Conn() == nil {
|
|
t.Fatal("database connection is nil")
|
|
}
|
|
|
|
// Test schema creation (already done in New)
|
|
// Verify tables exist
|
|
tables := []string{
|
|
"files", "file_chunks", "chunks", "blobs",
|
|
"blob_chunks", "chunk_files", "snapshots",
|
|
}
|
|
|
|
for _, table := range tables {
|
|
var name string
|
|
err := db.conn.QueryRow("SELECT name FROM sqlite_master WHERE type='table' AND name=?", table).Scan(&name)
|
|
if err != nil {
|
|
t.Errorf("table %s does not exist: %v", table, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDatabaseInvalidPath(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
// Test with invalid path
|
|
_, err := New(ctx, "/invalid/path/that/does/not/exist/test.db")
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid path")
|
|
}
|
|
}
|
|
|
|
func TestDatabaseConcurrentAccess(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := filepath.Join(t.TempDir(), "test.db")
|
|
|
|
db, err := New(ctx, dbPath)
|
|
if err != nil {
|
|
t.Fatalf("failed to create database: %v", err)
|
|
}
|
|
defer func() {
|
|
if err := db.Close(); err != nil {
|
|
t.Errorf("failed to close database: %v", err)
|
|
}
|
|
}()
|
|
|
|
// Test concurrent writes
|
|
done := make(chan bool, 10)
|
|
for i := 0; i < 10; i++ {
|
|
go func(i int) {
|
|
_, err := db.ExecWithLock(ctx, "INSERT INTO chunks (chunk_hash, sha256, size) VALUES (?, ?, ?)",
|
|
fmt.Sprintf("hash%d", i), fmt.Sprintf("sha%d", i), i*1024)
|
|
if err != nil {
|
|
t.Errorf("concurrent insert failed: %v", err)
|
|
}
|
|
done <- true
|
|
}(i)
|
|
}
|
|
|
|
// Wait for all goroutines
|
|
for i := 0; i < 10; i++ {
|
|
<-done
|
|
}
|
|
|
|
// Verify all inserts succeeded
|
|
var count int
|
|
err = db.conn.QueryRowContext(ctx, "SELECT COUNT(*) FROM chunks").Scan(&count)
|
|
if err != nil {
|
|
t.Fatalf("failed to count chunks: %v", err)
|
|
}
|
|
if count != 10 {
|
|
t.Errorf("expected 10 chunks, got %d", count)
|
|
}
|
|
}
|