feat: file_save_disk, file_delete, file_serve, upload_parse, upload_handler, thumbnail_generate (issue 0014 fase 3)
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package infra
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFileSaveDisk(t *testing.T) {
|
||||
t.Run("guarda contenido en baseDir con nombre UUID", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
body := strings.NewReader("hello world")
|
||||
got, err := FileSaveDisk(dir, "saludos.txt", body)
|
||||
if err != nil {
|
||||
t.Fatalf("FileSaveDisk err: %v", err)
|
||||
}
|
||||
if got.Filename != "saludos.txt" {
|
||||
t.Errorf("got Filename %q, want saludos.txt", got.Filename)
|
||||
}
|
||||
if !strings.HasSuffix(got.StoredName, ".txt") {
|
||||
t.Errorf("got StoredName %q, want suffix .txt", got.StoredName)
|
||||
}
|
||||
if !strings.HasPrefix(got.Path, dir) {
|
||||
t.Errorf("got Path %q, want prefix %q", got.Path, dir)
|
||||
}
|
||||
if got.Size != int64(len("hello world")) {
|
||||
t.Errorf("got Size %d, want %d", got.Size, len("hello world"))
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(got.Path)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile: %v", err)
|
||||
}
|
||||
if string(data) != "hello world" {
|
||||
t.Errorf("contenido en disco %q, want %q", data, "hello world")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("crea baseDir si no existe", func(t *testing.T) {
|
||||
base := filepath.Join(t.TempDir(), "nested", "uploads")
|
||||
body := strings.NewReader("x")
|
||||
got, err := FileSaveDisk(base, "a.png", body)
|
||||
if err != nil {
|
||||
t.Fatalf("FileSaveDisk err: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(got.Path); err != nil {
|
||||
t.Fatalf("archivo no escrito: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("infiere ContentType desde la extension", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
got, err := FileSaveDisk(dir, "logo.png", strings.NewReader("x"))
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if !strings.HasPrefix(got.ContentType, "image/png") {
|
||||
t.Errorf("got ContentType %q, want image/png prefix", got.ContentType)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user