From 447f05185fe762e16b489cc226cc6f64f92072f2 Mon Sep 17 00:00:00 2001
From: viktorstrate <viktorstrate@gmail.com>
Date: Mon, 28 Mar 2022 18:43:00 +0200
Subject: [PATCH] Fix scanner queue tests

---
 api/scanner/scanner_album.go            |  2 +-
 api/scanner/scanner_queue/queue.go      |  6 ++++++
 api/scanner/scanner_queue/queue_test.go | 28 +++++++++++++------------
 api/test_utils/scanner_helpers.go       | 14 ++++++-------
 4 files changed, 29 insertions(+), 21 deletions(-)

diff --git a/api/scanner/scanner_album.go b/api/scanner/scanner_album.go
index 97e126a..cfa05f8 100644
--- a/api/scanner/scanner_album.go
+++ b/api/scanner/scanner_album.go
@@ -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)
diff --git a/api/scanner/scanner_queue/queue.go b/api/scanner/scanner_queue/queue.go
index 7dea950..48f0880 100644
--- a/api/scanner/scanner_queue/queue.go
+++ b/api/scanner/scanner_queue/queue.go
@@ -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)
 }
diff --git a/api/scanner/scanner_queue/queue_test.go b/api/scanner/scanner_queue/queue_test.go
index f29ce57..0f01b29 100644
--- a/api/scanner/scanner_queue/queue_test.go
+++ b/api/scanner/scanner_queue/queue_test.go
@@ -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 {
diff --git a/api/test_utils/scanner_helpers.go b/api/test_utils/scanner_helpers.go
index b143fc6..ec3e719 100644
--- a/api/test_utils/scanner_helpers.go
+++ b/api/test_utils/scanner_helpers.go
@@ -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()
 }
