feat: add image optimization task model
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"cmbone/internal/models"
|
||||
)
|
||||
|
||||
func TestMigrateCreatesImageTaskTables(t *testing.T) {
|
||||
store := newTestStore(t)
|
||||
|
||||
for _, table := range []string{"image_tasks", "image_task_results"} {
|
||||
var name string
|
||||
if err := store.DB.QueryRow("SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?", table).Scan(&name); err != nil {
|
||||
t.Fatalf("query table %s: %v", table, err)
|
||||
}
|
||||
if name != table {
|
||||
t.Fatalf("table = %q, want %q", name, table)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageOptimizationServiceCreateTask(t *testing.T) {
|
||||
store := newTestStore(t)
|
||||
service := NewImageOptimizationService(store)
|
||||
sourcePath := filepath.Join(t.TempDir(), "sku-main.jpg")
|
||||
|
||||
detail, err := service.CreateTask(models.ImageTaskInput{
|
||||
SourcePath: sourcePath,
|
||||
Operation: "enhance",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create task: %v", err)
|
||||
}
|
||||
|
||||
if detail.Task.ID == 0 {
|
||||
t.Fatal("task id is empty")
|
||||
}
|
||||
if detail.Task.Status != imageTaskStatusSucceeded {
|
||||
t.Fatalf("status = %q, want %q", detail.Task.Status, imageTaskStatusSucceeded)
|
||||
}
|
||||
if detail.Task.Operation != "enhance" {
|
||||
t.Fatalf("operation = %q, want enhance", detail.Task.Operation)
|
||||
}
|
||||
if detail.Result.TaskID != detail.Task.ID {
|
||||
t.Fatalf("result task id = %d, want %d", detail.Result.TaskID, detail.Task.ID)
|
||||
}
|
||||
if !strings.Contains(detail.Result.OutputPath, "sku-main_enhance_optimized.jpg") {
|
||||
t.Fatalf("output path = %q, want simulated optimized path", detail.Result.OutputPath)
|
||||
}
|
||||
|
||||
got, err := service.GetTask(detail.Task.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get task: %v", err)
|
||||
}
|
||||
if got.Task.ID != detail.Task.ID {
|
||||
t.Fatalf("got task id = %d, want %d", got.Task.ID, detail.Task.ID)
|
||||
}
|
||||
|
||||
tasks, err := service.ListTasks(models.ImageTaskFilter{Status: imageTaskStatusSucceeded})
|
||||
if err != nil {
|
||||
t.Fatalf("list tasks: %v", err)
|
||||
}
|
||||
if len(tasks) != 1 {
|
||||
t.Fatalf("tasks len = %d, want 1", len(tasks))
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageOptimizationServiceCreateTaskRequiresSourcePath(t *testing.T) {
|
||||
store := newTestStore(t)
|
||||
service := NewImageOptimizationService(store)
|
||||
|
||||
_, err := service.CreateTask(models.ImageTaskInput{})
|
||||
if !errors.Is(err, errImageTaskSourcePathRequired) {
|
||||
t.Fatalf("create task error = %v, want source path required", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user