2020-01-28 20:59:26 +01:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-07-20 19:48:31 +02:00
|
|
|
"time"
|
2020-01-28 20:59:26 +01:00
|
|
|
|
2020-07-17 12:47:12 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2020-05-08 15:41:01 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-01-28 20:59:26 +01:00
|
|
|
)
|
|
|
|
|
2020-05-24 22:16:06 +02:00
|
|
|
func TestFilesByPath(t *testing.T) {
|
|
|
|
t.Run("files found", func(t *testing.T) {
|
2021-08-16 17:15:06 +02:00
|
|
|
files, err := FilesByPath(10, 0, entity.RootOriginals, "Holiday")
|
2020-05-24 22:16:06 +02:00
|
|
|
|
|
|
|
t.Logf("files: %+v", files)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-07-13 11:40:49 +02:00
|
|
|
assert.LessOrEqual(t, 1, len(files))
|
|
|
|
})
|
|
|
|
t.Run("files found - path starting with /", func(t *testing.T) {
|
2021-08-16 17:15:06 +02:00
|
|
|
files, err := FilesByPath(10, 0, entity.RootOriginals, "/Holiday")
|
2020-07-13 11:40:49 +02:00
|
|
|
|
|
|
|
t.Logf("files: %+v", files)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:16:06 +02:00
|
|
|
assert.LessOrEqual(t, 1, len(files))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
func TestExistingFiles(t *testing.T) {
|
2020-01-28 20:59:26 +01:00
|
|
|
t.Run("files found", func(t *testing.T) {
|
2020-06-07 10:09:35 +02:00
|
|
|
files, err := Files(1000, 0, "/", true)
|
2020-05-08 15:41:01 +02:00
|
|
|
|
|
|
|
t.Logf("files: %+v", files)
|
2020-01-28 20:59:26 +01:00
|
|
|
|
2020-05-13 10:16:12 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-05-01 14:16:40 +02:00
|
|
|
assert.LessOrEqual(t, 5, len(files))
|
2020-01-28 20:59:26 +01:00
|
|
|
})
|
2020-07-13 11:40:49 +02:00
|
|
|
t.Run("files found - includeMissing false", func(t *testing.T) {
|
|
|
|
files, err := Files(1000, 0, "/", false)
|
|
|
|
|
|
|
|
t.Logf("files: %+v", files)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.LessOrEqual(t, 5, len(files))
|
|
|
|
})
|
2020-05-13 10:16:12 +02:00
|
|
|
t.Run("search for files path", func(t *testing.T) {
|
2020-06-07 10:09:35 +02:00
|
|
|
files, err := Files(1000, 0, "Photos", true)
|
2020-05-13 10:16:12 +02:00
|
|
|
|
|
|
|
t.Logf("files: %+v", files)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-05-24 22:16:06 +02:00
|
|
|
|
2020-05-13 10:16:12 +02:00
|
|
|
assert.Empty(t, files)
|
|
|
|
})
|
2020-01-28 20:59:26 +01:00
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
func TestFilesByUID(t *testing.T) {
|
2020-01-28 20:59:26 +01:00
|
|
|
t.Run("files found", func(t *testing.T) {
|
2020-05-23 20:58:58 +02:00
|
|
|
files, err := FilesByUID([]string{"ft8es39w45bnlqdw"}, 100, 0)
|
2020-01-28 20:59:26 +01:00
|
|
|
|
2020-05-13 10:16:12 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-01-28 20:59:26 +01:00
|
|
|
assert.Equal(t, 1, len(files))
|
2021-08-05 12:33:52 +02:00
|
|
|
assert.Equal(t, "2790/07/27900704_070228_D6D51B6C.jpg", files[0].FileName)
|
2020-01-28 20:59:26 +01:00
|
|
|
})
|
2020-05-13 10:16:12 +02:00
|
|
|
t.Run("no files found", func(t *testing.T) {
|
2020-05-23 20:58:58 +02:00
|
|
|
files, err := FilesByUID([]string{"ft8es39w45bnlxxx"}, 100, 0)
|
2020-05-13 10:16:12 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, 0, len(files))
|
|
|
|
})
|
2021-03-03 11:49:32 +01:00
|
|
|
t.Run("error", func(t *testing.T) {
|
|
|
|
files, err := FilesByUID([]string{"ft8es39w45bnlxxx"}, -100, 0)
|
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, 0, len(files))
|
|
|
|
})
|
2020-01-28 20:59:26 +01:00
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
func TestFileByPhotoUID(t *testing.T) {
|
2020-01-28 20:59:26 +01:00
|
|
|
t.Run("files found", func(t *testing.T) {
|
2020-05-25 19:10:44 +02:00
|
|
|
file, err := FileByPhotoUID("pt9jtdre2lvl0y11")
|
2020-01-28 20:59:26 +01:00
|
|
|
|
2020-05-13 10:16:12 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-16 17:15:06 +02:00
|
|
|
assert.Equal(t, "Germany/bridge.jpg", file.FileName)
|
2020-01-28 20:59:26 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no files found", func(t *testing.T) {
|
2020-05-23 20:58:58 +02:00
|
|
|
file, err := FileByPhotoUID("111")
|
2020-01-28 20:59:26 +01:00
|
|
|
|
|
|
|
assert.Error(t, err, "record not found")
|
|
|
|
t.Log(file)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-13 11:40:49 +02:00
|
|
|
func TestVideoByPhotoUID(t *testing.T) {
|
|
|
|
t.Run("files found", func(t *testing.T) {
|
|
|
|
file, err := VideoByPhotoUID("pt9jtdre2lvl0yh0")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-16 17:15:06 +02:00
|
|
|
assert.Equal(t, "1990/04/bridge2.mp4", file.FileName)
|
2020-07-13 11:40:49 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no files found", func(t *testing.T) {
|
|
|
|
file, err := VideoByPhotoUID("111")
|
|
|
|
|
|
|
|
assert.Error(t, err, "record not found")
|
|
|
|
t.Log(file)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
func TestFileByUID(t *testing.T) {
|
2020-01-28 20:59:26 +01:00
|
|
|
t.Run("files found", func(t *testing.T) {
|
2020-05-23 20:58:58 +02:00
|
|
|
file, err := FileByUID("ft8es39w45bnlqdw")
|
2020-01-28 20:59:26 +01:00
|
|
|
|
2020-04-08 13:24:06 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-05 12:33:52 +02:00
|
|
|
assert.Equal(t, "2790/07/27900704_070228_D6D51B6C.jpg", file.FileName)
|
2020-01-28 20:59:26 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no files found", func(t *testing.T) {
|
2020-05-23 20:58:58 +02:00
|
|
|
file, err := FileByUID("111")
|
2020-04-08 13:24:06 +02:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("error expected")
|
|
|
|
}
|
2020-01-28 20:59:26 +01:00
|
|
|
|
|
|
|
assert.Error(t, err, "record not found")
|
|
|
|
t.Log(file)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
func TestFileByHash(t *testing.T) {
|
2020-01-28 20:59:26 +01:00
|
|
|
t.Run("files found", func(t *testing.T) {
|
2020-05-08 15:41:01 +02:00
|
|
|
file, err := FileByHash("2cad9168fa6acc5c5c2965ddf6ec465ca42fd818")
|
2020-01-28 20:59:26 +01:00
|
|
|
|
2020-05-13 10:16:12 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-05 12:33:52 +02:00
|
|
|
assert.Equal(t, "2790/07/27900704_070228_D6D51B6C.jpg", file.FileName)
|
2020-01-28 20:59:26 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no files found", func(t *testing.T) {
|
2020-05-08 15:41:01 +02:00
|
|
|
file, err := FileByHash("111")
|
2020-01-28 20:59:26 +01:00
|
|
|
|
|
|
|
assert.Error(t, err, "record not found")
|
|
|
|
t.Log(file)
|
|
|
|
})
|
|
|
|
}
|
2020-05-13 10:16:12 +02:00
|
|
|
|
|
|
|
func TestSetPhotoPrimary(t *testing.T) {
|
2021-01-24 20:40:40 +01:00
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
assert.Equal(t, false, entity.FileFixturesExampleXMP.FilePrimary)
|
2020-05-13 10:16:12 +02:00
|
|
|
|
2021-01-24 20:40:40 +01:00
|
|
|
err := SetPhotoPrimary("pt9jtdre2lvl0yh7", "ft2es49whhbnlqdn")
|
2020-05-13 10:16:12 +02:00
|
|
|
|
2021-01-24 20:40:40 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("no_file_uid", func(t *testing.T) {
|
|
|
|
err := SetPhotoPrimary("pt9jtdre2lvl0yh7", "")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("no_uid", func(t *testing.T) {
|
|
|
|
err := SetPhotoPrimary("", "")
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("error expected")
|
|
|
|
}
|
|
|
|
})
|
2021-08-31 16:33:55 +02:00
|
|
|
t.Run("file missing", func(t *testing.T) {
|
|
|
|
err := SetPhotoPrimary("pt9jtdre2lvl0y22", "")
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("error expected")
|
|
|
|
}
|
2022-01-05 11:40:44 +01:00
|
|
|
assert.Contains(t, err.Error(), "cannot find primary file")
|
2021-08-31 16:33:55 +02:00
|
|
|
})
|
2020-05-13 10:16:12 +02:00
|
|
|
}
|
2020-07-13 11:40:49 +02:00
|
|
|
|
|
|
|
func TestSetFileError(t *testing.T) {
|
|
|
|
assert.Equal(t, "", entity.FileFixturesExampleXMP.FileError)
|
|
|
|
|
|
|
|
SetFileError("ft2es49whhbnlqdn", "errorFromTest")
|
|
|
|
|
|
|
|
//TODO How to assert
|
|
|
|
//assert.Equal(t, true, entity.FileFixturesExampleXMP.FilePrimary)
|
|
|
|
}
|
2020-07-17 12:47:12 +02:00
|
|
|
|
2021-09-30 15:50:10 +02:00
|
|
|
func TestRenameFile(t *testing.T) {
|
|
|
|
t.Run("empty name", func(t *testing.T) {
|
|
|
|
err := RenameFile("xxx", "", "yyy", "yyy")
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
assert.Equal(t, "2790/02/Photo01.xmp", entity.FileFixturesExampleXMP.FileName)
|
|
|
|
assert.Equal(t, "/", entity.FileFixturesExampleXMP.FileRoot)
|
|
|
|
err := RenameFile("/", "exampleXmpFile.xmp", "test-root", "yyy.jpg")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
//TODO how to assert?
|
|
|
|
//assert.Equal(t, "", entity.FileFixturesExampleXMP.FileName)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-17 12:47:12 +02:00
|
|
|
func TestIndexedFiles(t *testing.T) {
|
2020-07-20 19:48:31 +02:00
|
|
|
if err := entity.AddDuplicate(
|
|
|
|
"Photo18.jpg",
|
|
|
|
entity.RootSidecar,
|
|
|
|
"3cad9168fa6acc5c5c2965ddf6ec465ca42fd818",
|
|
|
|
661858,
|
|
|
|
time.Date(2019, 3, 6, 2, 6, 51, 0, time.UTC).Unix(),
|
|
|
|
); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-07-17 12:47:12 +02:00
|
|
|
result, err := IndexedFiles()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-09-30 15:50:10 +02:00
|
|
|
t.Logf("indexed files: %#v", result)
|
2020-07-17 12:47:12 +02:00
|
|
|
}
|
2021-01-24 17:46:18 +01:00
|
|
|
|
2021-09-30 15:50:10 +02:00
|
|
|
func TestOrphanFiles(t *testing.T) {
|
|
|
|
files, err := OrphanFiles()
|
2021-03-03 11:49:32 +01:00
|
|
|
|
2021-09-30 15:50:10 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-03-03 11:49:32 +01:00
|
|
|
|
2021-09-30 15:50:10 +02:00
|
|
|
assert.IsType(t, entity.Files{}, files)
|
2021-03-03 11:49:32 +01:00
|
|
|
}
|