- 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
105 lines
2.4 KiB
Go
105 lines
2.4 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
func TestChunkRepository(t *testing.T) {
|
|
db, cleanup := setupTestDB(t)
|
|
defer cleanup()
|
|
|
|
ctx := context.Background()
|
|
repo := NewChunkRepository(db)
|
|
|
|
// Test Create
|
|
chunk := &Chunk{
|
|
ChunkHash: "chunkhash123",
|
|
SHA256: "sha256hash123",
|
|
Size: 4096,
|
|
}
|
|
|
|
err := repo.Create(ctx, nil, chunk)
|
|
if err != nil {
|
|
t.Fatalf("failed to create chunk: %v", err)
|
|
}
|
|
|
|
// Test GetByHash
|
|
retrieved, err := repo.GetByHash(ctx, chunk.ChunkHash)
|
|
if err != nil {
|
|
t.Fatalf("failed to get chunk: %v", err)
|
|
}
|
|
if retrieved == nil {
|
|
t.Fatal("expected chunk, got nil")
|
|
}
|
|
if retrieved.ChunkHash != chunk.ChunkHash {
|
|
t.Errorf("chunk hash mismatch: got %s, want %s", retrieved.ChunkHash, chunk.ChunkHash)
|
|
}
|
|
if retrieved.SHA256 != chunk.SHA256 {
|
|
t.Errorf("sha256 mismatch: got %s, want %s", retrieved.SHA256, chunk.SHA256)
|
|
}
|
|
if retrieved.Size != chunk.Size {
|
|
t.Errorf("size mismatch: got %d, want %d", retrieved.Size, chunk.Size)
|
|
}
|
|
|
|
// Test duplicate insert (should be idempotent)
|
|
err = repo.Create(ctx, nil, chunk)
|
|
if err != nil {
|
|
t.Fatalf("failed to create duplicate chunk: %v", err)
|
|
}
|
|
|
|
// Test GetByHashes
|
|
chunk2 := &Chunk{
|
|
ChunkHash: "chunkhash456",
|
|
SHA256: "sha256hash456",
|
|
Size: 8192,
|
|
}
|
|
err = repo.Create(ctx, nil, chunk2)
|
|
if err != nil {
|
|
t.Fatalf("failed to create second chunk: %v", err)
|
|
}
|
|
|
|
chunks, err := repo.GetByHashes(ctx, []string{chunk.ChunkHash, chunk2.ChunkHash})
|
|
if err != nil {
|
|
t.Fatalf("failed to get chunks by hashes: %v", err)
|
|
}
|
|
if len(chunks) != 2 {
|
|
t.Errorf("expected 2 chunks, got %d", len(chunks))
|
|
}
|
|
|
|
// Test ListUnpacked
|
|
unpacked, err := repo.ListUnpacked(ctx, 10)
|
|
if err != nil {
|
|
t.Fatalf("failed to list unpacked chunks: %v", err)
|
|
}
|
|
if len(unpacked) != 2 {
|
|
t.Errorf("expected 2 unpacked chunks, got %d", len(unpacked))
|
|
}
|
|
}
|
|
|
|
func TestChunkRepositoryNotFound(t *testing.T) {
|
|
db, cleanup := setupTestDB(t)
|
|
defer cleanup()
|
|
|
|
ctx := context.Background()
|
|
repo := NewChunkRepository(db)
|
|
|
|
// Test GetByHash with non-existent hash
|
|
chunk, err := repo.GetByHash(ctx, "nonexistent")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if chunk != nil {
|
|
t.Error("expected nil for non-existent chunk")
|
|
}
|
|
|
|
// Test GetByHashes with empty list
|
|
chunks, err := repo.GetByHashes(ctx, []string{})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if chunks != nil {
|
|
t.Error("expected nil for empty hash list")
|
|
}
|
|
}
|