-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* made the logging path of fileDownload.go more straight-forward * refactored the file downloader into separate testable functions * added a testcase for `maybeResizeImage` * made shure that the file-downloading runs in a transaction * added a testcase for `ensureFileDoesNotExist` * fixed a typo * rebase
- Loading branch information
1 parent
301a0c7
commit 892c29b
Showing
5 changed files
with
183 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package cron | ||
|
||
import ( | ||
"image" | ||
"os" | ||
"testing" | ||
|
||
"github.com/disintegration/imaging" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMaybeResizeImage(t *testing.T) { | ||
t.Run("Resize Image", func(t *testing.T) { | ||
dstPath := "test_image.jpg" | ||
require.NoError(t, createDummyImage(dstPath, 2000, 1000)) | ||
defer os.Remove(dstPath) | ||
require.NoError(t, maybeResizeImage(dstPath)) | ||
img, err := imaging.Open(dstPath) | ||
require.NoError(t, err) | ||
require.Equal(t, 1280, img.Bounds().Dx()) | ||
require.Equal(t, 640, img.Bounds().Dy()) | ||
}) | ||
t.Run("Do not Resize smaller Image", func(t *testing.T) { | ||
dstPath := "test_image.jpg" | ||
require.NoError(t, createDummyImage(dstPath, 1000, 2000)) | ||
defer os.Remove(dstPath) | ||
require.NoError(t, maybeResizeImage(dstPath)) | ||
img, err := imaging.Open(dstPath) | ||
require.NoError(t, err) | ||
require.Equal(t, 1280, img.Bounds().Dx()) | ||
require.Equal(t, 2560, img.Bounds().Dy()) | ||
}) | ||
|
||
t.Run("Skip Non-Image", func(t *testing.T) { | ||
nonImageFile := "non_image.txt" | ||
content := []byte("Dummy Text") | ||
require.NoError(t, createDummyFile(nonImageFile, content)) | ||
defer os.Remove(nonImageFile) | ||
require.NoError(t, maybeResizeImage(nonImageFile)) | ||
contentAfterExecution, err := os.ReadFile(nonImageFile) | ||
require.NoError(t, err) | ||
require.Equal(t, content, contentAfterExecution) | ||
}) | ||
} | ||
|
||
func TestEnsureFileDoesNotExist(t *testing.T) { | ||
tmpFilePath := "test_dir/test_file.txt" | ||
defer func() { _ = os.RemoveAll("test_dir") }() | ||
|
||
t.Run("FileDoesNotExist", func(t *testing.T) { | ||
require.NoError(t, ensureFileDoesNotExist(tmpFilePath)) | ||
|
||
_, dirErr := os.Stat("test_dir") | ||
require.NoError(t, dirErr) | ||
|
||
_, fileErr := os.Stat(tmpFilePath) | ||
require.True(t, os.IsNotExist(fileErr)) | ||
}) | ||
|
||
t.Run("FileExists", func(t *testing.T) { | ||
_, createErr := os.Create(tmpFilePath) | ||
require.NoError(t, createErr) | ||
|
||
require.NoError(t, ensureFileDoesNotExist(tmpFilePath)) | ||
|
||
_, dirErr := os.Stat("test_dir") | ||
require.NoError(t, dirErr) | ||
|
||
_, fileErr := os.Stat(tmpFilePath) | ||
require.True(t, os.IsNotExist(fileErr)) | ||
}) | ||
} | ||
|
||
// createDummyImage creates a dummy image file with the specified dimensions | ||
func createDummyImage(filePath string, width, height int) error { | ||
img := image.NewRGBA(image.Rect(0, 0, width, height)) | ||
return imaging.Save(img, filePath, imaging.JPEGQuality(75)) | ||
} | ||
|
||
// createDummyFile creates a dummy non-image file | ||
func createDummyFile(filePath string, content []byte) error { | ||
file, err := os.Create(filePath) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
if _, err := file.Write(content); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters