Fix scanner queue tests
This commit is contained in:
parent
b09d32019e
commit
447f05185f
|
@ -96,7 +96,7 @@ func ScanAlbum(ctx scanner_task.TaskContext) error {
|
|||
// Scan for photos
|
||||
albumMedia, err := findMediaForAlbum(ctx)
|
||||
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)
|
||||
|
|
|
@ -25,6 +25,12 @@ type ScannerJob struct {
|
|||
// cache *scanner_cache.AlbumScannerCache
|
||||
}
|
||||
|
||||
func NewScannerJob(ctx scanner_task.TaskContext) ScannerJob {
|
||||
return ScannerJob{
|
||||
ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func (job *ScannerJob) Run(db *gorm.DB) {
|
||||
scanner.ScanAlbum(job.ctx)
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package scanner_queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/photoview/photoview/api/graphql/models"
|
||||
"github.com/photoview/photoview/api/scanner/scanner_cache"
|
||||
"github.com/photoview/photoview/api/scanner/scanner_task"
|
||||
)
|
||||
|
||||
func makeAlbumWithID(id int) *models.Album {
|
||||
|
@ -14,11 +16,15 @@ func makeAlbumWithID(id int) *models.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) {
|
||||
|
||||
scannerJobs := []ScannerJob{
|
||||
{album: makeAlbumWithID(100), cache: scanner_cache.MakeAlbumCache()},
|
||||
{album: makeAlbumWithID(20), cache: scanner_cache.MakeAlbumCache()},
|
||||
makeScannerJob(100),
|
||||
makeScannerJob(20),
|
||||
}
|
||||
|
||||
mockScannerQueue := ScannerQueue{
|
||||
|
@ -29,7 +35,7 @@ func TestScannerQueue_AddJob(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)
|
||||
|
||||
|
@ -49,7 +55,8 @@ func TestScannerQueue_AddJob(t *testing.T) {
|
|||
t.Run("add existing job to scanner queue", func(t *testing.T) {
|
||||
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 {
|
||||
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) {
|
||||
|
||||
scannerJobs := []ScannerJob{
|
||||
{album: makeAlbumWithID(100), cache: scanner_cache.MakeAlbumCache()},
|
||||
{album: makeAlbumWithID(20), cache: scanner_cache.MakeAlbumCache()},
|
||||
makeScannerJob(100),
|
||||
makeScannerJob(20),
|
||||
}
|
||||
|
||||
mockScannerQueue := ScannerQueue{
|
||||
|
@ -81,12 +87,8 @@ func TestScannerQueue_JobOnQueue(t *testing.T) {
|
|||
bool
|
||||
ScannerJob
|
||||
}{
|
||||
{"album which owner is already on the queue", true, ScannerJob{
|
||||
album: makeAlbumWithID(100), cache: scanner_cache.MakeAlbumCache(),
|
||||
}},
|
||||
{"album that is not on the queue", false, ScannerJob{
|
||||
album: makeAlbumWithID(321), cache: scanner_cache.MakeAlbumCache(),
|
||||
}},
|
||||
{"album which owner is already on the queue", true, makeScannerJob(100)},
|
||||
{"album that is not on the queue", false, makeScannerJob(321)},
|
||||
}
|
||||
|
||||
for _, test := range onQueueTests {
|
||||
|
|
|
@ -4,33 +4,33 @@ import (
|
|||
"testing"
|
||||
|
||||
"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"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if !assert.NoError(t, scanner.AddUserToQueue(user)) {
|
||||
if !assert.NoError(t, scanner_queue.AddUserToQueue(user)) {
|
||||
return
|
||||
}
|
||||
|
||||
// wait for all jobs to finish
|
||||
scanner.CloseScannerQueue()
|
||||
scanner_queue.CloseScannerQueue()
|
||||
}
|
||||
|
||||
func RunScannerAll(t *testing.T, db *gorm.DB) {
|
||||
if !assert.NoError(t, scanner.InitializeScannerQueue(db)) {
|
||||
if !assert.NoError(t, scanner_queue.InitializeScannerQueue(db)) {
|
||||
return
|
||||
}
|
||||
|
||||
if !assert.NoError(t, scanner.AddAllToQueue()) {
|
||||
if !assert.NoError(t, scanner_queue.AddAllToQueue()) {
|
||||
return
|
||||
}
|
||||
|
||||
// wait for all jobs to finish
|
||||
scanner.CloseScannerQueue()
|
||||
scanner_queue.CloseScannerQueue()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue