Fix scanner test. (#1241)

This commit is contained in:
Googol Lee
2025-07-09 22:25:44 +02:00
committed by GitHub
parent 3bc3e09467
commit 6aca4fe2f9
8 changed files with 79 additions and 64 deletions

View File

@@ -75,7 +75,9 @@ RUN export $(cat /env) \
&& cp -a pkgconfig/* ${PKG_CONFIG_PATH} \ && cp -a pkgconfig/* ${PKG_CONFIG_PATH} \
&& cp -a lib/* /usr/local/lib/ \ && cp -a lib/* /usr/local/lib/ \
&& ldconfig \ && ldconfig \
&& apt-get install -y ./deb/jellyfin-ffmpeg.deb && apt-get install -y ./deb/jellyfin-ffmpeg.deb \
&& ln -s /usr/lib/jellyfin-ffmpeg/ffmpeg /usr/local/bin/ \
&& ln -s /usr/lib/jellyfin-ffmpeg/ffprobe /usr/local/bin/
COPY api/go.mod api/go.sum /app/api/ COPY api/go.mod api/go.sum /app/api/
# Split values in `/env` # Split values in `/env`

View File

@@ -34,6 +34,8 @@ func SetPathWithCurrent(t *testing.T, paths ...string) {
func TestInitFfprobePath(t *testing.T) { func TestInitFfprobePath(t *testing.T) {
t.Run("PathFail", func(t *testing.T) { t.Run("PathFail", func(t *testing.T) {
SetPathWithCurrent(t, "non_exist_path")
err := SetFfprobePath() err := SetFfprobePath()
if err == nil { if err == nil {
t.Fatalf("InitFfprobePath() returns nil, want an error") t.Fatalf("InitFfprobePath() returns nil, want an error")

View File

@@ -8,7 +8,6 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/photoview/photoview/api/scanner/media_encoding/executable_worker"
"gopkg.in/gographics/imagick.v3/imagick" "gopkg.in/gographics/imagick.v3/imagick"
) )
@@ -44,21 +43,6 @@ func BenchmarkStdlib(b *testing.B) {
} }
} }
func BenchmarkMagickCLI(b *testing.B) {
dir := b.TempDir()
for b.Loop() {
func() {
output := filepath.Join(dir, "test.jpg")
defer os.Remove(output)
if err := executable_worker.Magick.EncodeJpeg("./test_media/real_media/png.png", output, 70); err != nil {
b.Fatal("encode jpeg error:", err)
}
}()
}
}
func BenchmarkMagickWand(b *testing.B) { func BenchmarkMagickWand(b *testing.B) {
dir := b.TempDir() dir := b.TempDir()

View File

@@ -36,38 +36,28 @@ func TestFullScan(t *testing.T) {
Path: "./test_media", Path: "./test_media",
} }
wantNoImages := []string{ wantWebPhotos := []string{
"avi.avi", "bmp.bmp",
"mkv.mkv",
"mp4.mp4",
"mpeg.mpg",
"ogg.ogg",
"quicktime.mov",
"webm.webm",
"wmv.wmv",
}
wantThumbnailsImages := []string{
"buttercup_close_summer_yellow.jpg",
"gif.gif", "gif.gif",
"lilac_lilac_bush_lilac.jpg", "jpeg.jpg",
"mount_merapi_volcano_indonesia.jpg", "png.png",
"webp.webp",
"jpg_with_file.jpg",
"recoverable_bad_rst_marker.jpg",
"standalone_jpg.jpg",
"boy1.jpg", "boy1.jpg",
"boy2.jpg", "boy2.jpg",
"buttercup_close_summer_yellow.jpg",
"girl_black_hair2.jpg", "girl_black_hair2.jpg",
"girl_blond1.jpg", "girl_blond1.jpg",
"girl_blond2.jpg", "girl_blond2.jpg",
"girl_blond3.jpg", "girl_blond3.jpg",
"lilac_lilac_bush_lilac.jpg",
"bmp.bmp", "mount_merapi_volcano_indonesia.jpg",
"jpeg.jpg",
"jpg_with_file.jpg",
"webp.webp",
"png.png",
"standalone_jpg.jpg",
"recoverable_bad_rst_marker.jpg",
} }
wantHighresImages := []string{ wantNonWebPhotos := []string{
"heif.heif", "heif.heif",
"jpg2000.jp2", "jpg2000.jp2",
"raw_with_file.tiff", "raw_with_file.tiff",
@@ -75,6 +65,18 @@ func TestFullScan(t *testing.T) {
"standalone_raw.tiff", "standalone_raw.tiff",
"tiff.tiff", "tiff.tiff",
} }
wantWebVideos := []string{
"mp4.mp4",
"mpeg.mpg",
"ogg.ogg",
"webm.webm",
}
wantNonWebVideos := []string{
"avi.avi",
"mkv.mkv",
"quicktime.mov",
"wmv.wmv",
}
wantFaceGroups := [][]string{ wantFaceGroups := [][]string{
{"boy1.jpg", "boy2.jpg"}, {"boy1.jpg", "boy2.jpg"},
@@ -109,15 +111,19 @@ func TestFullScan(t *testing.T) {
t.Fatal("get all media error:", err) t.Fatal("get all media error:", err)
} }
var want []string want := []string{}
want = append(want, wantNoImages...) want = append(want, wantWebPhotos...)
want = append(want, wantThumbnailsImages...) want = append(want, wantNonWebPhotos...)
want = append(want, wantHighresImages...) want = append(want, wantWebVideos...)
want = append(want, wantNonWebVideos...)
slices.Sort(want) slices.Sort(want)
got := make([]string, len(allMedia)) got := make([]string, len(allMedia))
for i, media := range allMedia { for i, media := range allMedia {
got[i] = media.Title got[i] = media.Title
if media.Blurhash == nil {
t.Errorf("media %q(%s) doesn't have Blurhash, while it should have", media.Title, media.Type)
}
} }
slices.Sort(got) slices.Sort(got)
@@ -133,28 +139,27 @@ func TestFullScan(t *testing.T) {
} }
var want []string var want []string
want = append(want, wantWebPhotos...)
wantThumbs := slices.Clone(wantThumbnailsImages) for _, name := range wantWebPhotos {
want = append(want, wantThumbnailsImages...) want = append(want, "thumbnail_"+strings.ReplaceAll(name, ".", "_")+".jpg")
for _, file := range copyFilelistWithJpgExt(wantThumbs) {
want = append(want, "thumbnail_"+file)
} }
wantHighres := slices.Clone(wantHighresImages) want = append(want, wantNonWebPhotos...)
want = append(want, wantHighresImages...) for _, name := range wantNonWebPhotos {
for _, file := range copyFilelistWithJpgExt(wantHighres) { want = append(want, "thumbnail_"+strings.ReplaceAll(name, ".", "_")+".jpg")
want = append(want, "highres_"+file) want = append(want, "highres_"+strings.ReplaceAll(name, ".", "_")+".jpg")
want = append(want, "thumbnail_"+file)
} }
wantSet := make(map[string]struct{}) want = append(want, wantWebVideos...)
for _, item := range want { for _, name := range wantWebVideos {
wantSet[item] = struct{}{} want = append(want, "video_thumb_"+strings.ReplaceAll(name, ".", "_")+".jpg")
} }
want = make([]string, 0, len(wantSet))
for key := range wantSet { for _, name := range wantNonWebVideos {
want = append(want, key) want = append(want, "video_thumb_"+strings.ReplaceAll(name, ".", "_")+".jpg")
want = append(want, "web_video_"+strings.ReplaceAll(name, ".", "_")+".mp4")
} }
slices.Sort(want) slices.Sort(want)
if got, want := len(allMediaURL), len(want); got != want { if got, want := len(allMediaURL), len(want); got != want {
@@ -168,7 +173,7 @@ func TestFullScan(t *testing.T) {
slices.Sort(got) slices.Sort(got)
if diff := cmp.Diff(got, want, cmp.Comparer(equalNameWithoutSuffix)); diff != "" { if diff := cmp.Diff(got, want, cmp.Comparer(equalNameWithoutSuffix)); diff != "" {
t.Errorf("all media diff (-got, +want):\n%s", diff) t.Errorf("all media url diff (-got, +want):\n%s", diff)
} }
}) })
@@ -218,8 +223,8 @@ func equalNameWithoutSuffix(a, b string) bool {
return false return false
} }
// a is not part of b and b is not part of a // a is not prefix of b and b is not prefix of a
if strings.Index(mainA, mainB) < 0 && strings.Index(mainB, mainA) < 0 { if strings.HasPrefix(mainA, mainB) && strings.HasPrefix(mainB, mainA) {
return false return false
} }

View File

@@ -2,14 +2,22 @@ package scanner_utils
import ( import (
"testing" "testing"
"time"
"github.com/photoview/photoview/api/graphql/models" "github.com/photoview/photoview/api/graphql/models"
"github.com/photoview/photoview/api/scanner"
"github.com/photoview/photoview/api/scanner/scanner_queue" "github.com/photoview/photoview/api/scanner/scanner_queue"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gorm.io/gorm" "gorm.io/gorm"
) )
func RunScannerOnUser(t *testing.T, db *gorm.DB, user *models.User) { func RunScannerOnUser(t *testing.T, db *gorm.DB, user *models.User) {
start := time.Now()
defer func() {
dur := time.Now().Sub(start)
t.Logf("RunScannerOnUser(user(id:%d)) took %s.", user.ID, dur)
}()
if !assert.NoError(t, scanner_queue.InitializeScannerQueue(db)) { if !assert.NoError(t, scanner_queue.InitializeScannerQueue(db)) {
return return
} }
@@ -20,9 +28,19 @@ func RunScannerOnUser(t *testing.T, db *gorm.DB, user *models.User) {
// wait for all jobs to finish // wait for all jobs to finish
scanner_queue.CloseScannerQueue() scanner_queue.CloseScannerQueue()
if err := scanner.GenerateBlurhashes(db); err != nil {
t.Fatalf("generate blurhashes error: %v", err)
}
} }
func RunScannerAll(t *testing.T, db *gorm.DB) { func RunScannerAll(t *testing.T, db *gorm.DB) {
start := time.Now()
defer func() {
dur := time.Now().Sub(start)
t.Logf("RunScannerAll() took %s.", dur)
}()
if !assert.NoError(t, scanner_queue.InitializeScannerQueue(db)) { if !assert.NoError(t, scanner_queue.InitializeScannerQueue(db)) {
return return
} }
@@ -33,4 +51,8 @@ func RunScannerAll(t *testing.T, db *gorm.DB) {
// wait for all jobs to finish // wait for all jobs to finish
scanner_queue.CloseScannerQueue() scanner_queue.CloseScannerQueue()
if err := scanner.GenerateBlurhashes(db); err != nil {
t.Fatalf("generate blurhashes error: %v", err)
}
} }