Include a group of small but real media files for testing. (#1131)

* Move scanner/test_data/* to a stand-alone directory.

* Include real medias.

* Rename testdata to test_data

* Rename test_data/scanner -> test_data/library

* Clean tests to avoid depneding env.

* Rename: scanner/test_data -> scanner/test_media

* Rename: test_data/bin -> test_data/mock_bin

* Fix paths in tests.
This commit is contained in:
Googol Lee
2024-11-23 09:02:11 +01:00
committed by GitHub
parent dde45c29b9
commit f1b523a27e
52 changed files with 75 additions and 65 deletions

View File

@@ -3,10 +3,10 @@ package executable_worker
import (
"testing"
"github.com/photoview/photoview/api/test_utils/test_env"
"github.com/photoview/photoview/api/test_utils"
)
const testdataBinPath = "./testdata/bin"
const testdataBinPath = "./test_data/mock_bin"
func TestInitFfprobePath(t *testing.T) {
t.Run("PathFail", func(t *testing.T) {
@@ -17,10 +17,10 @@ func TestInitFfprobePath(t *testing.T) {
})
t.Run("VersionFail", func(t *testing.T) {
donePath := test_env.SetPathWithCurrent(testdataBinPath)
donePath := test_utils.SetPathWithCurrent(testdataBinPath)
defer donePath()
doneEnv := test_env.SetEnv("FAIL_WITH", "expect failure")
doneEnv := test_utils.SetEnv("FAIL_WITH", "expect failure")
defer doneEnv()
err := SetFfprobePath()
@@ -30,7 +30,7 @@ func TestInitFfprobePath(t *testing.T) {
})
t.Run("Succeed", func(t *testing.T) {
donePath := test_env.SetPathWithCurrent(testdataBinPath)
donePath := test_utils.SetPathWithCurrent(testdataBinPath)
defer donePath()
err := SetFfprobePath()

View File

@@ -5,13 +5,13 @@ import (
"regexp"
"testing"
"github.com/photoview/photoview/api/test_utils/test_env"
"github.com/photoview/photoview/api/test_utils"
"github.com/photoview/photoview/api/utils"
"gopkg.in/vansante/go-ffprobe.v2"
)
func TestFfmpegNotExist(t *testing.T) {
done := test_env.SetPathWithCurrent()
done := test_utils.SetPathWithCurrent()
defer done()
Ffmpeg = newFfmpegCli()
@@ -34,10 +34,10 @@ func TestFfmpegNotExist(t *testing.T) {
}
func TestFfmpegVersionFail(t *testing.T) {
donePath := test_env.SetPathWithCurrent(testdataBinPath)
donePath := test_utils.SetPathWithCurrent(testdataBinPath)
defer donePath()
doneEnv := test_env.SetEnv("FAIL_WITH", "expect failure")
doneEnv := test_utils.SetEnv("FAIL_WITH", "expect failure")
defer doneEnv()
Ffmpeg = newFfmpegCli()
@@ -60,10 +60,10 @@ func TestFfmpegVersionFail(t *testing.T) {
}
func TestFfmpegIgnore(t *testing.T) {
donePath := test_env.SetPathWithCurrent(testdataBinPath)
donePath := test_utils.SetPathWithCurrent(testdataBinPath)
defer donePath()
doneEnv := test_env.SetEnv("PHOTOVIEW_DISABLE_VIDEO_ENCODING", "true")
doneEnv := test_utils.SetEnv("PHOTOVIEW_DISABLE_VIDEO_ENCODING", "true")
defer doneEnv()
Ffmpeg = newFfmpegCli()
@@ -86,7 +86,7 @@ func TestFfmpegIgnore(t *testing.T) {
}
func TestFfmpeg(t *testing.T) {
done := test_env.SetPathWithCurrent(testdataBinPath)
done := test_utils.SetPathWithCurrent(testdataBinPath)
defer done()
Ffmpeg = newFfmpegCli()
@@ -96,14 +96,14 @@ func TestFfmpeg(t *testing.T) {
}
t.Run("EncodeMp4Failed", func(t *testing.T) {
doneEnv := test_env.SetEnv("FAIL_WITH", "expect failure")
doneEnv := test_utils.SetEnv("FAIL_WITH", "expect failure")
defer doneEnv()
err := Ffmpeg.EncodeMp4("input", "output")
if err == nil {
t.Fatalf("Ffmpeg.EncodeMp4(...) = nil, should be an error.")
}
if got, want := err.Error(), `^encoding video with ".*/testdata/bin/ffmpeg" \[-i input -vcodec h264 .* output\] error: .*$`; !regexp.MustCompile(want).MatchString(got) {
if got, want := err.Error(), `^encoding video with ".*/test_data/mock_bin/ffmpeg" \[-i input -vcodec h264 .* output\] error: .*$`; !regexp.MustCompile(want).MatchString(got) {
t.Errorf("Ffmpeg.EncodeMp4(...) = %q, should be as reg pattern %q", got, want)
}
})
@@ -121,14 +121,14 @@ func TestFfmpeg(t *testing.T) {
},
}
t.Run("EncodeVideoThumbnailMp4Failed", func(t *testing.T) {
doneEnv := test_env.SetEnv("FAIL_WITH", "expect failure")
doneEnv := test_utils.SetEnv("FAIL_WITH", "expect failure")
defer doneEnv()
err := Ffmpeg.EncodeVideoThumbnail("input", "output", probeData)
if err == nil {
t.Fatalf("Ffmpeg.EncodeVideoThumbnail(...) = nil, should be an error.")
}
if got, want := err.Error(), `^encoding video thumbnail with ".*/testdata/bin/ffmpeg" \[-ss 2 -i input .* output\] error: .*$`; !regexp.MustCompile(want).MatchString(got) {
if got, want := err.Error(), `^encoding video thumbnail with ".*/test_data/mock_bin/ffmpeg" \[-ss 2 -i input .* output\] error: .*$`; !regexp.MustCompile(want).MatchString(got) {
t.Errorf("Ffmpeg.EncodeVideoThumbnail(...) = %q, should be as reg pattern %q", got, want)
}
})
@@ -142,43 +142,43 @@ func TestFfmpeg(t *testing.T) {
}
func TestFfmpegWithHWAcc(t *testing.T) {
doneCodec := test_env.SetEnv(utils.EnvVideoHardwareAcceleration.GetName(), "qsv")
doneCodec := test_utils.SetEnv(utils.EnvVideoHardwareAcceleration.GetName(), "qsv")
defer doneCodec()
donePath := test_env.SetPathWithCurrent(testdataBinPath)
donePath := test_utils.SetPathWithCurrent(testdataBinPath)
defer donePath()
Ffmpeg = newFfmpegCli()
doneEnv := test_env.SetEnv("FAIL_WITH", "expect failure")
doneEnv := test_utils.SetEnv("FAIL_WITH", "expect failure")
defer doneEnv()
err := Ffmpeg.EncodeMp4("input", "output")
if err == nil {
t.Fatalf("Ffmpeg.EncodeMp4(...) = nil, should be an error.")
}
if got, want := err.Error(), `^encoding video with ".*/testdata/bin/ffmpeg" \[-i input -vcodec h264_qsv .* output\] error: .*$`; !regexp.MustCompile(want).MatchString(got) {
if got, want := err.Error(), `^encoding video with ".*/test_data/mock_bin/ffmpeg" \[-i input -vcodec h264_qsv .* output\] error: .*$`; !regexp.MustCompile(want).MatchString(got) {
t.Errorf("Ffmpeg.EncodeMp4(...) = %q, should be as reg pattern %q", got, want)
}
}
func TestFfmpegWithCustomCOdec(t *testing.T) {
doneCodec := test_env.SetEnv(utils.EnvVideoHardwareAcceleration.GetName(), "_custom")
doneCodec := test_utils.SetEnv(utils.EnvVideoHardwareAcceleration.GetName(), "_custom")
defer doneCodec()
donePath := test_env.SetPathWithCurrent(testdataBinPath)
donePath := test_utils.SetPathWithCurrent(testdataBinPath)
defer donePath()
Ffmpeg = newFfmpegCli()
doneEnv := test_env.SetEnv("FAIL_WITH", "expect failure")
doneEnv := test_utils.SetEnv("FAIL_WITH", "expect failure")
defer doneEnv()
err := Ffmpeg.EncodeMp4("input", "output")
if err == nil {
t.Fatalf("Ffmpeg.EncodeMp4(...) = nil, should be an error.")
}
if got, want := err.Error(), `^encoding video with ".*/testdata/bin/ffmpeg" \[-i input -vcodec custom .* output\] error: .*$`; !regexp.MustCompile(want).MatchString(got) {
if got, want := err.Error(), `^encoding video with ".*/test_data/mock_bin/ffmpeg" \[-i input -vcodec custom .* output\] error: .*$`; !regexp.MustCompile(want).MatchString(got) {
t.Errorf("Ffmpeg.EncodeMp4(...) = %q, should be as reg pattern %q", got, want)
}
}

View File

@@ -5,11 +5,11 @@ import (
"regexp"
"testing"
"github.com/photoview/photoview/api/test_utils/test_env"
"github.com/photoview/photoview/api/test_utils"
)
func TestMagickCliNotExist(t *testing.T) {
done := test_env.SetPathWithCurrent()
done := test_utils.SetPathWithCurrent()
defer done()
Magick = newMagickCli()
@@ -28,10 +28,10 @@ func TestMagickCliNotExist(t *testing.T) {
}
func TestMagickCliIgnore(t *testing.T) {
donePath := test_env.SetPathWithCurrent(testdataBinPath)
donePath := test_utils.SetPathWithCurrent(testdataBinPath)
defer donePath()
doneDisableRaw := test_env.SetEnv("PHOTOVIEW_DISABLE_RAW_PROCESSING", "true")
doneDisableRaw := test_utils.SetEnv("PHOTOVIEW_DISABLE_RAW_PROCESSING", "true")
defer doneDisableRaw()
Magick = newMagickCli()
@@ -50,10 +50,10 @@ func TestMagickCliIgnore(t *testing.T) {
}
func TestMagickCliVersionFail(t *testing.T) {
donePath := test_env.SetPathWithCurrent(testdataBinPath)
donePath := test_utils.SetPathWithCurrent(testdataBinPath)
defer donePath()
done := test_env.SetEnv("FAIL_WITH", "failure")
done := test_utils.SetEnv("FAIL_WITH", "failure")
defer done()
Magick = newMagickCli()
@@ -72,7 +72,7 @@ func TestMagickCliVersionFail(t *testing.T) {
}
func TestMagickCliFail(t *testing.T) {
donePath := test_env.SetPathWithCurrent(testdataBinPath)
donePath := test_utils.SetPathWithCurrent(testdataBinPath)
defer donePath()
Magick = newMagickCli()
@@ -81,7 +81,7 @@ func TestMagickCliFail(t *testing.T) {
t.Fatal("MagickCli should be installed")
}
done := test_env.SetEnv("FAIL_WITH", "failure")
done := test_utils.SetEnv("FAIL_WITH", "failure")
defer done()
err := Magick.EncodeJpeg("input", "output", 70)
@@ -89,13 +89,13 @@ func TestMagickCliFail(t *testing.T) {
t.Fatalf(`MagickCli.EncodeJpeg(...) = nil, should be an error.`)
}
if got, want := err.Error(), `^encoding image with ".*/testdata/bin/magick \[input -auto-orient -quality 70 output\]" error: .*$`; !regexp.MustCompile(want).MatchString(got) {
if got, want := err.Error(), `^encoding image with ".*/test_data/mock_bin/magick \[input -auto-orient -quality 70 output\]" error: .*$`; !regexp.MustCompile(want).MatchString(got) {
t.Errorf(`MagickCli.EncodeJpeg(...) = %q, should be matched with reg pattern %q`, got, want)
}
}
func TestMagickCliSucceed(t *testing.T) {
donePath := test_env.SetPathWithCurrent(testdataBinPath)
donePath := test_utils.SetPathWithCurrent(testdataBinPath)
defer donePath()
Magick = newMagickCli()

View File

@@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/assert"
)
const testDataPath = "./test_data"
const testDataPath = "./test_media/library"
func TestNewRootPath(t *testing.T) {
db := test_utils.DatabaseTest(t)
@@ -29,7 +29,7 @@ func TestNewRootPath(t *testing.T) {
}
assert.NotNil(t, album)
assert.Contains(t, album.Path, "/api/scanner/test_data")
assert.Contains(t, album.Path, "/api/scanner/test_media")
assert.NotEmpty(t, album.Owners)
})
@@ -65,7 +65,7 @@ func TestNewRootPath(t *testing.T) {
}
assert.NotNil(t, album)
assert.Contains(t, album.Path, "/api/scanner/test_data")
assert.Contains(t, album.Path, "/api/scanner/test_media")
ownerCount := db.Model(&album).Association("Owners").Count()
assert.EqualValues(t, 2, ownerCount)

View File

@@ -10,6 +10,7 @@ import (
"github.com/photoview/photoview/api/graphql/models"
"github.com/photoview/photoview/api/scanner/face_detection"
"github.com/photoview/photoview/api/test_utils"
scanner_utils "github.com/photoview/photoview/api/test_utils/scanner"
"github.com/stretchr/testify/assert"
)
@@ -31,7 +32,7 @@ func TestCleanupMedia(t *testing.T) {
}
testDir := t.TempDir()
assert.NoError(t, copy.Copy("../../test_data", testDir))
assert.NoError(t, copy.Copy("../../test_media/library", testDir))
countAllMedia := func() int {
var allMedia []*models.Media
@@ -79,31 +80,31 @@ func TestCleanupMedia(t *testing.T) {
}
t.Run("Modify albums", func(t *testing.T) {
test_utils.RunScannerOnUser(t, db, user1)
scanner_utils.RunScannerOnUser(t, db, user1)
assert.Equal(t, 9, countAllMedia())
assert.Equal(t, 18, countAllMediaURLs())
// move faces directory
assert.NoError(t, os.Rename(path.Join(testDir, "faces"), path.Join(testDir, "faces_moved")))
test_utils.RunScannerAll(t, db)
scanner_utils.RunScannerAll(t, db)
assert.Equal(t, 9, countAllMedia())
assert.Equal(t, 18, countAllMediaURLs())
// remove faces_moved directory
assert.NoError(t, os.RemoveAll(path.Join(testDir, "faces_moved")))
test_utils.RunScannerAll(t, db)
scanner_utils.RunScannerAll(t, db)
assert.Equal(t, 3, countAllMedia())
assert.Equal(t, 6, countAllMediaURLs())
})
t.Run("Modify images", func(t *testing.T) {
assert.NoError(t, os.Rename(path.Join(testDir, "buttercup_close_summer_yellow.jpg"), path.Join(testDir, "yellow-flower.jpg")))
test_utils.RunScannerAll(t, db)
scanner_utils.RunScannerAll(t, db)
assert.Equal(t, 3, countAllMedia())
assert.Equal(t, 6, countAllMediaURLs())
assert.NoError(t, os.Remove(path.Join(testDir, "lilac_lilac_bush_lilac.jpg")))
test_utils.RunScannerAll(t, db)
scanner_utils.RunScannerAll(t, db)
assert.Equal(t, 2, countAllMedia())
assert.Equal(t, 4, countAllMediaURLs())
})

View File

@@ -8,7 +8,7 @@ import (
"testing"
"github.com/photoview/photoview/api/scanner/scanner_task"
"github.com/photoview/photoview/api/test_utils/test_env"
"github.com/photoview/photoview/api/test_utils"
"github.com/photoview/photoview/api/utils"
)
@@ -69,11 +69,11 @@ func TestCounterpartFilesTaskMediaFound(t *testing.T) {
},
}
mediaPath := test_env.PathFromAPIRoot("scanner/test_data/fake_media")
mediaPath := test_utils.PathFromAPIRoot("scanner/test_media/fake_media")
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
done := test_env.SetEnv(string(utils.EnvDisableRawProcessing), fmt.Sprintf("%v", tc.disableRawProcessing))
done := test_utils.SetEnv(string(utils.EnvDisableRawProcessing), fmt.Sprintf("%v", tc.disableRawProcessing))
defer done()
ctx := scanner_task.NewTaskContext(context.Background(), nil, nil, nil)

View File

@@ -8,6 +8,7 @@ import (
"github.com/photoview/photoview/api/graphql/models"
"github.com/photoview/photoview/api/scanner/face_detection"
"github.com/photoview/photoview/api/test_utils"
scanner_utils "github.com/photoview/photoview/api/test_utils/scanner"
"github.com/stretchr/testify/assert"
)
@@ -27,7 +28,7 @@ func TestFullScan(t *testing.T) {
rootAlbum := models.Album{
Title: "root album",
Path: "./test_data",
Path: "./test_media/library",
}
if !assert.NoError(t, db.Save(&rootAlbum).Error) {
@@ -43,7 +44,7 @@ func TestFullScan(t *testing.T) {
return
}
test_utils.RunScannerOnUser(t, db, user)
scanner_utils.RunScannerOnUser(t, db, user)
var allMedia []*models.Media
if !assert.NoError(t, db.Find(&allMedia).Error) {

View File

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View File

Before

Width:  |  Height:  |  Size: 118 KiB

After

Width:  |  Height:  |  Size: 118 KiB

View File

Before

Width:  |  Height:  |  Size: 114 KiB

After

Width:  |  Height:  |  Size: 114 KiB

View File

Before

Width:  |  Height:  |  Size: 229 KiB

After

Width:  |  Height:  |  Size: 229 KiB

View File

Before

Width:  |  Height:  |  Size: 267 KiB

After

Width:  |  Height:  |  Size: 267 KiB

View File

Before

Width:  |  Height:  |  Size: 171 KiB

After

Width:  |  Height:  |  Size: 171 KiB

View File

Before

Width:  |  Height:  |  Size: 236 KiB

After

Width:  |  Height:  |  Size: 236 KiB

View File

Before

Width:  |  Height:  |  Size: 45 KiB

After

Width:  |  Height:  |  Size: 45 KiB

View File

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 B

View File

@@ -0,0 +1,5 @@
%PDF-1.
1 0 obj<</Pages 2 0 R>>endobj
2 0 obj<</Kids[3 0 R]/Count 1>>endobj
3 0 obj<</Parent 2 0 R>>endobj
trailer <</Root 1 0 R>>

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 B

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B

View File

@@ -0,0 +1,5 @@
%PDF-1.
1 0 obj<</Pages 2 0 R>>endobj
2 0 obj<</Kids[3 0 R]/Count 1>>endobj
3 0 obj<</Parent 2 0 R>>endobj
trailer <</Root 1 0 R>>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 B

Binary file not shown.

View File

@@ -0,0 +1,5 @@
%PDF-1.
1 0 obj<</Pages 2 0 R>>endobj
2 0 obj<</Kids[3 0 R]/Count 1>>endobj
3 0 obj<</Parent 2 0 R>>endobj
trailer <</Root 1 0 R>>

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 B

Binary file not shown.