Fix queue tests

This commit is contained in:
viktorstrate
2020-06-23 14:10:03 +02:00
parent 95f4df4b7d
commit cc1eadaef4

View File

@@ -1,101 +1,95 @@
package scanner
// func TestScannerQueue_AddJob(t *testing.T) {
import (
"testing"
// scannerJobs := []ScannerJob{
// {scope: JOB_SCAN_ALBUM, model: models.Album{AlbumID: 100, OwnerID: 123}},
// {scope: JOB_SCAN_USER, model: models.User{UserID: 20}},
// }
"github.com/viktorstrate/photoview/api/graphql/models"
)
// mockScannerQueue := ScannerQueue{
// idle_chan: make(chan bool, 1),
// in_progress: make([]ScannerJob, 0),
// up_next: scannerJobs,
// db: nil,
// }
func TestScannerQueue_AddJob(t *testing.T) {
// t.Run("add new job to scanner queue", func(t *testing.T) {
// newJob := ScannerJob{
// scope: JOB_SCAN_USER,
// model: models.User{UserID: 253},
// }
scannerJobs := []ScannerJob{
{album: &models.Album{AlbumID: 100}, cache: MakeAlbumCache()},
{album: &models.Album{AlbumID: 20}, cache: MakeAlbumCache()},
}
// startingJobs := len(mockScannerQueue.up_next)
mockScannerQueue := ScannerQueue{
idle_chan: make(chan bool, 1),
in_progress: make([]ScannerJob, 0),
up_next: scannerJobs,
db: nil,
}
// err := mockScannerQueue.AddJob(&newJob)
// if err != nil {
// t.Errorf(".AddJob() returned an unexpected error: %s", err)
// }
t.Run("add new job to scanner queue", func(t *testing.T) {
newJob := ScannerJob{album: &models.Album{AlbumID: 42}, cache: MakeAlbumCache()}
// 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)
// }
startingJobs := len(mockScannerQueue.up_next)
// })
err := mockScannerQueue.addJob(&newJob)
if err != nil {
t.Errorf(".AddJob() returned an unexpected error: %s", err)
}
// t.Run("add existing job to scanner queue", func(t *testing.T) {
// startingJobs := len(mockScannerQueue.up_next)
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)
}
// err := mockScannerQueue.AddJob(&ScannerJob{
// scope: JOB_SCAN_USER,
// model: models.User{UserID: 20},
// })
// 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))
// }
t.Run("add existing job to scanner queue", func(t *testing.T) {
startingJobs := len(mockScannerQueue.up_next)
// })
err := mockScannerQueue.addJob(&ScannerJob{album: &models.Album{AlbumID: 20}, cache: MakeAlbumCache()})
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{
// {scope: JOB_SCAN_ALBUM, model: models.Album{AlbumID: 100, OwnerID: 123}},
// {scope: JOB_SCAN_USER, model: models.User{UserID: 20}},
// }
}
// mockScannerQueue := ScannerQueue{
// idle_chan: make(chan bool, 1),
// in_progress: make([]ScannerJob, 0),
// up_next: scannerJobs,
// db: nil,
// }
func TestScannerQueue_JobOnQueue(t *testing.T) {
// onQueueTests := []struct {
// string
// bool
// ScannerJob
// }{
// {"user that is already on the queue", true, ScannerJob{
// scope: JOB_SCAN_USER,
// model: models.User{UserID: 20},
// }},
// {"album which owner is already on the queue", true, ScannerJob{
// scope: JOB_SCAN_ALBUM,
// model: models.Album{AlbumID: 40, OwnerID: 20},
// }},
// {"album that is not on the queue", false, ScannerJob{
// scope: JOB_SCAN_ALBUM,
// model: models.Album{AlbumID: 321, OwnerID: 11},
// }},
// }
scannerJobs := []ScannerJob{
{album: &models.Album{AlbumID: 100}, cache: MakeAlbumCache()},
{album: &models.Album{AlbumID: 20}, cache: MakeAlbumCache()},
}
// 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()
// }
// })
// }
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{
album: &models.Album{AlbumID: 100}, cache: MakeAlbumCache(),
}},
{"album that is not on the queue", false, ScannerJob{
album: &models.Album{AlbumID: 321}, cache: MakeAlbumCache(),
}},
}
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()
}
})
}
}