From a01288a5ac4c27e121f15624614fbda1c491c0d3 Mon Sep 17 00:00:00 2001 From: theresa Date: Wed, 3 Mar 2021 14:44:30 +0100 Subject: [PATCH] Tests: Add tests for internal/thumb --- internal/thumb/create_test.go | 55 ++++++++++ internal/thumb/jpeg_test.go | 198 ++++++++++++++++++++++++++++++++++ 2 files changed, 253 insertions(+) diff --git a/internal/thumb/create_test.go b/internal/thumb/create_test.go index eccf5c63c..d6b09c8ee 100644 --- a/internal/thumb/create_test.go +++ b/internal/thumb/create_test.go @@ -233,6 +233,24 @@ func TestFromFile(t *testing.T) { assert.FileExists(t, dst) }) + t.Run("orientation >1 ", func(t *testing.T) { + colorThumb := Types["colors"] + src := "testdata/example.gif" + dst := "testdata/1/2/3/123456789098765432_3x3_resize.png" + + assert.FileExists(t, src) + + fileName, err := FromFile(src, "123456789098765432", "testdata", colorThumb.Width, colorThumb.Height, 3, colorThumb.Options...) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, dst, fileName) + + assert.FileExists(t, dst) + }) + t.Run("missing file", func(t *testing.T) { colorThumb := Types["colors"] src := "testdata/example.xxx" @@ -349,6 +367,43 @@ func TestCreate(t *testing.T) { assert.Equal(t, 500, boundsNew.Max.X) assert.Equal(t, 500, boundsNew.Max.Y) }) + t.Run("width & height <= 150", 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, 150, 150, tile500.Options...) + + if err != nil { + t.Fatal(err) + } + + assert.FileExists(t, dst) + + if err := os.Remove(dst); err != nil { + t.Fatal(err) + } + + imgNew := resized + boundsNew := imgNew.Bounds() + + assert.Equal(t, 150, boundsNew.Max.X) + assert.Equal(t, 150, boundsNew.Max.Y) + }) t.Run("invalid width", func(t *testing.T) { tile500 := Types["tile_500"] src := "testdata/example.jpg" diff --git a/internal/thumb/jpeg_test.go b/internal/thumb/jpeg_test.go index 427c8b946..823006aae 100644 --- a/internal/thumb/jpeg_test.go +++ b/internal/thumb/jpeg_test.go @@ -30,6 +30,204 @@ func TestJpeg(t *testing.T) { assert.Equal(t, 67, bounds.Max.X) assert.Equal(t, 100, bounds.Max.Y) + if err := os.Remove(dst); err != nil { + t.Fatal(err) + } + }) + t.Run("OrientationFlipH", func(t *testing.T) { + src := "testdata/example." + ext + dst := "testdata/example." + ext + fs.JpegExt + + assert.NoFileExists(t, dst) + + img, err := Jpeg(src, dst, OrientationFlipH) + + if err != nil { + t.Fatal(err) + } + + assert.FileExists(t, dst) + + bounds := img.Bounds() + assert.Equal(t, 100, bounds.Max.X) + assert.Equal(t, 67, bounds.Max.Y) + + if err := os.Remove(dst); err != nil { + t.Fatal(err) + } + }) + t.Run("OrientationFlipV", func(t *testing.T) { + src := "testdata/example." + ext + dst := "testdata/example." + ext + fs.JpegExt + + assert.NoFileExists(t, dst) + + img, err := Jpeg(src, dst, OrientationFlipV) + + if err != nil { + t.Fatal(err) + } + + assert.FileExists(t, dst) + + bounds := img.Bounds() + assert.Equal(t, 100, bounds.Max.X) + assert.Equal(t, 67, bounds.Max.Y) + + if err := os.Remove(dst); err != nil { + t.Fatal(err) + } + }) + t.Run("OrientationRotate90", func(t *testing.T) { + src := "testdata/example." + ext + dst := "testdata/example." + ext + fs.JpegExt + + assert.NoFileExists(t, dst) + + img, err := Jpeg(src, dst, OrientationRotate90) + + if err != nil { + t.Fatal(err) + } + + assert.FileExists(t, dst) + + bounds := img.Bounds() + assert.Equal(t, 67, bounds.Max.X) + assert.Equal(t, 100, bounds.Max.Y) + + if err := os.Remove(dst); err != nil { + t.Fatal(err) + } + }) + t.Run("OrientationRotate180", func(t *testing.T) { + src := "testdata/example." + ext + dst := "testdata/example." + ext + fs.JpegExt + + assert.NoFileExists(t, dst) + + img, err := Jpeg(src, dst, OrientationRotate180) + + if err != nil { + t.Fatal(err) + } + + assert.FileExists(t, dst) + + bounds := img.Bounds() + assert.Equal(t, 100, bounds.Max.X) + assert.Equal(t, 67, bounds.Max.Y) + + if err := os.Remove(dst); err != nil { + t.Fatal(err) + } + }) + t.Run("OrientationTranspose", func(t *testing.T) { + src := "testdata/example." + ext + dst := "testdata/example." + ext + fs.JpegExt + + assert.NoFileExists(t, dst) + + img, err := Jpeg(src, dst, OrientationTranspose) + + if err != nil { + t.Fatal(err) + } + + assert.FileExists(t, dst) + + bounds := img.Bounds() + assert.Equal(t, 67, bounds.Max.X) + assert.Equal(t, 100, bounds.Max.Y) + + if err := os.Remove(dst); err != nil { + t.Fatal(err) + } + }) + t.Run("OrientationTransverse", func(t *testing.T) { + src := "testdata/example." + ext + dst := "testdata/example." + ext + fs.JpegExt + + assert.NoFileExists(t, dst) + + img, err := Jpeg(src, dst, OrientationTransverse) + + if err != nil { + t.Fatal(err) + } + + assert.FileExists(t, dst) + + bounds := img.Bounds() + assert.Equal(t, 67, bounds.Max.X) + assert.Equal(t, 100, bounds.Max.Y) + + if err := os.Remove(dst); err != nil { + t.Fatal(err) + } + }) + t.Run("OrientationUnspecified", func(t *testing.T) { + src := "testdata/example." + ext + dst := "testdata/example." + ext + fs.JpegExt + + assert.NoFileExists(t, dst) + + img, err := Jpeg(src, dst, OrientationUnspecified) + + if err != nil { + t.Fatal(err) + } + + assert.FileExists(t, dst) + + bounds := img.Bounds() + assert.Equal(t, 100, bounds.Max.X) + assert.Equal(t, 67, bounds.Max.Y) + + if err := os.Remove(dst); err != nil { + t.Fatal(err) + } + }) + t.Run("OrientationNormal", func(t *testing.T) { + src := "testdata/example." + ext + dst := "testdata/example." + ext + fs.JpegExt + + assert.NoFileExists(t, dst) + + img, err := Jpeg(src, dst, OrientationNormal) + + if err != nil { + t.Fatal(err) + } + + assert.FileExists(t, dst) + + bounds := img.Bounds() + assert.Equal(t, 100, bounds.Max.X) + assert.Equal(t, 67, bounds.Max.Y) + + if err := os.Remove(dst); err != nil { + t.Fatal(err) + } + }) + t.Run("invalid orientation", func(t *testing.T) { + src := "testdata/example." + ext + dst := "testdata/example." + ext + fs.JpegExt + + assert.NoFileExists(t, dst) + + img, err := Jpeg(src, dst, 500) + + if err != nil { + t.Fatal(err) + } + + assert.FileExists(t, dst) + + bounds := img.Bounds() + assert.Equal(t, 100, bounds.Max.X) + assert.Equal(t, 67, bounds.Max.Y) + if err := os.Remove(dst); err != nil { t.Fatal(err) }