Fix tests, fix photo route

This commit is contained in:
viktorstrate
2020-12-06 15:24:15 +01:00
parent 98f13d76e6
commit 4b778fbdde
3 changed files with 21 additions and 14 deletions

View File

@@ -20,7 +20,7 @@ func RegisterPhotoRoutes(db *gorm.DB, router *mux.Router) {
mediaName := mux.Vars(r)["name"]
var mediaURL models.MediaURL
result := db.Model(&models.MediaURL{}).Select("media_urls.*").Where("media_url.media_name = ?", mediaName).Joins("JOIN media ON media_urls.media_id = media.id").Scan(&mediaURL)
result := db.Model(&models.MediaURL{}).Joins("Media").Select("media_urls.*").Where("media_urls.media_name = ?", mediaName).Scan(&mediaURL)
if err := result.Error; err != nil {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404"))
@@ -54,12 +54,12 @@ func RegisterPhotoRoutes(db *gorm.DB, router *mux.Router) {
if _, err := os.Stat(cachedPath); os.IsNotExist((err)) {
err := db.Transaction(func(tx *gorm.DB) error {
if _, err = scanner.ProcessMedia(tx, media); err != nil {
log.Printf("ERROR: processing image not found in cache: %s\n", err)
log.Printf("ERROR: processing image not found in cache (%s): %s\n", cachedPath, err)
return err
}
if _, err = os.Stat(cachedPath); err != nil {
log.Printf("ERROR: after reprocessing image not found in cache: %s\n", err)
log.Printf("ERROR: after reprocessing image not found in cache (%s): %s\n", cachedPath, err)
return err
}

View File

@@ -195,7 +195,7 @@ func processPhoto(tx *gorm.DB, imageData *EncodeMediaData, photoCachePath *strin
return didProcess, nil
}
func makeMediaCacheDir(photo *models.Media) (*string, error) {
func makeMediaCacheDir(media *models.Media) (*string, error) {
// Make root cache dir if not exists
if _, err := os.Stat(PhotoCache()); os.IsNotExist(err) {
@@ -205,7 +205,7 @@ func makeMediaCacheDir(photo *models.Media) (*string, error) {
}
// Make album cache dir if not exists
albumCachePath := path.Join(PhotoCache(), strconv.Itoa(int(photo.ID)))
albumCachePath := path.Join(PhotoCache(), strconv.Itoa(int(media.AlbumID)))
if _, err := os.Stat(albumCachePath); os.IsNotExist(err) {
if err := os.Mkdir(albumCachePath, os.ModePerm); err != nil {
return nil, errors.Wrap(err, "could not make album image cache directory")
@@ -213,7 +213,7 @@ func makeMediaCacheDir(photo *models.Media) (*string, error) {
}
// Make photo cache dir if not exists
photoCachePath := path.Join(albumCachePath, strconv.Itoa(int(photo.ID)))
photoCachePath := path.Join(albumCachePath, strconv.Itoa(int(media.ID)))
if _, err := os.Stat(photoCachePath); os.IsNotExist(err) {
if err := os.Mkdir(photoCachePath, os.ModePerm); err != nil {
return nil, errors.Wrap(err, "could not make photo image cache directory")

View File

@@ -6,11 +6,18 @@ import (
"github.com/viktorstrate/photoview/api/graphql/models"
)
func makeAlbumWithID(id int) *models.Album {
var album models.Album
album.ID = id
return &album
}
func TestScannerQueue_AddJob(t *testing.T) {
scannerJobs := []ScannerJob{
{album: &models.Album{AlbumID: 100}, cache: MakeAlbumCache()},
{album: &models.Album{AlbumID: 20}, cache: MakeAlbumCache()},
{album: makeAlbumWithID(100), cache: MakeAlbumCache()},
{album: makeAlbumWithID(20), cache: MakeAlbumCache()},
}
mockScannerQueue := ScannerQueue{
@@ -21,7 +28,7 @@ func TestScannerQueue_AddJob(t *testing.T) {
}
t.Run("add new job to scanner queue", func(t *testing.T) {
newJob := ScannerJob{album: &models.Album{AlbumID: 42}, cache: MakeAlbumCache()}
newJob := ScannerJob{album: makeAlbumWithID(42), cache: MakeAlbumCache()}
startingJobs := len(mockScannerQueue.up_next)
@@ -41,7 +48,7 @@ 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: &models.Album{AlbumID: 20}, cache: MakeAlbumCache()})
err := mockScannerQueue.addJob(&ScannerJob{album: makeAlbumWithID(20), cache: MakeAlbumCache()})
if err != nil {
t.Errorf(".AddJob() returned an unexpected error: %s", err)
}
@@ -57,8 +64,8 @@ func TestScannerQueue_AddJob(t *testing.T) {
func TestScannerQueue_JobOnQueue(t *testing.T) {
scannerJobs := []ScannerJob{
{album: &models.Album{AlbumID: 100}, cache: MakeAlbumCache()},
{album: &models.Album{AlbumID: 20}, cache: MakeAlbumCache()},
{album: makeAlbumWithID(100), cache: MakeAlbumCache()},
{album: makeAlbumWithID(20), cache: MakeAlbumCache()},
}
mockScannerQueue := ScannerQueue{
@@ -74,10 +81,10 @@ func TestScannerQueue_JobOnQueue(t *testing.T) {
ScannerJob
}{
{"album which owner is already on the queue", true, ScannerJob{
album: &models.Album{AlbumID: 100}, cache: MakeAlbumCache(),
album: makeAlbumWithID(100), cache: MakeAlbumCache(),
}},
{"album that is not on the queue", false, ScannerJob{
album: &models.Album{AlbumID: 321}, cache: MakeAlbumCache(),
album: makeAlbumWithID(321), cache: MakeAlbumCache(),
}},
}