2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2019-05-16 04:03:55 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-05-31 15:40:52 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/face"
|
|
|
|
|
2020-05-19 11:00:17 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2019-05-16 04:03:55 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-05-07 18:08:12 +02:00
|
|
|
func TestFirstFileByHash(t *testing.T) {
|
|
|
|
t.Run("not existing file", func(t *testing.T) {
|
|
|
|
f, err := FirstFileByHash("xxx")
|
2020-11-21 15:43:13 +01:00
|
|
|
|
|
|
|
assert.EqualError(t, err, "record not found")
|
2020-05-07 18:08:12 +02:00
|
|
|
assert.Equal(t, uint(0), f.ID)
|
|
|
|
})
|
|
|
|
t.Run("existing file", func(t *testing.T) {
|
|
|
|
f, err := FirstFileByHash("2cad9168fa6acc5c5c2965ddf6ec465ca42fd818")
|
|
|
|
if err != nil {
|
2020-05-08 19:34:29 +02:00
|
|
|
t.Fatal(err)
|
2020-05-07 18:08:12 +02:00
|
|
|
}
|
|
|
|
assert.Equal(t, uint(0xf4240), f.ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-09 11:27:44 +02:00
|
|
|
func TestFile_ShareFileName(t *testing.T) {
|
2019-12-17 18:26:13 +01:00
|
|
|
t.Run("photo with title", func(t *testing.T) {
|
2020-04-20 20:07:58 +02:00
|
|
|
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: "Berlin / Morning Mood"}
|
2020-05-25 19:10:44 +02:00
|
|
|
file := &File{Photo: photo, FileType: "jpg", FileUID: "foobar345678765", FileHash: "e98eb86480a72bd585d228a709f0622f90e86cbc"}
|
2019-05-16 04:03:55 +02:00
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
filename := file.ShareBase(0)
|
2019-05-16 04:03:55 +02:00
|
|
|
|
2020-04-20 20:07:58 +02:00
|
|
|
assert.Contains(t, filename, "20190115-000000-Berlin-Morning-Mood")
|
2020-05-19 11:00:17 +02:00
|
|
|
assert.Contains(t, filename, fs.JpegExt)
|
2019-12-17 18:26:13 +01:00
|
|
|
})
|
|
|
|
t.Run("photo without title", func(t *testing.T) {
|
2020-04-20 20:07:58 +02:00
|
|
|
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: ""}
|
2020-05-25 19:10:44 +02:00
|
|
|
file := &File{Photo: photo, FileType: "jpg", PhotoUID: "123", FileUID: "foobar345678765", FileHash: "e98eb86480a72bd585d228a709f0622f90e86cbc"}
|
2019-12-17 18:26:13 +01:00
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
filename := file.ShareBase(0)
|
2019-12-17 18:26:13 +01:00
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
assert.Equal(t, filename, "e98eb86480a72bd585d228a709f0622f90e86cbc.jpg")
|
2019-12-17 18:26:13 +01:00
|
|
|
})
|
|
|
|
t.Run("photo without photo", func(t *testing.T) {
|
2020-05-25 19:10:44 +02:00
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileUID: "foobar345678765", FileHash: "e98eb86480a72bd585d228a709f0622f90e86cbc"}
|
2019-12-17 18:26:13 +01:00
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
filename := file.ShareBase(0)
|
2019-12-17 18:26:13 +01:00
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
assert.Equal(t, "e98eb86480a72bd585d228a709f0622f90e86cbc.jpg", filename)
|
2019-12-17 18:26:13 +01:00
|
|
|
})
|
2021-08-26 17:26:48 +02:00
|
|
|
t.Run("file without photo", func(t *testing.T) {
|
|
|
|
file := FileFixtures.Get("FileWithoutPhoto.mp4")
|
|
|
|
|
|
|
|
filename := file.ShareBase(0)
|
|
|
|
|
|
|
|
assert.Equal(t, "pcad9a68fa6acc5c5ba965adf6ec465ca42fd916.mp4", filename)
|
|
|
|
})
|
2020-07-09 11:27:44 +02:00
|
|
|
t.Run("file hash < 8", func(t *testing.T) {
|
|
|
|
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: "Berlin / Morning Mood"}
|
|
|
|
|
|
|
|
file := &File{Photo: photo, FileType: "jpg", FileUID: "foobar345678765", FileHash: "e98"}
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
filename := file.ShareBase(0)
|
2020-07-09 11:27:44 +02:00
|
|
|
|
|
|
|
assert.NotContains(t, filename, "20190115-000000-Berlin-Morning-Mood")
|
|
|
|
})
|
|
|
|
t.Run("no file uid", func(t *testing.T) {
|
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileHash: "e98ijhyt"}
|
|
|
|
|
2021-01-27 21:30:10 +01:00
|
|
|
filename := file.ShareBase(0)
|
2020-07-09 11:27:44 +02:00
|
|
|
|
|
|
|
assert.Equal(t, filename, "e98ijhyt.jpg")
|
|
|
|
})
|
2019-05-16 04:03:55 +02:00
|
|
|
}
|
2020-05-07 18:08:12 +02:00
|
|
|
|
|
|
|
func TestFile_Changed(t *testing.T) {
|
2020-05-14 15:28:11 +02:00
|
|
|
var deletedAt = time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)
|
2020-05-07 18:08:12 +02:00
|
|
|
t.Run("different modified times", func(t *testing.T) {
|
2020-07-17 16:09:55 +02:00
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSize: 500, ModTime: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC).Unix()}
|
2020-11-21 15:43:13 +01:00
|
|
|
d := time.Date(2020, 01, 15, 0, 0, 0, 0, time.UTC)
|
|
|
|
assert.Equal(t, true, file.Changed(500, d))
|
2020-05-07 18:08:12 +02:00
|
|
|
})
|
|
|
|
t.Run("different sizes", func(t *testing.T) {
|
2020-07-17 16:09:55 +02:00
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSize: 600, ModTime: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC).Unix()}
|
2020-11-21 15:43:13 +01:00
|
|
|
d := time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)
|
|
|
|
assert.Equal(t, true, file.Changed(500, d))
|
2020-05-07 18:08:12 +02:00
|
|
|
})
|
|
|
|
t.Run("no change", func(t *testing.T) {
|
2020-07-17 16:09:55 +02:00
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSize: 500, ModTime: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC).Unix()}
|
2020-11-21 15:43:13 +01:00
|
|
|
d := time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)
|
|
|
|
assert.Equal(t, false, file.Changed(500, d))
|
2020-05-07 18:08:12 +02:00
|
|
|
})
|
2020-05-14 15:28:11 +02:00
|
|
|
t.Run("deleted", func(t *testing.T) {
|
2020-07-17 16:09:55 +02:00
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSize: 500, ModTime: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC).Unix(), DeletedAt: &deletedAt}
|
2020-11-21 15:43:13 +01:00
|
|
|
d := time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)
|
|
|
|
assert.Equal(t, false, file.Changed(500, d))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFile_Missing(t *testing.T) {
|
|
|
|
var deletedAt = time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
t.Run("deleted", func(t *testing.T) {
|
|
|
|
file := &File{FileMissing: false, Photo: nil, FileType: "jpg", FileSize: 500, ModTime: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC).Unix(), DeletedAt: &deletedAt}
|
|
|
|
assert.Equal(t, true, file.Missing())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("missing", func(t *testing.T) {
|
|
|
|
file := &File{FileMissing: true, Photo: nil, FileType: "jpg", FileSize: 500, ModTime: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC).Unix(), DeletedAt: nil}
|
|
|
|
assert.Equal(t, true, file.Missing())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("not_missing", func(t *testing.T) {
|
|
|
|
file := &File{FileMissing: false, Photo: nil, FileType: "jpg", FileSize: 500, ModTime: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC).Unix(), DeletedAt: nil}
|
|
|
|
assert.Equal(t, false, file.Missing())
|
2020-05-14 15:28:11 +02:00
|
|
|
})
|
2020-05-07 18:08:12 +02:00
|
|
|
}
|
2020-05-08 10:21:03 +02:00
|
|
|
|
2020-07-09 11:27:44 +02:00
|
|
|
func TestFile_Create(t *testing.T) {
|
|
|
|
t.Run("photo id == 0", func(t *testing.T) {
|
|
|
|
file := File{PhotoID: 0}
|
|
|
|
|
|
|
|
assert.Error(t, file.Create())
|
|
|
|
})
|
|
|
|
t.Run("file already exists", func(t *testing.T) {
|
2020-07-17 16:09:55 +02:00
|
|
|
file := &File{PhotoID: 123, FileType: "jpg", FileSize: 500, ModTime: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC).Unix()}
|
2020-11-21 15:43:13 +01:00
|
|
|
assert.Nil(t, file.Create())
|
2020-07-09 11:27:44 +02:00
|
|
|
assert.Error(t, file.Create())
|
|
|
|
})
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: "Berlin / Morning Mood"}
|
|
|
|
|
|
|
|
file := &File{Photo: photo, FileType: "jpg", FileSize: 500, PhotoID: 766, FileName: "testname", FileRoot: "xyz"}
|
|
|
|
|
|
|
|
err := file.Create()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-08 10:21:03 +02:00
|
|
|
func TestFile_Purge(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSize: 500}
|
|
|
|
assert.Equal(t, nil, file.Purge())
|
|
|
|
})
|
|
|
|
}
|
2020-05-14 15:28:11 +02:00
|
|
|
|
2021-02-24 15:31:10 +01:00
|
|
|
func TestFile_Found(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSize: 500}
|
|
|
|
assert.Equal(t, nil, file.Purge())
|
|
|
|
assert.Equal(t, true, file.FileMissing)
|
|
|
|
err := file.Found()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, false, file.FileMissing)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-14 15:28:11 +02:00
|
|
|
func TestFile_AllFilesMissing(t *testing.T) {
|
|
|
|
t.Run("true", func(t *testing.T) {
|
2021-08-26 17:26:48 +02:00
|
|
|
file := FileFixtures.Get("missing.jpg")
|
2020-05-14 15:28:11 +02:00
|
|
|
assert.True(t, file.AllFilesMissing())
|
|
|
|
})
|
2021-08-26 17:26:48 +02:00
|
|
|
t.Run("false", func(t *testing.T) {
|
|
|
|
file := FileFixtures.Get("Quality1FavoriteTrue.jpg")
|
2020-05-14 15:28:11 +02:00
|
|
|
assert.False(t, file.AllFilesMissing())
|
2021-08-26 17:26:48 +02:00
|
|
|
})
|
2020-05-14 15:28:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFile_Save(t *testing.T) {
|
2020-05-18 17:24:54 +02:00
|
|
|
t.Run("save without photo", func(t *testing.T) {
|
2020-05-23 20:58:58 +02:00
|
|
|
file := &File{Photo: nil, FileType: "jpg", PhotoUID: "123", FileUID: "123"}
|
2020-05-14 15:28:11 +02:00
|
|
|
err := file.Save()
|
2020-05-18 17:24:54 +02:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("error should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if file.ID != 0 {
|
|
|
|
t.Fatalf("file id should be 0: %d", file.ID)
|
|
|
|
}
|
|
|
|
|
2021-10-05 18:42:39 +02:00
|
|
|
assert.Equal(t, "file 123: can't save file with empty photo id", err.Error())
|
2020-05-14 15:28:11 +02:00
|
|
|
})
|
2020-07-09 11:27:44 +02:00
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: "Berlin / Morning Mood"}
|
|
|
|
|
|
|
|
file := &File{Photo: photo, FileType: "jpg", FileSize: 500, PhotoID: 766, FileName: "Food", FileRoot: "", UpdatedAt: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)}
|
|
|
|
|
|
|
|
err := file.Save()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFile_UpdateVideoInfos(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
file := &File{FileType: "jpg", FileWidth: 600, FileName: "VideoUpdate", PhotoID: 1000003}
|
|
|
|
|
2021-08-16 17:15:06 +02:00
|
|
|
assert.Equal(t, "1990/04/bridge2.mp4", FileFixturesExampleBridgeVideo.FileName)
|
2020-07-09 11:27:44 +02:00
|
|
|
assert.Equal(t, int(1200), FileFixturesExampleBridgeVideo.FileWidth)
|
|
|
|
|
|
|
|
err := file.UpdateVideoInfos()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var files Files
|
|
|
|
|
|
|
|
if err := Db().Where("photo_id = ? AND file_video = 1", file.PhotoID).Find(&files).Error; err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, files, 1)
|
|
|
|
|
|
|
|
for _, f := range files {
|
2021-08-16 17:15:06 +02:00
|
|
|
assert.Equal(t, "1990/04/bridge2.mp4", f.FileName)
|
2020-07-09 11:27:44 +02:00
|
|
|
assert.Equal(t, int(600), f.FileWidth)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFile_Update(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
file := &File{FileType: "jpg", FileSize: 500, FileName: "ToBeUpdated", FileRoot: "", PhotoID: 5678}
|
|
|
|
|
|
|
|
err := file.Save()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, "ToBeUpdated", file.FileName)
|
|
|
|
|
|
|
|
err2 := file.Update("FileName", "Happy")
|
|
|
|
|
|
|
|
if err2 != nil {
|
|
|
|
t.Fatal(err2)
|
|
|
|
}
|
|
|
|
assert.Equal(t, "Happy", file.FileName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFile_Links(t *testing.T) {
|
2020-07-16 13:02:48 +02:00
|
|
|
t.Run("result", func(t *testing.T) {
|
2020-07-09 11:27:44 +02:00
|
|
|
file := FileFixturesExampleBridge
|
|
|
|
links := file.Links()
|
|
|
|
assert.Equal(t, "5jxf3jfn2k", links[0].LinkToken)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFile_NoJPEG(t *testing.T) {
|
2020-07-16 13:02:48 +02:00
|
|
|
t.Run("true", func(t *testing.T) {
|
|
|
|
file := &File{Photo: nil, FileType: "xmp", FileSize: 500}
|
|
|
|
assert.True(t, file.NoJPEG())
|
|
|
|
})
|
2020-07-09 11:27:44 +02:00
|
|
|
t.Run("false", func(t *testing.T) {
|
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSize: 500}
|
|
|
|
assert.False(t, file.NoJPEG())
|
|
|
|
})
|
2020-07-16 13:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFile_Panorama(t *testing.T) {
|
2020-07-17 10:10:01 +02:00
|
|
|
t.Run("3000", func(t *testing.T) {
|
2020-07-16 13:02:48 +02:00
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSidecar: false, FileWidth: 3000, FileHeight: 1000}
|
|
|
|
assert.True(t, file.Panorama())
|
|
|
|
})
|
2020-07-17 10:10:01 +02:00
|
|
|
t.Run("1999", func(t *testing.T) {
|
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSidecar: false, FileWidth: 1999, FileHeight: 1000}
|
|
|
|
assert.False(t, file.Panorama())
|
|
|
|
})
|
|
|
|
t.Run("2000", func(t *testing.T) {
|
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSidecar: false, FileWidth: 2000, FileHeight: 1000}
|
|
|
|
assert.True(t, file.Panorama())
|
|
|
|
})
|
2020-07-16 13:02:48 +02:00
|
|
|
t.Run("false", func(t *testing.T) {
|
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSidecar: false, FileWidth: 1500, FileHeight: 1000}
|
|
|
|
assert.False(t, file.Panorama())
|
|
|
|
})
|
|
|
|
t.Run("equirectangular", func(t *testing.T) {
|
2021-09-03 19:02:26 +02:00
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSidecar: false, FileWidth: 1500, FileHeight: 1000, FileProjection: ProjEquirectangular}
|
|
|
|
assert.True(t, file.Panorama())
|
|
|
|
})
|
|
|
|
t.Run("transverse-cylindrical", func(t *testing.T) {
|
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSidecar: false, FileWidth: 1500, FileHeight: 1000, FileProjection: ProjTransverseCylindrical}
|
2020-07-16 13:02:48 +02:00
|
|
|
assert.True(t, file.Panorama())
|
|
|
|
})
|
|
|
|
t.Run("sidecar", func(t *testing.T) {
|
|
|
|
file := &File{Photo: nil, FileType: "xmp", FileSidecar: true, FileWidth: 3000, FileHeight: 1000}
|
|
|
|
assert.False(t, file.Panorama())
|
2020-07-09 11:27:44 +02:00
|
|
|
})
|
2020-05-14 15:28:11 +02:00
|
|
|
}
|
2020-10-20 09:25:19 +02:00
|
|
|
|
2021-09-03 19:02:26 +02:00
|
|
|
func TestFile_SetProjection(t *testing.T) {
|
|
|
|
t.Run(ProjDefault, func(t *testing.T) {
|
|
|
|
m := &File{}
|
|
|
|
m.SetProjection(ProjDefault)
|
|
|
|
assert.Equal(t, ProjDefault, m.FileProjection)
|
|
|
|
})
|
|
|
|
t.Run(ProjCubestrip, func(t *testing.T) {
|
|
|
|
m := &File{}
|
|
|
|
m.SetProjection(ProjCubestrip)
|
|
|
|
assert.Equal(t, ProjCubestrip, m.FileProjection)
|
|
|
|
})
|
|
|
|
t.Run(ProjCylindrical, func(t *testing.T) {
|
|
|
|
m := &File{}
|
|
|
|
m.SetProjection(ProjCylindrical)
|
|
|
|
assert.Equal(t, ProjCylindrical, m.FileProjection)
|
|
|
|
})
|
|
|
|
t.Run(ProjTransverseCylindrical, func(t *testing.T) {
|
|
|
|
m := &File{}
|
|
|
|
m.SetProjection(ProjTransverseCylindrical)
|
|
|
|
assert.Equal(t, ProjTransverseCylindrical, m.FileProjection)
|
|
|
|
})
|
|
|
|
t.Run(ProjPseudocylindricalCompromise, func(t *testing.T) {
|
|
|
|
m := &File{}
|
|
|
|
m.SetProjection(ProjPseudocylindricalCompromise)
|
|
|
|
assert.Equal(t, ProjPseudocylindricalCompromise, m.FileProjection)
|
|
|
|
})
|
|
|
|
t.Run("Sanitize", func(t *testing.T) {
|
|
|
|
m := &File{}
|
|
|
|
m.SetProjection(" 幸福 Hanzi are logograms developed for the writing of Chinese! ")
|
|
|
|
assert.Equal(t, "hanzi are logograms developed for the writing of chinese", m.FileProjection)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-20 09:25:19 +02:00
|
|
|
func TestFile_Delete(t *testing.T) {
|
|
|
|
t.Run("permanently", func(t *testing.T) {
|
|
|
|
file := &File{FileType: "jpg", FileSize: 500, FileName: "ToBePermanentlyDeleted", FileRoot: "", PhotoID: 5678}
|
|
|
|
|
|
|
|
err := file.Save()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, "ToBePermanentlyDeleted", file.FileName)
|
|
|
|
|
|
|
|
err2 := file.Delete(true)
|
|
|
|
|
|
|
|
assert.Nil(t, err2)
|
|
|
|
})
|
|
|
|
t.Run("not permanently", func(t *testing.T) {
|
|
|
|
file := &File{FileType: "jpg", FileSize: 500, FileName: "ToBeDeleted", FileRoot: "", PhotoID: 5678}
|
|
|
|
|
|
|
|
err := file.Save()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, "ToBeDeleted", file.FileName)
|
|
|
|
|
|
|
|
err2 := file.Delete(false)
|
|
|
|
|
|
|
|
assert.Nil(t, err2)
|
|
|
|
})
|
|
|
|
}
|
2021-02-24 15:31:10 +01:00
|
|
|
|
|
|
|
func TestPrimaryFile(t *testing.T) {
|
|
|
|
file, err := PrimaryFile("pt9jtdre2lvl0y17")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-16 17:15:06 +02:00
|
|
|
assert.Equal(t, "Holiday/Video.jpg", file.FileName)
|
2021-02-24 15:31:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFile_OriginalBase(t *testing.T) {
|
|
|
|
t.Run("original name empty, filename empty", func(t *testing.T) {
|
|
|
|
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: "Berlin / Morning Mood"}
|
|
|
|
file := &File{Photo: photo, FileType: "jpg", FileUID: "foobar345678765", FileHash: "e98eb86480a72bd585d228a709f0622f90e86cbc", OriginalName: "", FileName: ""}
|
|
|
|
|
|
|
|
filename := file.OriginalBase(0)
|
|
|
|
|
|
|
|
assert.Contains(t, filename, "20190115-000000-Berlin-Morning-Mood")
|
|
|
|
assert.Contains(t, filename, fs.JpegExt)
|
|
|
|
|
|
|
|
filename2 := file.OriginalBase(1)
|
|
|
|
assert.Contains(t, filename2, "20190115-000000-Berlin-Morning-Mood")
|
|
|
|
assert.Contains(t, filename2, "(1)")
|
|
|
|
assert.Contains(t, filename2, fs.JpegExt)
|
|
|
|
})
|
|
|
|
t.Run("original name empty", func(t *testing.T) {
|
|
|
|
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: "Berlin / Morning Mood"}
|
|
|
|
file := &File{Photo: photo, FileType: "jpg", FileUID: "foobar345678765", FileHash: "e98eb86480a72bd585d228a709f0622f90e86cbc", OriginalName: "", FileName: "sonnenaufgang.jpg"}
|
|
|
|
|
|
|
|
filename := file.OriginalBase(0)
|
|
|
|
|
|
|
|
assert.Contains(t, filename, "sonnenaufgang")
|
|
|
|
assert.Contains(t, filename, fs.JpegExt)
|
|
|
|
|
|
|
|
filename2 := file.OriginalBase(1)
|
|
|
|
assert.Contains(t, filename2, "sonnenaufgang")
|
|
|
|
assert.Contains(t, filename2, "(1)")
|
|
|
|
assert.Contains(t, filename2, fs.JpegExt)
|
|
|
|
})
|
|
|
|
t.Run("original name not empty", func(t *testing.T) {
|
|
|
|
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: "Berlin / Morning Mood"}
|
|
|
|
file := &File{Photo: photo, FileType: "jpg", FileUID: "foobar345678765", FileHash: "e98eb86480a72bd585d228a709f0622f90e86cbc", OriginalName: "Sonnenaufgang.jpg", FileName: "123.jpg"}
|
|
|
|
|
|
|
|
filename := file.OriginalBase(0)
|
|
|
|
|
|
|
|
assert.Contains(t, filename, "Sonnenaufgang")
|
|
|
|
assert.Contains(t, filename, fs.JpegExt)
|
|
|
|
|
|
|
|
filename2 := file.OriginalBase(1)
|
|
|
|
assert.Contains(t, filename2, "Sonnenaufgang")
|
|
|
|
assert.Contains(t, filename2, "(1)")
|
|
|
|
assert.Contains(t, filename2, fs.JpegExt)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFile_DownloadName(t *testing.T) {
|
|
|
|
t.Run("DownloadNameFile", func(t *testing.T) {
|
|
|
|
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: "Berlin / Morning Mood"}
|
|
|
|
file := &File{Photo: photo, FileType: "jpg", FileUID: "foobar345678765", FileHash: "e98eb86480a72bd585d228a709f0622f90e86cbc", OriginalName: "originalName.jpg", FileName: "filename.jpg"}
|
|
|
|
|
|
|
|
filename := file.DownloadName(DownloadNameFile, 0)
|
|
|
|
assert.Contains(t, filename, "filename")
|
|
|
|
assert.Contains(t, filename, fs.JpegExt)
|
|
|
|
|
|
|
|
filename2 := file.DownloadName(DownloadNameOriginal, 1)
|
|
|
|
assert.Contains(t, filename2, "originalName")
|
|
|
|
assert.Contains(t, filename2, "(1)")
|
|
|
|
assert.Contains(t, filename2, fs.JpegExt)
|
|
|
|
|
|
|
|
filename3 := file.DownloadName("xxx", 0)
|
|
|
|
assert.Contains(t, filename3, "20190115-000000-Berlin-Morning-Mood")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFile_Undelete(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSize: 500}
|
|
|
|
assert.Equal(t, nil, file.Purge())
|
|
|
|
assert.Equal(t, true, file.FileMissing)
|
|
|
|
err := file.Undelete()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, false, file.FileMissing)
|
|
|
|
})
|
|
|
|
t.Run("file not missing", func(t *testing.T) {
|
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSize: 500}
|
|
|
|
assert.Equal(t, false, file.FileMissing)
|
|
|
|
err := file.Undelete()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, false, file.FileMissing)
|
|
|
|
})
|
|
|
|
}
|
2021-05-26 14:41:59 +02:00
|
|
|
|
|
|
|
func TestFile_AddFaces(t *testing.T) {
|
2021-09-22 19:33:41 +02:00
|
|
|
t.Run("Primary", func(t *testing.T) {
|
|
|
|
file := &File{FileUID: "fqzuh65p4sjk3kdn", FileHash: "346b3897eec9ef75e35fbf0bbc4c83c55ca41e31", FileType: "jpg", FileWidth: 720, FileName: "FacesTest", PhotoID: 1000003, FilePrimary: true}
|
2021-05-26 14:41:59 +02:00
|
|
|
|
|
|
|
faces := face.Faces{face.Face{
|
2021-09-22 19:33:41 +02:00
|
|
|
Rows: 480,
|
|
|
|
Cols: 720,
|
|
|
|
Score: 45,
|
|
|
|
Area: face.NewArea("face", 250, 200, 10),
|
|
|
|
Eyes: face.Areas{face.NewArea("eye_l", 240, 195, 1), face.NewArea("eye_r", 240, 205, 1)},
|
|
|
|
Landmarks: face.Areas{face.NewArea("a", 250, 185, 2), face.NewArea("b", 250, 215, 2)},
|
2021-09-30 13:44:23 +02:00
|
|
|
Embeddings: face.Embeddings{{0.012816238, 0.0054710666, 0.06963101, 0.037285835, 0.04412884, 0.017333193, 0.03373656, -0.033069234, 0.025952332, -0.0035901065, -0.029420156, 0.07464688, -0.043232113, 0.060328174, 0.028897963, -0.027495274, -0.02622295, 0.038605634, -0.030962847, 0.05343173, -0.05042871, 0.010407827, 0.014773584, 0.04305641, -0.045918103, 0.014705811, 0.0031296816, 0.08703609, 0.012646829, 0.040463835, 0.080548696, -0.04496776, 0.032542497, -0.046235666, 0.0018886769, -0.09422433, -0.006701393, 0.0601084, 0.05649471, -0.02277308, 0.048038833, -0.022022927, -0.024692882, -0.0067683067, 0.02597589, -0.026766079, -0.04489042, -0.060946267, 0.052194964, 0.0098239435, -0.063547, 0.008626338, -0.041202333, -0.057555206, -0.05206756, -0.007974572, -0.036597952, -0.04232167, 0.0064586936, 0.011131428, -0.076106876, -0.014716604, 0.027977718, 0.060634963, 0.0046368516, -0.035929997, -0.079733424, -0.051017676, -0.03521493, -0.0062531913, -0.030387852, 0.101194955, -0.027980363, -0.010152243, -0.005128962, -0.026926627, 0.008371125, -0.088778615, 0.022396773, -0.025815062, -0.0027552384, -0.049987435, -0.019902563, -0.024667386, 0.064883195, -0.010091326, -0.024541432, -0.03390568, -0.04975766, -0.05255319, 0.0462333, -0.062871166, 0.070803925, -0.020970127, 0.012365979, -0.048543453, 0.027297763, 0.02785581, 0.09220687, -0.021206442, 0.015040259, 0.11726589, 0.00079200073, 0.08544253, 0.08694455, -0.037786104, -0.09956117, 0.07473473, 0.086737245, 0.02916126, 0.0355523, 0.067868374, -0.056218974, 0.007066174, 0.046310645, -0.025015457, -0.019863142, -0.018884404, 0.00076502684, -0.0699868, 0.043558553, 0.11221989, -0.036503807, -0.07346668, 0.023614183, 0.008353507, 0.05629068, -0.05628395, -0.030611087, 0.013364313, -0.014508443, 0.013493559, 0.061809033, 0.06598724, -0.03538405, -0.08597677, -0.06253287, -0.032587055, 0.030790405, 0.031729434, 0.0349981, 0.09145327, 0.012044479, 0.09593962, -0.011460096, -0.014851449, -0.041916795, 0.0037967677, -0.028313408, -0.016944066, -0.023236271, 0.046519253, 0.09026307, -0.014203754, 0.0048228586, 0.012194195, -0.062746234, -0.02189861, -0.030368697, 0.004226377, -0.044146035, 0.04542304, 0.046805177, 0.03882082, -0.06006401, 0.06286592, 0.03714168, -0.011287339, -0.0129849315, 0.01757729, -0.031555075, 0.005606887, 0.0045785876, 0.031963747, 0.040269036, 0.033833507, -0.06477002, -0.0039275866, 0.079373375, 0.044617712, 0.012070597, -0.06322144, 0.011061547, -0.006825576, 0.033158936, -0.10063759, -0.016583988, 0.008227036, 0.05604638, -0.0039418507, 0.030264255, 0.006545456, -0.046788998, -0.06612186, 0.019110108, 0.010173552, -0.0015304928, -0.02745248, 0.08436771, -0.05111628, 0.03491268, -0.018905425, 0.009436589, -0.071091056, 0.06312779, 0.055885248, -0.008187491, 0.013967105, 0.049851406, -0.046775173, -0.05380721, -0.02520902, 0.048415728, -0.053037673, 0.08821214, -0.04349023, -0.002511317, -0.013129268, -0.04000359, -0.0100794975, -0.0659472, 0.044489317, -0.03651276, 0.0032823374, -0.004647774, -0.019675476, 0.11854173, 0.035627883, 0.015952459, -0.017490689, -0.009468227, -0.034936972, -0.0040077316, -0.014501512, -0.040732305, -0.004475036, 0.026295308, 0.11893579, -0.012221011, 0.01921595, 0.003704211, -0.00081420684, 0.031362444, 0.021526098, -0.03796045, -0.04051389, 0.08994492, 0.020430982, -0.13368618, 0.059530005, 0.02978135, -0.020171875, 0.07243986, 0.08047519, 0.014236827, 0.023928184, -0.056827813, 0.030533543, -0.01695773, 0.0019564428, 0.019315101, 0.048426118, 0.012069902, 0.014532966, 0.07157925, -0.00082132005, -0.03102693, 0.05207618, 0.033050887, -0.06816059, 0.037159886, 0.012156096, 0.0906456, 0.05786973, 0.021087963, -0.03615757, -0.0006905898, 0.0062891473, 0.054622658, -0.02605763, -0.050890833, -0.00017370642, -0.010385195, 0.022578984, 0.001822225, -0.045328267, 0.015035055, 0.05529688, 0.046605356, -0.0007772419, -0.09158666, -0.039371215, -0.0026332953, 0.022653094, 0.077683136, -0.027678892, -0.07956019, -0.08317627, 0.012950206, -0.04643972, 0.027308058, -0.007675166, 0.009162879, -0.0064983773, -0.0073145335, 0.041186735, -0
|
2021-05-26 14:41:59 +02:00
|
|
|
}}
|
|
|
|
|
|
|
|
file.AddFaces(faces)
|
|
|
|
|
2021-09-22 19:33:41 +02:00
|
|
|
assert.Equal(t, 1, len(*file.Markers()))
|
|
|
|
|
2021-05-26 14:41:59 +02:00
|
|
|
if err := file.Save(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, false, file.FileMissing)
|
|
|
|
assert.NotEmpty(t, file.FileUID)
|
2021-09-02 11:12:42 +02:00
|
|
|
assert.NotEmpty(t, file.Markers())
|
2021-05-26 14:41:59 +02:00
|
|
|
})
|
2021-09-22 19:33:41 +02:00
|
|
|
t.Run("NoEmbeddings", func(t *testing.T) {
|
|
|
|
file := &File{FileUID: "fqzuh65p4sjk3kd1", FileHash: "146b3897eec9ef75e35fbf0bbc4c83c55ca41e31", FileType: "jpg", FileWidth: 720, FileName: "FacesTest", PhotoID: 1000003, FilePrimary: false}
|
|
|
|
|
|
|
|
faces := face.Faces{face.Face{
|
|
|
|
Rows: 480,
|
|
|
|
Cols: 720,
|
|
|
|
Score: 45,
|
|
|
|
Area: face.NewArea("face", 250, 200, 10),
|
|
|
|
Eyes: face.Areas{face.NewArea("eye_l", 240, 195, 1), face.NewArea("eye_r", 240, 205, 1)},
|
|
|
|
Landmarks: face.Areas{face.NewArea("a", 250, 185, 2), face.NewArea("b", 250, 215, 2)},
|
|
|
|
}}
|
|
|
|
|
|
|
|
file.AddFaces(faces)
|
|
|
|
|
|
|
|
assert.Equal(t, 0, len(*file.Markers()))
|
|
|
|
})
|
2021-05-26 14:41:59 +02:00
|
|
|
}
|
2021-06-02 17:25:04 +02:00
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
func TestFile_ValidFaceCount(t *testing.T) {
|
2021-06-02 17:25:04 +02:00
|
|
|
t.Run("FileFixturesExampleBridge", func(t *testing.T) {
|
|
|
|
file := FileFixturesExampleBridge
|
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
result := file.ValidFaceCount()
|
2021-06-02 17:25:04 +02:00
|
|
|
|
|
|
|
assert.GreaterOrEqual(t, result, 3)
|
|
|
|
})
|
|
|
|
}
|
2021-08-26 17:26:48 +02:00
|
|
|
|
|
|
|
func TestFile_Rename(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
m := FileFixtures.Get("exampleFileName.jpg")
|
2021-09-02 14:23:40 +02:00
|
|
|
|
2021-08-26 17:26:48 +02:00
|
|
|
assert.Equal(t, "2790/07/27900704_070228_D6D51B6C.jpg", m.FileName)
|
|
|
|
assert.Equal(t, RootOriginals, m.FileRoot)
|
|
|
|
assert.Equal(t, false, m.FileMissing)
|
|
|
|
assert.Nil(t, m.DeletedAt)
|
|
|
|
|
|
|
|
p := m.RelatedPhoto()
|
2021-09-02 14:23:40 +02:00
|
|
|
|
2021-08-26 17:26:48 +02:00
|
|
|
assert.Equal(t, "2790/07", p.PhotoPath)
|
|
|
|
assert.Equal(t, "27900704_070228_D6D51B6C", p.PhotoName)
|
|
|
|
|
2021-09-02 14:23:40 +02:00
|
|
|
if err := m.Rename("x/y/newName.jpg", "newRoot", "x/y", "newBase"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-26 17:26:48 +02:00
|
|
|
assert.Equal(t, "x/y/newName.jpg", m.FileName)
|
|
|
|
assert.Equal(t, "newRoot", m.FileRoot)
|
|
|
|
assert.Equal(t, false, m.FileMissing)
|
|
|
|
assert.Nil(t, m.DeletedAt)
|
|
|
|
assert.Equal(t, "x/y", p.PhotoPath)
|
|
|
|
assert.Equal(t, "newBase", p.PhotoName)
|
|
|
|
|
2021-09-02 14:23:40 +02:00
|
|
|
if err := m.Rename("2790/07/27900704_070228_D6D51B6C.jpg", RootOriginals, "2790/07", "27900704_070228_D6D51B6C"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-26 17:26:48 +02:00
|
|
|
assert.Equal(t, "2790/07/27900704_070228_D6D51B6C.jpg", m.FileName)
|
|
|
|
assert.Equal(t, RootOriginals, m.FileRoot)
|
|
|
|
assert.Equal(t, false, m.FileMissing)
|
|
|
|
assert.Nil(t, m.DeletedAt)
|
|
|
|
assert.Equal(t, "2790/07", p.PhotoPath)
|
|
|
|
assert.Equal(t, "27900704_070228_D6D51B6C", p.PhotoName)
|
|
|
|
})
|
|
|
|
}
|
2021-09-02 11:12:42 +02:00
|
|
|
|
|
|
|
func TestFile_SubjectNames(t *testing.T) {
|
2021-09-02 17:44:27 +02:00
|
|
|
t.Run("Photo27.jpg", func(t *testing.T) {
|
|
|
|
m := FileFixtures.Get("Photo27.jpg")
|
2021-09-02 14:23:40 +02:00
|
|
|
|
|
|
|
names := m.SubjectNames()
|
2021-09-02 17:44:27 +02:00
|
|
|
t.Log(len(names))
|
2021-09-02 14:23:40 +02:00
|
|
|
if len(names) != 1 {
|
|
|
|
t.Errorf("there should be one name: %#v", names)
|
|
|
|
} else {
|
2021-09-02 17:44:27 +02:00
|
|
|
assert.Equal(t, "Actress A", names[0])
|
2021-09-02 14:23:40 +02:00
|
|
|
}
|
|
|
|
})
|
2021-09-02 17:44:27 +02:00
|
|
|
t.Run("Video.jpg", func(t *testing.T) {
|
|
|
|
m := FileFixtures.Get("Video.jpg")
|
2021-09-02 14:23:40 +02:00
|
|
|
|
|
|
|
names := m.SubjectNames()
|
2021-09-02 17:44:27 +02:00
|
|
|
t.Log(len(names))
|
2021-09-02 14:23:40 +02:00
|
|
|
if len(names) != 1 {
|
|
|
|
t.Errorf("there should be one name: %#v", names)
|
|
|
|
} else {
|
2021-09-02 17:44:27 +02:00
|
|
|
assert.Equal(t, "Actor A", names[0])
|
2021-09-02 14:23:40 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("bridge.jpg", func(t *testing.T) {
|
|
|
|
m := FileFixtures.Get("bridge.jpg")
|
|
|
|
|
|
|
|
names := m.SubjectNames()
|
|
|
|
|
|
|
|
if len(names) != 2 {
|
|
|
|
t.Errorf("two names expected: %#v", names)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, []string{"Corn McCornface", "Jens Mander"}, names)
|
|
|
|
}
|
|
|
|
})
|
2021-09-02 11:12:42 +02:00
|
|
|
}
|
2021-09-23 23:46:17 +02:00
|
|
|
|
|
|
|
func TestFile_UnsavedMarkers(t *testing.T) {
|
|
|
|
t.Run("bridge2.jpg", func(t *testing.T) {
|
|
|
|
m := FileFixtures.Get("bridge2.jpg")
|
|
|
|
assert.Equal(t, "ft2es49w15bnlqdw", m.FileUID)
|
|
|
|
assert.False(t, m.UnsavedMarkers())
|
|
|
|
|
|
|
|
markers := m.Markers()
|
|
|
|
|
|
|
|
assert.Equal(t, 1, m.ValidFaceCount())
|
|
|
|
assert.Equal(t, 1, markers.ValidFaceCount())
|
|
|
|
assert.Equal(t, 1, markers.DetectedFaceCount())
|
|
|
|
assert.False(t, m.UnsavedMarkers())
|
|
|
|
assert.False(t, markers.Unsaved())
|
|
|
|
|
|
|
|
newMarker := *NewMarker(m, cropArea1, "lt9k3pw1wowuy1c1", SrcManual, MarkerFace, 100, 65)
|
|
|
|
|
|
|
|
markers.Append(newMarker)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, m.ValidFaceCount())
|
|
|
|
assert.Equal(t, 2, markers.ValidFaceCount())
|
|
|
|
assert.Equal(t, 1, markers.DetectedFaceCount())
|
|
|
|
assert.True(t, m.UnsavedMarkers())
|
|
|
|
assert.True(t, markers.Unsaved())
|
|
|
|
})
|
|
|
|
}
|
2021-09-24 13:13:59 +02:00
|
|
|
|
|
|
|
func TestFile_ReplaceHash(t *testing.T) {
|
|
|
|
t.Run("exampleFileName.jpg", func(t *testing.T) {
|
|
|
|
m := FileFixtures.Get("exampleFileName.jpg")
|
|
|
|
|
|
|
|
if err := m.ReplaceHash(""); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|