- Add internal/types package with type-safe wrappers for IDs, hashes, paths, and credentials (FileID, BlobID, ChunkHash, etc.) - Implement driver.Valuer and sql.Scanner for UUID-based types - Add `vaultik version` command showing version, commit, go version - Add `--verify` flag to restore command that checksums all restored files against expected chunk hashes with progress bar - Remove fetch.go (dead code, functionality in restore) - Clean up TODO.md, remove completed items - Update all database and snapshot code to use new custom types
102 lines
2.3 KiB
Go
102 lines
2.3 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"git.eeqj.de/sneak/vaultik/internal/types"
|
|
)
|
|
|
|
func TestChunkRepository(t *testing.T) {
|
|
db, cleanup := setupTestDB(t)
|
|
defer cleanup()
|
|
|
|
ctx := context.Background()
|
|
repo := NewChunkRepository(db)
|
|
|
|
// Test Create
|
|
chunk := &Chunk{
|
|
ChunkHash: types.ChunkHash("chunkhash123"),
|
|
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.String())
|
|
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.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: types.ChunkHash("chunkhash456"),
|
|
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.String(), chunk2.ChunkHash.String()})
|
|
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")
|
|
}
|
|
}
|