From 253f126aaf2f7e206f33f037b2c0229194acc0c1 Mon Sep 17 00:00:00 2001 From: viktorstrate Date: Mon, 26 Apr 2021 15:07:06 +0200 Subject: [PATCH] Add scanner_album tests --- api/scanner/face_detection/face_detector.go | 2 - api/scanner/scanner_album.go | 9 +++++ api/scanner/scanner_album_test.go | 42 +++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 api/scanner/scanner_album_test.go diff --git a/api/scanner/face_detection/face_detector.go b/api/scanner/face_detection/face_detector.go index 32c133c1..3cecbe7c 100644 --- a/api/scanner/face_detection/face_detector.go +++ b/api/scanner/face_detection/face_detector.go @@ -107,8 +107,6 @@ func (fd *FaceDetector) DetectFaces(db *gorm.DB, media *models.Media) error { return err } - log.Printf("Face thumb path: %v %v\n", thumbnailPath, fd) - fd.mutex.Lock() faces, err := fd.rec.RecognizeFile(thumbnailPath) fd.mutex.Unlock() diff --git a/api/scanner/scanner_album.go b/api/scanner/scanner_album.go index fdfb450a..d998c981 100644 --- a/api/scanner/scanner_album.go +++ b/api/scanner/scanner_album.go @@ -30,6 +30,15 @@ func NewRootAlbum(db *gorm.DB, rootPath string, owner *models.User) (*models.Alb if len(matchedAlbums) > 0 { album := matchedAlbums[0] + var matchedUserAlbumCount int64 + if err := db.Table("user_albums").Where("user_id = ?", owner.ID).Where("album_id = ?", album.ID).Count(&matchedUserAlbumCount).Error; err != nil { + return nil, err + } + + if matchedUserAlbumCount > 0 { + return nil, errors.New(fmt.Sprintf("user already owns path (%s)", rootPath)) + } + if err := db.Model(&owner).Association("Albums").Append(&album); err != nil { return nil, errors.Wrap(err, "failed to add owner to already existing album") } diff --git a/api/scanner/scanner_album_test.go b/api/scanner/scanner_album_test.go new file mode 100644 index 00000000..f234354c --- /dev/null +++ b/api/scanner/scanner_album_test.go @@ -0,0 +1,42 @@ +package scanner_test + +import ( + "testing" + + "github.com/photoview/photoview/api/graphql/models" + "github.com/photoview/photoview/api/scanner" + "github.com/photoview/photoview/api/test_utils" + "github.com/stretchr/testify/assert" +) + +func TestNewRootPath(t *testing.T) { + db := test_utils.DatabaseTest(t) + + user := models.User{ + Username: "user1", + } + + if !assert.NoError(t, db.Save(&user).Error) { + return + } + + t.Run("Insert valid root album", func(t *testing.T) { + album, err := scanner.NewRootAlbum(db, "./test_data", &user) + if !assert.NoError(t, err) { + return + } + + assert.NotNil(t, album) + assert.Equal(t, "./test_data", album.Path) + assert.NotEmpty(t, album.Owners) + }) + + t.Run("Insert duplicate root album", func(t *testing.T) { + + _, err := scanner.NewRootAlbum(db, "./test_data", &user) + + assert.Error(t, err) + assert.Equal(t, err.Error(), "user already owns path (./test_data)") + }) + +}