63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|