- Add unified compression/encryption package in internal/blobgen - Update DATAMODEL.md to reflect current schema implementation - Refactor snapshot cleanup into well-named methods for clarity - Add snapshot_id to uploads table to track new blobs per snapshot - Fix blob count reporting for incremental backups - Add DeleteOrphaned method to BlobChunkRepository - Fix cleanup order to respect foreign key constraints - Update tests to reflect schema changes
100 lines
2.2 KiB
Go
100 lines
2.2 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",
|
|
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.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",
|
|
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")
|
|
}
|
|
}
|