photoprism/internal/entity/file_test.go

116 lines
3.9 KiB
Go
Raw Normal View History

package entity
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
2020-05-07 16:08:12 +00:00
func TestFirstFileByHash(t *testing.T) {
t.Run("not existing file", func(t *testing.T) {
f, err := FirstFileByHash("xxx")
assert.Equal(t, "record not found", err.Error())
assert.Equal(t, uint(0), f.ID)
})
t.Run("existing file", func(t *testing.T) {
f, err := FirstFileByHash("2cad9168fa6acc5c5c2965ddf6ec465ca42fd818")
if err != nil {
t.Fatal(err)
2020-05-07 16:08:12 +00:00
}
assert.Equal(t, uint(0xf4240), f.ID)
})
}
func TestFile_DownloadFileName(t *testing.T) {
2019-12-17 17:26:13 +00:00
t.Run("photo with title", 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", FileUUID: "foobar345678765"}
filename := file.ShareFileName()
assert.Contains(t, filename, "20190115-000000-Berlin-Morning-Mood")
assert.Contains(t, filename, ".jpg")
2019-12-17 17:26:13 +00:00
})
t.Run("photo without title", func(t *testing.T) {
photo := &Photo{TakenAtLocal: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), PhotoTitle: ""}
file := &File{Photo: photo, FileType: "jpg", PhotoUUID: "123", FileUUID: "foobar345678765"}
2019-12-17 17:26:13 +00:00
filename := file.ShareFileName()
2019-12-17 17:26:13 +00:00
assert.Contains(t, filename, "20190115-000000-123")
assert.Contains(t, filename, ".jpg")
2019-12-17 17:26:13 +00:00
})
t.Run("photo without photo", func(t *testing.T) {
file := &File{Photo: nil, FileType: "jpg", FileHash: "123Hash", FileUUID: "foobar345678765"}
2019-12-17 17:26:13 +00:00
filename := file.ShareFileName()
2019-12-17 17:26:13 +00:00
assert.Equal(t, "123Hash.jpg", filename)
})
}
2020-05-07 16:08:12 +00:00
func TestFile_Changed(t *testing.T) {
2020-05-14 13:28:11 +00:00
var deletedAt = time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)
2020-05-07 16:08:12 +00:00
t.Run("different modified times", func(t *testing.T) {
file := &File{Photo: nil, FileType: "jpg", FileSize: 500, FileModified: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)}
time := time.Date(2020, 01, 15, 0, 0, 0, 0, time.UTC)
assert.Equal(t, true, file.Changed(500, time))
})
t.Run("different sizes", func(t *testing.T) {
file := &File{Photo: nil, FileType: "jpg", FileSize: 600, FileModified: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)}
time := time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)
assert.Equal(t, true, file.Changed(500, time))
})
t.Run("no change", func(t *testing.T) {
file := &File{Photo: nil, FileType: "jpg", FileSize: 500, FileModified: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)}
time := time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)
assert.Equal(t, false, file.Changed(500, time))
})
2020-05-14 13:28:11 +00:00
t.Run("deleted", func(t *testing.T) {
file := &File{Photo: nil, FileType: "jpg", FileSize: 500, FileModified: time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), DeletedAt: &deletedAt}
time := time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC)
assert.Equal(t, true, file.Changed(500, time))
})
2020-05-07 16:08:12 +00:00
}
2020-05-08 08:21:03 +00: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 13:28:11 +00: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: ""}
file := &File{Photo: photo, FileType: "jpg", PhotoUUID: "123", FileUUID: "123", FileMissing: true}
file2 := &File{Photo: photo, FileType: "jpg", PhotoUUID: "123", FileUUID: "456", FileMissing: true}
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) {
t.Run("save without photo", func(t *testing.T) {
2020-05-14 13:28:11 +00:00
file := &File{Photo: nil, FileType: "jpg", PhotoUUID: "123", FileUUID: "123"}
err := file.Save()
if err == nil {
t.Fatal("error should not be nil")
}
if file.ID != 0 {
t.Fatalf("file id should be 0: %d", file.ID)
}
assert.Equal(t, "file: photo id is empty (123)", err.Error())
2020-05-14 13:28:11 +00:00
})
}