2020-06-22 21:52:53 +02:00
|
|
|
package scanner
|
|
|
|
|
2020-06-23 14:10:03 +02:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-12-17 22:51:43 +01:00
|
|
|
"github.com/photoview/photoview/api/graphql/models"
|
2020-06-23 14:10:03 +02:00
|
|
|
)
|
|
|
|
|
2020-12-06 15:24:15 +01:00
|
|
|
func makeAlbumWithID(id int) *models.Album {
|
|
|
|
var album models.Album
|
|
|
|
album.ID = id
|
|
|
|
|
|
|
|
return &album
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:10:03 +02:00
|
|
|
func TestScannerQueue_AddJob(t *testing.T) {
|
|
|
|
|
|
|
|
scannerJobs := []ScannerJob{
|
2020-12-06 15:24:15 +01:00
|
|
|
{album: makeAlbumWithID(100), cache: MakeAlbumCache()},
|
|
|
|
{album: makeAlbumWithID(20), cache: MakeAlbumCache()},
|
2020-06-23 14:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mockScannerQueue := ScannerQueue{
|
|
|
|
idle_chan: make(chan bool, 1),
|
|
|
|
in_progress: make([]ScannerJob, 0),
|
|
|
|
up_next: scannerJobs,
|
|
|
|
db: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("add new job to scanner queue", func(t *testing.T) {
|
2020-12-06 15:24:15 +01:00
|
|
|
newJob := ScannerJob{album: makeAlbumWithID(42), cache: MakeAlbumCache()}
|
2020-06-23 14:10:03 +02:00
|
|
|
|
|
|
|
startingJobs := len(mockScannerQueue.up_next)
|
|
|
|
|
|
|
|
err := mockScannerQueue.addJob(&newJob)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf(".AddJob() returned an unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mockScannerQueue.up_next) != startingJobs+1 {
|
|
|
|
t.Errorf("Expected scanner queue length to be %d but got %d", startingJobs+1, len(mockScannerQueue.up_next))
|
|
|
|
} else if mockScannerQueue.up_next[len(mockScannerQueue.up_next)-1] != newJob {
|
|
|
|
t.Errorf("Expected scanner queue to contain the job that was added: %+v", newJob)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("add existing job to scanner queue", func(t *testing.T) {
|
|
|
|
startingJobs := len(mockScannerQueue.up_next)
|
|
|
|
|
2020-12-06 15:24:15 +01:00
|
|
|
err := mockScannerQueue.addJob(&ScannerJob{album: makeAlbumWithID(20), cache: MakeAlbumCache()})
|
2020-06-23 14:10:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf(".AddJob() returned an unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mockScannerQueue.up_next) != startingJobs {
|
|
|
|
t.Errorf("Expected scanner queue length not to change: start length %d, new length %d", startingJobs, len(mockScannerQueue.up_next))
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScannerQueue_JobOnQueue(t *testing.T) {
|
|
|
|
|
|
|
|
scannerJobs := []ScannerJob{
|
2020-12-06 15:24:15 +01:00
|
|
|
{album: makeAlbumWithID(100), cache: MakeAlbumCache()},
|
|
|
|
{album: makeAlbumWithID(20), cache: MakeAlbumCache()},
|
2020-06-23 14:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mockScannerQueue := ScannerQueue{
|
|
|
|
idle_chan: make(chan bool, 1),
|
|
|
|
in_progress: make([]ScannerJob, 0),
|
|
|
|
up_next: scannerJobs,
|
|
|
|
db: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
onQueueTests := []struct {
|
|
|
|
string
|
|
|
|
bool
|
|
|
|
ScannerJob
|
|
|
|
}{
|
|
|
|
{"album which owner is already on the queue", true, ScannerJob{
|
2020-12-06 15:24:15 +01:00
|
|
|
album: makeAlbumWithID(100), cache: MakeAlbumCache(),
|
2020-06-23 14:10:03 +02:00
|
|
|
}},
|
|
|
|
{"album that is not on the queue", false, ScannerJob{
|
2020-12-06 15:24:15 +01:00
|
|
|
album: makeAlbumWithID(321), cache: MakeAlbumCache(),
|
2020-06-23 14:10:03 +02:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range onQueueTests {
|
|
|
|
t.Run(test.string, func(t *testing.T) {
|
|
|
|
onQueue, err := mockScannerQueue.jobOnQueue(&test.ScannerJob)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Expected jobOnQueue not to return an error")
|
|
|
|
} else if onQueue != test.bool {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|