101 lines
2.8 KiB
Go
101 lines
2.8 KiB
Go
package workers
|
|
|
|
import (
|
|
"context"
|
|
"image/color"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/disintegration/imaging"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func TestThumbnailJob_Execute(t *testing.T) {
|
|
logger := zap.NewNop()
|
|
ctx := context.Background()
|
|
|
|
// Créer un répertoire temporaire pour les tests
|
|
tmpDir := t.TempDir()
|
|
|
|
// Créer une image de test simple (1x1 pixel PNG)
|
|
testImagePath := filepath.Join(tmpDir, "test.png")
|
|
testThumbnailPath := filepath.Join(tmpDir, "test_thumb.jpg")
|
|
|
|
// Créer une image de test avec imaging (image rouge 100x100)
|
|
img := imaging.New(100, 100, color.NRGBA{255, 0, 0, 255})
|
|
if err := imaging.Save(img, testImagePath); err != nil {
|
|
t.Fatalf("Failed to create test image: %v", err)
|
|
}
|
|
|
|
// Test 1: Génération de thumbnail normale
|
|
t.Run("Generate thumbnail successfully", func(t *testing.T) {
|
|
job := NewThumbnailJob(testImagePath, testThumbnailPath, 50, 50)
|
|
|
|
err := job.Execute(ctx, logger)
|
|
if err != nil {
|
|
t.Fatalf("Expected no error, got: %v", err)
|
|
}
|
|
|
|
// Vérifier que le thumbnail existe
|
|
if _, err := os.Stat(testThumbnailPath); os.IsNotExist(err) {
|
|
t.Fatal("Thumbnail file was not created")
|
|
}
|
|
})
|
|
|
|
// Test 2: Fichier source inexistant
|
|
t.Run("Fail when input file does not exist", func(t *testing.T) {
|
|
job := NewThumbnailJob("/nonexistent/image.png", testThumbnailPath, 50, 50)
|
|
|
|
err := job.Execute(ctx, logger)
|
|
if err == nil {
|
|
t.Fatal("Expected error for nonexistent file, got nil")
|
|
}
|
|
})
|
|
|
|
// Test 3: Valeurs par défaut
|
|
t.Run("Use default dimensions when not specified", func(t *testing.T) {
|
|
thumbPath2 := filepath.Join(tmpDir, "test_thumb2.jpg")
|
|
job := NewThumbnailJob(testImagePath, thumbPath2, 0, 0)
|
|
|
|
// Vérifier que les valeurs par défaut sont appliquées
|
|
if job.Width != 300 || job.Height != 300 {
|
|
t.Errorf("Expected default dimensions 300x300, got %dx%d", job.Width, job.Height)
|
|
}
|
|
|
|
err := job.Execute(ctx, logger)
|
|
if err != nil {
|
|
t.Fatalf("Expected no error, got: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestNewThumbnailJob(t *testing.T) {
|
|
t.Run("Create job with specified dimensions", func(t *testing.T) {
|
|
job := NewThumbnailJob("input.jpg", "output.jpg", 200, 150)
|
|
|
|
if job.InputPath != "input.jpg" {
|
|
t.Errorf("Expected InputPath 'input.jpg', got '%s'", job.InputPath)
|
|
}
|
|
if job.OutputPath != "output.jpg" {
|
|
t.Errorf("Expected OutputPath 'output.jpg', got '%s'", job.OutputPath)
|
|
}
|
|
if job.Width != 200 {
|
|
t.Errorf("Expected Width 200, got %d", job.Width)
|
|
}
|
|
if job.Height != 150 {
|
|
t.Errorf("Expected Height 150, got %d", job.Height)
|
|
}
|
|
})
|
|
|
|
t.Run("Apply default dimensions when zero", func(t *testing.T) {
|
|
job := NewThumbnailJob("input.jpg", "output.jpg", 0, 0)
|
|
|
|
if job.Width != 300 {
|
|
t.Errorf("Expected default Width 300, got %d", job.Width)
|
|
}
|
|
if job.Height != 300 {
|
|
t.Errorf("Expected default Height 300, got %d", job.Height)
|
|
}
|
|
})
|
|
}
|