Skip to content

Commit

Permalink
test(images): add a test function to validate images getting converte…
Browse files Browse the repository at this point in the history
…d to the pdf format
  • Loading branch information
danvergara committed Jan 11, 2024
1 parent 2621dab commit 6cdf961
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
163 changes: 163 additions & 0 deletions pkg/files/images/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func TestConvertImage(t *testing.T) {
images.TIFF,
images.BMP,
},
"Document": {
images.PDF,
},
},
},
},
Expand All @@ -105,6 +108,9 @@ func TestConvertImage(t *testing.T) {
images.TIFF,
images.BMP,
},
"Document": {
images.PDF,
},
},
},
},
Expand Down Expand Up @@ -154,6 +160,9 @@ func TestConvertImage(t *testing.T) {
images.TIFF,
images.BMP,
},
"Document": {
images.PDF,
},
},
},
},
Expand All @@ -177,6 +186,9 @@ func TestConvertImage(t *testing.T) {
images.TIFF,
images.WEBP,
},
"Document": {
images.PDF,
},
},
},
},
Expand All @@ -199,9 +211,57 @@ func TestConvertImage(t *testing.T) {
images.TIFF,
images.BMP,
},
"Document": {
images.PDF,
},
},
},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
inputImg, err := os.ReadFile(tc.input.filename)
require.NoError(t, err)

detectedFileType := mimetype.Detect(inputImg)
require.Equal(t, tc.input.mimetype, detectedFileType.String())

convertedImg, err := tc.input.imager.ConvertTo(
tc.input.targetFileType,
tc.input.targetFormat,
inputImg,
)

require.NoError(t, err)

detectedFileType = mimetype.Detect(convertedImg)
require.Equal(t, tc.expected.mimetype, detectedFileType.String())
formats := tc.input.imager.SupportedFormats()
require.EqualValues(t, tc.expected.supportedFormats, formats)
})
}
}

func TestConvertImageToDocument(t *testing.T) {
type input struct {
filename string
mimetype string
targetFileType string
targetFormat string
imager imager
}
type expected struct {
mimetype string
supportedFormats map[string][]string
}

var tests = []struct {
name string
input input
expected expected
}{
{
name: "png to pdf",
input: input{
Expand All @@ -228,6 +288,109 @@ func TestConvertImage(t *testing.T) {
},
},
},
{
name: "jpg to pdf",
input: input{
filename: "testdata/Golang_Gopher.jpg",
mimetype: "image/jpeg",
targetFileType: "Document",
targetFormat: "pdf",
imager: images.NewJpeg(),
},
expected: expected{
mimetype: "application/pdf",
supportedFormats: map[string][]string{
"Image": {
images.PNG,
images.GIF,
images.WEBP,
images.TIFF,
images.BMP,
},
"Document": {
images.PDF,
},
},
},
},
{
name: "bmp to pdf",
input: input{
filename: "testdata/sunset.bmp",
mimetype: "image/bmp",
targetFileType: "Document",
targetFormat: "pdf",
imager: images.NewBmp(),
},
expected: expected{
mimetype: "application/pdf",
supportedFormats: map[string][]string{
"Image": {
images.JPG,
images.JPEG,
images.PNG,
images.GIF,
images.TIFF,
images.WEBP,
},
"Document": {
images.PDF,
},
},
},
},
{
name: "gif to pdf",
input: input{
filename: "testdata/dancing-gopher.gif",
mimetype: "image/gif",
targetFileType: "Document",
targetFormat: "pdf",
imager: images.NewGif(),
},
expected: expected{
mimetype: "application/pdf",
supportedFormats: map[string][]string{
"Image": {
images.JPG,
images.JPEG,
images.PNG,
images.WEBP,
images.TIFF,
images.BMP,
},
"Document": {
images.PDF,
},
},
},
},
{
name: "webp to pdf",
input: input{
filename: "testdata/gopher.webp",
mimetype: "image/webp",
targetFileType: "Document",
targetFormat: "pdf",
imager: images.NewWebp(),
},
expected: expected{
mimetype: "application/pdf",
supportedFormats: map[string][]string{
"Image": {
images.JPG,
images.JPEG,
images.PNG,
images.GIF,
images.TIFF,
images.BMP,
},
"Document": {
images.PDF,
},
},
},
},
}

for _, tc := range tests {
Expand Down
Binary file added pkg/files/images/testdata/dancing-gopher.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6cdf961

Please sign in to comment.