Skip to content

Commit

Permalink
write test for FitAnimationGIF
Browse files Browse the repository at this point in the history
  • Loading branch information
logica0419 committed Dec 10, 2023
1 parent 33ddae6 commit 708fc5f
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions service/imaging/processor_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package imaging

import (
"bytes"
"fmt"
"image"
"image/png"
"io"
"io/fs"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/traPtitech/traQ/testdata/gif"
"github.com/traPtitech/traQ/utils"
)

const testdataFolder = "../../testdata/images/"
Expand Down Expand Up @@ -66,3 +70,95 @@ func TestProcessorDefault_Fit(t *testing.T) {
assert.Nil(t, err)
assertImg(t, actualImg, "test_fit.png")
}

func setupCustomConc(conc int) Processor {
processor := NewProcessor(Config{
MaxPixels: 500 * 500,
Concurrency: conc,
ThumbnailMaxSize: image.Point{X: 50, Y: 50},
})
return processor
}

func mustOpenGif(name string) fs.File {
f, err := gif.FS.Open(name)
if err != nil {
panic(err)
}
return f
}

func TestProcessorDefault_FitAnimationGIF(t *testing.T) {
t.Parallel()

testConc := []int{1, 2, 3, 4, 5}

test := []struct {
name string
file string
reader io.Reader
want []byte
err error
}{
{
name: "invalid (empty)",
reader: bytes.NewBufferString(""),
want: nil,
err: ErrInvalidImageSrc,
},
{
name: "invalid (invalid gif)",
reader: io.LimitReader(mustOpenGif("cube.gif"), 10),
want: nil,
err: ErrInvalidImageSrc,
},
{
name: "success (cube 正方形、透明ピクセルあり)",
file: "cube.gif",
want: utils.MustIoReaderToBytes(mustOpenGif("cube_resized.gif")),
err: nil,
},
{
name: "success (miku 横長、差分最適化)",
file: "miku.gif",
want: utils.MustIoReaderToBytes(mustOpenGif("miku_resized.gif")),
err: nil,
},
{
name: "success (parapara 正方形、差分最適化)",
file: "parapara.gif",
want: utils.MustIoReaderToBytes(mustOpenGif("parapara_resized.gif")),
err: nil,
},
}

for _, tt := range test {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

for _, conc := range testConc {
tt := tt
conc := conc

t.Run(fmt.Sprintf("%s, conc=%d", tt.name, conc), func(t *testing.T) {
t.Parallel()

processor := setupCustomConc(conc)
if tt.file != "" { // ファイルはこのタイミングで開かないと正常なデータにならない
tt.reader = mustOpenGif(tt.file)
}

actual, err := processor.FitAnimationGIF(tt.reader, 256, 256)
if tt.err != nil {
assert.Equal(t, tt.err, err)
} else {
assert.Nil(t, err)
assert.Equal(t, tt.want, utils.MustIoReaderToBytes(actual))
}
})
}
})
}
}

0 comments on commit 708fc5f

Please sign in to comment.