2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2019-05-16 04:03:55 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
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
|
|
|
|
2020-04-01 12:00:45 +02:00
|
|
|
filename := file.ShareFileName()
|
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
|
|
|
|
2020-04-01 12:00:45 +02:00
|
|
|
filename := file.ShareFileName()
|
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
|
|
|
|
2020-04-01 12:00:45 +02:00
|
|
|
filename := file.ShareFileName()
|
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
|
|
|
})
|
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"}
|
|
|
|
|
|
|
|
filename := file.ShareFileName()
|
|
|
|
|
|
|
|
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"}
|
|
|
|
|
|
|
|
filename := file.ShareFileName()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func TestFile_AllFilesMissing(t *testing.T) {
|
|
|
|
t.Run("true", func(t *testing.T) {
|
|
|
|
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: ""}
|
2020-05-23 20:58:58 +02:00
|
|
|
file := &File{Photo: photo, FileType: "jpg", PhotoUID: "123", FileUID: "123", FileMissing: true}
|
|
|
|
file2 := &File{Photo: photo, FileType: "jpg", PhotoUID: "123", FileUID: "456", FileMissing: true}
|
2020-05-14 15:28:11 +02:00
|
|
|
assert.True(t, file.AllFilesMissing())
|
|
|
|
assert.NotEmpty(t, file2)
|
|
|
|
})
|
|
|
|
//TODO test false
|
|
|
|
/*t.Run("false", func(t *testing.T) {
|
|
|
|
file := FileFixturesExampleJPG
|
|
|
|
assert.False(t, file.AllFilesMissing())
|
|
|
|
assert.NotEmpty(t, file)
|
|
|
|
})*/
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
assert.Equal(t, "file: photo id must not be empty (save 123)", 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}
|
|
|
|
|
|
|
|
assert.Equal(t, "bridge.mp4", FileFixturesExampleBridgeVideo.FileName)
|
|
|
|
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 {
|
|
|
|
assert.Equal(t, "bridge.mp4", f.FileName)
|
|
|
|
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) {
|
|
|
|
file := &File{Photo: nil, FileType: "jpg", FileSidecar: false, FileWidth: 1500, FileHeight: 1000, FileProjection: ProjectionEquirectangular}
|
|
|
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|