Backend: Add tests to internal/thumb
This commit is contained in:
parent
b4c27ffb51
commit
e3993b3dbf
2 changed files with 233 additions and 18 deletions
|
@ -10,37 +10,130 @@ import (
|
|||
)
|
||||
|
||||
func TestResampleOptions(t *testing.T) {
|
||||
method, filter, format := ResampleOptions(ResamplePng, ResampleFillCenter, ResampleDefault)
|
||||
t.Run("ResamplePng, FillCenter", func(t *testing.T) {
|
||||
method, filter, format := ResampleOptions(ResamplePng, ResampleFillCenter, ResampleDefault)
|
||||
|
||||
assert.Equal(t, ResampleFillCenter, method)
|
||||
assert.Equal(t, imaging.Lanczos.Support, filter.Support)
|
||||
assert.Equal(t, fs.TypePng, format)
|
||||
assert.Equal(t, ResampleFillCenter, method)
|
||||
assert.Equal(t, imaging.Lanczos.Support, filter.Support)
|
||||
assert.Equal(t, fs.TypePng, format)
|
||||
})
|
||||
t.Run("ResampleNearestNeighbor, FillTopLeft", func(t *testing.T) {
|
||||
method, filter, format := ResampleOptions(ResampleNearestNeighbor, ResampleFillTopLeft)
|
||||
|
||||
assert.Equal(t, ResampleFillTopLeft, method)
|
||||
assert.Equal(t, imaging.NearestNeighbor.Support, filter.Support)
|
||||
assert.Equal(t, fs.TypeJpeg, format)
|
||||
})
|
||||
t.Run("ResampleNearestNeighbor, FillBottomRight", func(t *testing.T) {
|
||||
method, filter, format := ResampleOptions(ResampleNearestNeighbor, ResampleFillBottomRight)
|
||||
|
||||
assert.Equal(t, ResampleFillBottomRight, method)
|
||||
assert.Equal(t, imaging.NearestNeighbor.Support, filter.Support)
|
||||
assert.Equal(t, fs.TypeJpeg, format)
|
||||
})
|
||||
}
|
||||
|
||||
func TestResample(t *testing.T) {
|
||||
tile50 := Types["tile_50"]
|
||||
t.Run("tile50 options", func(t *testing.T) {
|
||||
tile50 := Types["tile_50"]
|
||||
|
||||
src := "testdata/example.jpg"
|
||||
src := "testdata/example.jpg"
|
||||
|
||||
assert.FileExists(t, src)
|
||||
assert.FileExists(t, src)
|
||||
|
||||
img, err := imaging.Open(src, imaging.AutoOrientation(true))
|
||||
img, err := imaging.Open(src, imaging.AutoOrientation(true))
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
bounds := img.Bounds()
|
||||
bounds := img.Bounds()
|
||||
|
||||
assert.Equal(t, 750, bounds.Max.X)
|
||||
assert.Equal(t, 500, bounds.Max.Y)
|
||||
assert.Equal(t, 750, bounds.Max.X)
|
||||
assert.Equal(t, 500, bounds.Max.Y)
|
||||
|
||||
result := *Resample(&img, tile50.Width, tile50.Height, tile50.Options...)
|
||||
result := *Resample(&img, tile50.Width, tile50.Height, tile50.Options...)
|
||||
|
||||
boundsNew := result.Bounds()
|
||||
boundsNew := result.Bounds()
|
||||
|
||||
assert.Equal(t, 50, boundsNew.Max.X)
|
||||
assert.Equal(t, 50, boundsNew.Max.Y)
|
||||
assert.Equal(t, 50, boundsNew.Max.X)
|
||||
assert.Equal(t, 50, boundsNew.Max.Y)
|
||||
})
|
||||
t.Run("left_224 options", func(t *testing.T) {
|
||||
left_224 := Types["left_224"]
|
||||
|
||||
src := "testdata/example.jpg"
|
||||
|
||||
assert.FileExists(t, src)
|
||||
|
||||
img, err := imaging.Open(src, imaging.AutoOrientation(true))
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
bounds := img.Bounds()
|
||||
|
||||
assert.Equal(t, 750, bounds.Max.X)
|
||||
assert.Equal(t, 500, bounds.Max.Y)
|
||||
|
||||
result := *Resample(&img, left_224.Width, left_224.Height, left_224.Options...)
|
||||
|
||||
boundsNew := result.Bounds()
|
||||
|
||||
assert.Equal(t, 224, boundsNew.Max.X)
|
||||
assert.Equal(t, 224, boundsNew.Max.Y)
|
||||
})
|
||||
t.Run("right_224 options", func(t *testing.T) {
|
||||
right_224 := Types["right_224"]
|
||||
|
||||
src := "testdata/example.jpg"
|
||||
|
||||
assert.FileExists(t, src)
|
||||
|
||||
img, err := imaging.Open(src, imaging.AutoOrientation(true))
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
bounds := img.Bounds()
|
||||
|
||||
assert.Equal(t, 750, bounds.Max.X)
|
||||
assert.Equal(t, 500, bounds.Max.Y)
|
||||
|
||||
result := *Resample(&img, right_224.Width, right_224.Height, right_224.Options...)
|
||||
|
||||
boundsNew := result.Bounds()
|
||||
|
||||
assert.Equal(t, 224, boundsNew.Max.X)
|
||||
assert.Equal(t, 224, boundsNew.Max.Y)
|
||||
})
|
||||
t.Run("fit_1280 options", func(t *testing.T) {
|
||||
fit_1280 := Types["fit_1280"]
|
||||
|
||||
src := "testdata/example.jpg"
|
||||
|
||||
assert.FileExists(t, src)
|
||||
|
||||
img, err := imaging.Open(src, imaging.AutoOrientation(true))
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
bounds := img.Bounds()
|
||||
|
||||
assert.Equal(t, 750, bounds.Max.X)
|
||||
assert.Equal(t, 500, bounds.Max.Y)
|
||||
|
||||
result := *Resample(&img, fit_1280.Width, fit_1280.Height, fit_1280.Options...)
|
||||
|
||||
boundsNew := result.Bounds()
|
||||
|
||||
assert.Equal(t, 750, boundsNew.Max.X)
|
||||
assert.Equal(t, 500, boundsNew.Max.Y)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPostfix(t *testing.T) {
|
||||
|
@ -75,6 +168,38 @@ func TestFilename(t *testing.T) {
|
|||
|
||||
assert.Equal(t, "testdata/1/2/3/123456789098765432_720x720_fit.jpg", result)
|
||||
})
|
||||
t.Run("invalid width", func(t *testing.T) {
|
||||
colorThumb := Types["colors"]
|
||||
|
||||
result, err := Filename("123456789098765432", "testdata", -2, colorThumb.Height, colorThumb.Options...)
|
||||
|
||||
assert.Equal(t, "resample: width exceeds limit (-2)", err.Error())
|
||||
assert.Empty(t, result)
|
||||
})
|
||||
t.Run("invalid height", func(t *testing.T) {
|
||||
colorThumb := Types["colors"]
|
||||
|
||||
result, err := Filename("123456789098765432", "testdata", colorThumb.Width, -3, colorThumb.Options...)
|
||||
|
||||
assert.Equal(t, "resample: height exceeds limit (-3)", err.Error())
|
||||
assert.Empty(t, result)
|
||||
})
|
||||
t.Run("invalid hash", func(t *testing.T) {
|
||||
colorThumb := Types["colors"]
|
||||
|
||||
result, err := Filename("12", "testdata", colorThumb.Width, colorThumb.Height, colorThumb.Options...)
|
||||
|
||||
assert.Equal(t, "resample: file hash is empty or too short (12)", err.Error())
|
||||
assert.Empty(t, result)
|
||||
})
|
||||
t.Run("invalid thumb path", func(t *testing.T) {
|
||||
colorThumb := Types["colors"]
|
||||
|
||||
result, err := Filename("123456789098765432", "", colorThumb.Width, colorThumb.Height, colorThumb.Options...)
|
||||
|
||||
assert.Equal(t, "resample: folder is empty", err.Error())
|
||||
assert.Empty(t, result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestFromFile(t *testing.T) {
|
||||
|
@ -107,6 +232,14 @@ func TestFromFile(t *testing.T) {
|
|||
assert.Equal(t, "", fileName)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
t.Run("empty filename", func(t *testing.T) {
|
||||
colorThumb := Types["colors"]
|
||||
|
||||
fileName, err := FromFile("", "193456789098765432", "testdata", colorThumb.Width, colorThumb.Height, colorThumb.Options...)
|
||||
|
||||
assert.Equal(t, "", fileName)
|
||||
assert.Equal(t, "resample: image filename is empty or too short ()", err.Error())
|
||||
})
|
||||
}
|
||||
|
||||
func TestFromCache(t *testing.T) {
|
||||
|
@ -136,6 +269,25 @@ func TestFromCache(t *testing.T) {
|
|||
assert.Equal(t, "", fileName)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
t.Run("invalid hash", func(t *testing.T) {
|
||||
tile50 := Types["tile_50"]
|
||||
src := "testdata/example.jpg"
|
||||
|
||||
assert.FileExists(t, src)
|
||||
|
||||
fileName, err := FromCache(src, "12", "testdata", tile50.Width, tile50.Height, tile50.Options...)
|
||||
|
||||
assert.Equal(t, "resample: file hash is empty or too short (12)", err.Error())
|
||||
assert.Empty(t, fileName)
|
||||
})
|
||||
t.Run("empty filename", func(t *testing.T) {
|
||||
tile50 := Types["tile_50"]
|
||||
|
||||
fileName, err := FromCache("", "193456789098765432", "testdata", tile50.Width, tile50.Height, tile50.Options...)
|
||||
|
||||
assert.Equal(t, "resample: image filename is empty or too short ()", err.Error())
|
||||
assert.Empty(t, fileName)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
|
@ -176,4 +328,52 @@ func TestCreate(t *testing.T) {
|
|||
assert.Equal(t, 500, boundsNew.Max.X)
|
||||
assert.Equal(t, 500, boundsNew.Max.Y)
|
||||
})
|
||||
t.Run("invalid width", func(t *testing.T) {
|
||||
tile500 := Types["tile_500"]
|
||||
src := "testdata/example.jpg"
|
||||
dst := "testdata/example.tile_500.jpg"
|
||||
|
||||
assert.FileExists(t, src)
|
||||
assert.NoFileExists(t, dst)
|
||||
|
||||
img, err := imaging.Open(src, imaging.AutoOrientation(true))
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
bounds := img.Bounds()
|
||||
|
||||
assert.Equal(t, 750, bounds.Max.X)
|
||||
assert.Equal(t, 500, bounds.Max.Y)
|
||||
|
||||
resized, err := Create(&img, dst, -5, tile500.Height, tile500.Options...)
|
||||
|
||||
assert.Equal(t, "resample: width has an invalid value (-5)", err.Error())
|
||||
t.Log(resized)
|
||||
})
|
||||
t.Run("invalid height", func(t *testing.T) {
|
||||
tile500 := Types["tile_500"]
|
||||
src := "testdata/example.jpg"
|
||||
dst := "testdata/example.tile_500.jpg"
|
||||
|
||||
assert.FileExists(t, src)
|
||||
assert.NoFileExists(t, dst)
|
||||
|
||||
img, err := imaging.Open(src, imaging.AutoOrientation(true))
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
bounds := img.Bounds()
|
||||
|
||||
assert.Equal(t, 750, bounds.Max.X)
|
||||
assert.Equal(t, 500, bounds.Max.Y)
|
||||
|
||||
resized, err := Create(&img, dst, tile500.Width, -3, tile500.Options...)
|
||||
|
||||
assert.Equal(t, "resample: height has an invalid value (-3)", err.Error())
|
||||
t.Log(resized)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -39,3 +39,18 @@ func TestType_SkipPreRender(t *testing.T) {
|
|||
Size = 3840
|
||||
Limit = 3840
|
||||
}
|
||||
|
||||
func TestResampleFilter_Imaging(t *testing.T) {
|
||||
t.Run("Blackman", func(t *testing.T) {
|
||||
r := ResampleBlackman.Imaging()
|
||||
assert.Equal(t, float64(3), r.Support)
|
||||
})
|
||||
t.Run("Cubic", func(t *testing.T) {
|
||||
r := ResampleCubic.Imaging()
|
||||
assert.Equal(t, float64(2), r.Support)
|
||||
})
|
||||
t.Run("Linear", func(t *testing.T) {
|
||||
r := ResampleLinear.Imaging()
|
||||
assert.Equal(t, float64(1), r.Support)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue