1
Fork 0

Fix scanner queue tests

This commit is contained in:
viktorstrate 2022-03-28 18:43:00 +02:00
parent b09d32019e
commit 447f05185f
No known key found for this signature in database
GPG Key ID: 3F855605109C1E8A
4 changed files with 29 additions and 21 deletions

View File

@ -96,7 +96,7 @@ func ScanAlbum(ctx scanner_task.TaskContext) error {
// Scan for photos // Scan for photos
albumMedia, err := findMediaForAlbum(ctx) albumMedia, err := findMediaForAlbum(ctx)
if err != nil { if err != nil {
return errors.Wrapf(err, "find media for album (%s): %s", ctx.GetAlbum().Path) return errors.Wrapf(err, "find media for album (%s): %s", ctx.GetAlbum().Path, err)
} }
changedMedia := make([]*models.Media, 0) changedMedia := make([]*models.Media, 0)

View File

@ -25,6 +25,12 @@ type ScannerJob struct {
// cache *scanner_cache.AlbumScannerCache // cache *scanner_cache.AlbumScannerCache
} }
func NewScannerJob(ctx scanner_task.TaskContext) ScannerJob {
return ScannerJob{
ctx,
}
}
func (job *ScannerJob) Run(db *gorm.DB) { func (job *ScannerJob) Run(db *gorm.DB) {
scanner.ScanAlbum(job.ctx) scanner.ScanAlbum(job.ctx)
} }

View File

@ -1,10 +1,12 @@
package scanner_queue package scanner_queue
import ( import (
"context"
"testing" "testing"
"github.com/photoview/photoview/api/graphql/models" "github.com/photoview/photoview/api/graphql/models"
"github.com/photoview/photoview/api/scanner/scanner_cache" "github.com/photoview/photoview/api/scanner/scanner_cache"
"github.com/photoview/photoview/api/scanner/scanner_task"
) )
func makeAlbumWithID(id int) *models.Album { func makeAlbumWithID(id int) *models.Album {
@ -14,11 +16,15 @@ func makeAlbumWithID(id int) *models.Album {
return &album return &album
} }
func makeScannerJob(albumID int) ScannerJob {
return NewScannerJob(scanner_task.NewTaskContext(context.Background(), nil, makeAlbumWithID(albumID), scanner_cache.MakeAlbumCache()))
}
func TestScannerQueue_AddJob(t *testing.T) { func TestScannerQueue_AddJob(t *testing.T) {
scannerJobs := []ScannerJob{ scannerJobs := []ScannerJob{
{album: makeAlbumWithID(100), cache: scanner_cache.MakeAlbumCache()}, makeScannerJob(100),
{album: makeAlbumWithID(20), cache: scanner_cache.MakeAlbumCache()}, makeScannerJob(20),
} }
mockScannerQueue := ScannerQueue{ mockScannerQueue := ScannerQueue{
@ -29,7 +35,7 @@ func TestScannerQueue_AddJob(t *testing.T) {
} }
t.Run("add new job to scanner queue", func(t *testing.T) { t.Run("add new job to scanner queue", func(t *testing.T) {
newJob := ScannerJob{album: makeAlbumWithID(42), cache: scanner_cache.MakeAlbumCache()} newJob := makeScannerJob(42)
startingJobs := len(mockScannerQueue.up_next) startingJobs := len(mockScannerQueue.up_next)
@ -49,7 +55,8 @@ func TestScannerQueue_AddJob(t *testing.T) {
t.Run("add existing job to scanner queue", func(t *testing.T) { t.Run("add existing job to scanner queue", func(t *testing.T) {
startingJobs := len(mockScannerQueue.up_next) startingJobs := len(mockScannerQueue.up_next)
err := mockScannerQueue.addJob(&ScannerJob{album: makeAlbumWithID(20), cache: scanner_cache.MakeAlbumCache()}) job := makeScannerJob(20)
err := mockScannerQueue.addJob(&job)
if err != nil { if err != nil {
t.Errorf(".AddJob() returned an unexpected error: %s", err) t.Errorf(".AddJob() returned an unexpected error: %s", err)
} }
@ -59,14 +66,13 @@ func TestScannerQueue_AddJob(t *testing.T) {
} }
}) })
} }
func TestScannerQueue_JobOnQueue(t *testing.T) { func TestScannerQueue_JobOnQueue(t *testing.T) {
scannerJobs := []ScannerJob{ scannerJobs := []ScannerJob{
{album: makeAlbumWithID(100), cache: scanner_cache.MakeAlbumCache()}, makeScannerJob(100),
{album: makeAlbumWithID(20), cache: scanner_cache.MakeAlbumCache()}, makeScannerJob(20),
} }
mockScannerQueue := ScannerQueue{ mockScannerQueue := ScannerQueue{
@ -81,12 +87,8 @@ func TestScannerQueue_JobOnQueue(t *testing.T) {
bool bool
ScannerJob ScannerJob
}{ }{
{"album which owner is already on the queue", true, ScannerJob{ {"album which owner is already on the queue", true, makeScannerJob(100)},
album: makeAlbumWithID(100), cache: scanner_cache.MakeAlbumCache(), {"album that is not on the queue", false, makeScannerJob(321)},
}},
{"album that is not on the queue", false, ScannerJob{
album: makeAlbumWithID(321), cache: scanner_cache.MakeAlbumCache(),
}},
} }
for _, test := range onQueueTests { for _, test := range onQueueTests {

View File

@ -4,33 +4,33 @@ import (
"testing" "testing"
"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/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) {
if !assert.NoError(t, scanner.InitializeScannerQueue(db)) { if !assert.NoError(t, scanner_queue.InitializeScannerQueue(db)) {
return return
} }
if !assert.NoError(t, scanner.AddUserToQueue(user)) { if !assert.NoError(t, scanner_queue.AddUserToQueue(user)) {
return return
} }
// wait for all jobs to finish // wait for all jobs to finish
scanner.CloseScannerQueue() scanner_queue.CloseScannerQueue()
} }
func RunScannerAll(t *testing.T, db *gorm.DB) { func RunScannerAll(t *testing.T, db *gorm.DB) {
if !assert.NoError(t, scanner.InitializeScannerQueue(db)) { if !assert.NoError(t, scanner_queue.InitializeScannerQueue(db)) {
return return
} }
if !assert.NoError(t, scanner.AddAllToQueue()) { if !assert.NoError(t, scanner_queue.AddAllToQueue()) {
return return
} }
// wait for all jobs to finish // wait for all jobs to finish
scanner.CloseScannerQueue() scanner_queue.CloseScannerQueue()
} }