- 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
120 lines
2.6 KiB
Go
120 lines
2.6 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestFileChunkRepository(t *testing.T) {
|
|
db, cleanup := setupTestDB(t)
|
|
defer cleanup()
|
|
|
|
ctx := context.Background()
|
|
repo := NewFileChunkRepository(db)
|
|
|
|
// Test Create
|
|
fc1 := &FileChunk{
|
|
Path: "/test/file.txt",
|
|
Idx: 0,
|
|
ChunkHash: "chunk1",
|
|
}
|
|
|
|
err := repo.Create(ctx, nil, fc1)
|
|
if err != nil {
|
|
t.Fatalf("failed to create file chunk: %v", err)
|
|
}
|
|
|
|
// Add more chunks for the same file
|
|
fc2 := &FileChunk{
|
|
Path: "/test/file.txt",
|
|
Idx: 1,
|
|
ChunkHash: "chunk2",
|
|
}
|
|
err = repo.Create(ctx, nil, fc2)
|
|
if err != nil {
|
|
t.Fatalf("failed to create second file chunk: %v", err)
|
|
}
|
|
|
|
fc3 := &FileChunk{
|
|
Path: "/test/file.txt",
|
|
Idx: 2,
|
|
ChunkHash: "chunk3",
|
|
}
|
|
err = repo.Create(ctx, nil, fc3)
|
|
if err != nil {
|
|
t.Fatalf("failed to create third file chunk: %v", err)
|
|
}
|
|
|
|
// Test GetByPath
|
|
chunks, err := repo.GetByPath(ctx, "/test/file.txt")
|
|
if err != nil {
|
|
t.Fatalf("failed to get file chunks: %v", err)
|
|
}
|
|
if len(chunks) != 3 {
|
|
t.Errorf("expected 3 chunks, got %d", len(chunks))
|
|
}
|
|
|
|
// Verify order
|
|
for i, chunk := range chunks {
|
|
if chunk.Idx != i {
|
|
t.Errorf("wrong chunk order: expected idx %d, got %d", i, chunk.Idx)
|
|
}
|
|
}
|
|
|
|
// Test duplicate insert (should be idempotent)
|
|
err = repo.Create(ctx, nil, fc1)
|
|
if err != nil {
|
|
t.Fatalf("failed to create duplicate file chunk: %v", err)
|
|
}
|
|
|
|
// Test DeleteByPath
|
|
err = repo.DeleteByPath(ctx, nil, "/test/file.txt")
|
|
if err != nil {
|
|
t.Fatalf("failed to delete file chunks: %v", err)
|
|
}
|
|
|
|
chunks, err = repo.GetByPath(ctx, "/test/file.txt")
|
|
if err != nil {
|
|
t.Fatalf("failed to get deleted file chunks: %v", err)
|
|
}
|
|
if len(chunks) != 0 {
|
|
t.Errorf("expected 0 chunks after delete, got %d", len(chunks))
|
|
}
|
|
}
|
|
|
|
func TestFileChunkRepositoryMultipleFiles(t *testing.T) {
|
|
db, cleanup := setupTestDB(t)
|
|
defer cleanup()
|
|
|
|
ctx := context.Background()
|
|
repo := NewFileChunkRepository(db)
|
|
|
|
// Create chunks for multiple files
|
|
files := []string{"/file1.txt", "/file2.txt", "/file3.txt"}
|
|
for _, path := range files {
|
|
for i := 0; i < 2; i++ {
|
|
fc := &FileChunk{
|
|
Path: path,
|
|
Idx: i,
|
|
ChunkHash: fmt.Sprintf("%s_chunk%d", path, i),
|
|
}
|
|
err := repo.Create(ctx, nil, fc)
|
|
if err != nil {
|
|
t.Fatalf("failed to create file chunk: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Verify each file has correct chunks
|
|
for _, path := range files {
|
|
chunks, err := repo.GetByPath(ctx, path)
|
|
if err != nil {
|
|
t.Fatalf("failed to get chunks for %s: %v", path, err)
|
|
}
|
|
if len(chunks) != 2 {
|
|
t.Errorf("expected 2 chunks for %s, got %d", path, len(chunks))
|
|
}
|
|
}
|
|
}
|