2020-05-22 16:29:12 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-12-09 13:10:21 +01:00
|
|
|
"time"
|
2020-05-22 16:29:12 +02:00
|
|
|
|
2020-11-21 18:08:41 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
|
2020-05-22 16:29:12 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewFolder(t *testing.T) {
|
|
|
|
t.Run("2020/05", func(t *testing.T) {
|
2020-12-09 13:10:21 +01:00
|
|
|
folder := NewFolder(RootOriginals, "2020/05", time.Now().UTC())
|
2020-06-07 10:09:35 +02:00
|
|
|
assert.Equal(t, RootOriginals, folder.Root)
|
2020-05-22 16:29:12 +02:00
|
|
|
assert.Equal(t, "2020/05", folder.Path)
|
|
|
|
assert.Equal(t, "May 2020", folder.FolderTitle)
|
|
|
|
assert.Equal(t, "", folder.FolderDescription)
|
|
|
|
assert.Equal(t, "", folder.FolderType)
|
|
|
|
assert.Equal(t, SortOrderName, folder.FolderOrder)
|
2020-05-23 20:58:58 +02:00
|
|
|
assert.IsType(t, "", folder.FolderUID)
|
2020-05-22 16:29:12 +02:00
|
|
|
assert.Equal(t, false, folder.FolderFavorite)
|
|
|
|
assert.Equal(t, false, folder.FolderIgnore)
|
|
|
|
assert.Equal(t, false, folder.FolderWatch)
|
2020-05-28 15:12:18 +02:00
|
|
|
assert.Equal(t, 2020, folder.FolderYear)
|
|
|
|
assert.Equal(t, 5, folder.FolderMonth)
|
|
|
|
assert.Equal(t, "zz", folder.FolderCountry)
|
2020-05-22 16:29:12 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("/2020/05/01/", func(t *testing.T) {
|
2020-12-09 13:10:21 +01:00
|
|
|
folder := NewFolder(RootOriginals, "/2020/05/01/", time.Now().UTC())
|
2020-05-22 16:29:12 +02:00
|
|
|
assert.Equal(t, "2020/05/01", folder.Path)
|
|
|
|
assert.Equal(t, "May 2020", folder.FolderTitle)
|
2020-05-28 15:12:18 +02:00
|
|
|
assert.Equal(t, 2020, folder.FolderYear)
|
|
|
|
assert.Equal(t, 5, folder.FolderMonth)
|
|
|
|
assert.Equal(t, "zz", folder.FolderCountry)
|
2020-05-22 16:29:12 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("/2020/05/23/", func(t *testing.T) {
|
2020-12-09 13:10:21 +01:00
|
|
|
folder := NewFolder(RootImport, "/2020/05/23/", time.Now().UTC())
|
2020-05-22 16:29:12 +02:00
|
|
|
assert.Equal(t, "2020/05/23", folder.Path)
|
|
|
|
assert.Equal(t, "May 23, 2020", folder.FolderTitle)
|
2020-05-28 15:12:18 +02:00
|
|
|
assert.Equal(t, 2020, folder.FolderYear)
|
|
|
|
assert.Equal(t, 5, folder.FolderMonth)
|
|
|
|
assert.Equal(t, "zz", folder.FolderCountry)
|
2020-05-22 16:29:12 +02:00
|
|
|
})
|
|
|
|
|
2020-05-28 15:12:18 +02:00
|
|
|
t.Run("/2020/05/23/Iceland 2020", func(t *testing.T) {
|
2020-12-09 13:10:21 +01:00
|
|
|
folder := NewFolder(RootOriginals, "/2020/05/23/Iceland 2020", time.Now().UTC())
|
2020-05-28 15:12:18 +02:00
|
|
|
assert.Equal(t, "2020/05/23/Iceland 2020", folder.Path)
|
|
|
|
assert.Equal(t, "Iceland 2020", folder.FolderTitle)
|
|
|
|
assert.Equal(t, 2020, folder.FolderYear)
|
|
|
|
assert.Equal(t, 5, folder.FolderMonth)
|
|
|
|
assert.Equal(t, "is", folder.FolderCountry)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("/London/2020/05/23", func(t *testing.T) {
|
2020-12-09 13:10:21 +01:00
|
|
|
folder := NewFolder(RootOriginals, "/London/2020/05/23", time.Now().UTC())
|
2020-05-28 15:12:18 +02:00
|
|
|
assert.Equal(t, "London/2020/05/23", folder.Path)
|
|
|
|
assert.Equal(t, "May 23, 2020", folder.FolderTitle)
|
|
|
|
assert.Equal(t, 2020, folder.FolderYear)
|
|
|
|
assert.Equal(t, 5, folder.FolderMonth)
|
|
|
|
assert.Equal(t, "gb", folder.FolderCountry)
|
2020-05-22 16:29:12 +02:00
|
|
|
})
|
|
|
|
|
2020-05-22 19:05:16 +02:00
|
|
|
t.Run("empty", func(t *testing.T) {
|
2020-12-09 13:10:21 +01:00
|
|
|
folder := NewFolder(RootOriginals, "", time.Time{})
|
2020-05-22 19:05:16 +02:00
|
|
|
assert.Equal(t, "", folder.Path)
|
|
|
|
assert.Equal(t, "Originals", folder.FolderTitle)
|
2020-05-28 15:12:18 +02:00
|
|
|
assert.Equal(t, 0, folder.FolderYear)
|
|
|
|
assert.Equal(t, 0, folder.FolderMonth)
|
|
|
|
assert.Equal(t, "zz", folder.FolderCountry)
|
2020-05-22 19:05:16 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("root", func(t *testing.T) {
|
2020-12-09 13:10:21 +01:00
|
|
|
folder := NewFolder(RootOriginals, RootPath, time.Time{})
|
2020-05-22 19:05:16 +02:00
|
|
|
assert.Equal(t, "", folder.Path)
|
2020-05-22 16:29:12 +02:00
|
|
|
assert.Equal(t, "Originals", folder.FolderTitle)
|
2020-05-28 15:12:18 +02:00
|
|
|
assert.Equal(t, 0, folder.FolderYear)
|
|
|
|
assert.Equal(t, 0, folder.FolderMonth)
|
|
|
|
assert.Equal(t, "zz", folder.FolderCountry)
|
2020-05-22 16:29:12 +02:00
|
|
|
})
|
2020-07-09 12:24:44 +02:00
|
|
|
|
|
|
|
t.Run("pathName equals root path", func(t *testing.T) {
|
2021-02-24 15:31:10 +01:00
|
|
|
folder := NewFolder("", RootPath, time.Now().UTC())
|
2020-07-09 12:24:44 +02:00
|
|
|
assert.Equal(t, "", folder.Path)
|
|
|
|
})
|
2020-05-22 16:29:12 +02:00
|
|
|
}
|
2020-05-28 15:12:18 +02:00
|
|
|
|
|
|
|
func TestFirstOrCreateFolder(t *testing.T) {
|
2020-12-09 13:10:21 +01:00
|
|
|
folder := NewFolder(RootOriginals, RootPath, time.Now().UTC())
|
2020-05-28 15:12:18 +02:00
|
|
|
result := FirstOrCreateFolder(&folder)
|
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
t.Fatal("result should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if folder.FolderTitle != "Originals" {
|
|
|
|
t.Errorf("FolderTitle should be 'Originals'")
|
|
|
|
}
|
|
|
|
|
|
|
|
if folder.FolderCountry != "zz" {
|
|
|
|
t.Errorf("FolderCountry should be 'zz'")
|
|
|
|
}
|
|
|
|
|
2020-06-07 10:09:35 +02:00
|
|
|
found := FindFolder(RootOriginals, RootPath)
|
2020-05-28 15:12:18 +02:00
|
|
|
|
|
|
|
if found == nil {
|
|
|
|
t.Fatal("found should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if found.FolderTitle != "Originals" {
|
|
|
|
t.Errorf("FolderTitle should be 'Originals'")
|
|
|
|
}
|
|
|
|
|
|
|
|
if found.FolderCountry != "zz" {
|
|
|
|
t.Errorf("FolderCountry should be 'zz'")
|
|
|
|
}
|
|
|
|
}
|
2020-07-09 12:24:44 +02:00
|
|
|
|
|
|
|
func TestFolder_SetValuesFromPath(t *testing.T) {
|
|
|
|
t.Run("/", func(t *testing.T) {
|
2020-12-09 13:10:21 +01:00
|
|
|
folder := NewFolder("new", "", time.Now().UTC())
|
2020-07-09 12:24:44 +02:00
|
|
|
folder.SetValuesFromPath()
|
|
|
|
assert.Equal(t, "New", folder.FolderTitle)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFolder_Slug(t *testing.T) {
|
|
|
|
t.Run("/", func(t *testing.T) {
|
2020-12-08 22:40:13 +01:00
|
|
|
folder := Folder{FolderTitle: "Beautiful beach", Root: "sidecar", Path: "ugly/beach"}
|
|
|
|
assert.Equal(t, "ugly-beach", folder.Slug())
|
2020-07-09 12:24:44 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFolder_Title(t *testing.T) {
|
|
|
|
t.Run("/", func(t *testing.T) {
|
|
|
|
folder := Folder{FolderTitle: "Beautiful beach"}
|
|
|
|
assert.Equal(t, "Beautiful beach", folder.Title())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-24 15:31:10 +01:00
|
|
|
func TestFolder_RootPath(t *testing.T) {
|
|
|
|
t.Run("/rainbow", func(t *testing.T) {
|
|
|
|
folder := Folder{FolderTitle: "Beautiful beach", Root: "/", Path: "rainbow"}
|
|
|
|
assert.Equal(t, "/rainbow", folder.RootPath())
|
|
|
|
})
|
|
|
|
}
|
2020-07-09 12:24:44 +02:00
|
|
|
func TestFindFolder(t *testing.T) {
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
assert.Nil(t, FindFolder("vvfgt", "jgfuyf"))
|
|
|
|
})
|
2021-02-24 15:31:10 +01:00
|
|
|
t.Run("pathName === rootPath", func(t *testing.T) {
|
|
|
|
assert.Nil(t, FindFolder("vvfgt", RootPath))
|
|
|
|
})
|
2020-07-09 12:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFolder_Updates(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
2020-12-09 13:10:21 +01:00
|
|
|
folder := NewFolder("oldRoot", "oldPath", time.Now().UTC())
|
2020-07-09 12:24:44 +02:00
|
|
|
|
|
|
|
assert.Equal(t, "oldRoot", folder.Root)
|
|
|
|
assert.Equal(t, "oldPath", folder.Path)
|
|
|
|
|
|
|
|
err := folder.Updates(Folder{Root: "newRoot", Path: "newPath"})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, "newRoot", folder.Root)
|
|
|
|
assert.Equal(t, "newPath", folder.Path)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFolder_SetForm(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
formValues := Folder{FolderTitle: "Beautiful beach"}
|
|
|
|
|
|
|
|
folderForm, err := form.NewFolder(formValues)
|
|
|
|
|
2020-12-09 13:10:21 +01:00
|
|
|
folder := NewFolder("oldRoot", "oldPath", time.Now().UTC())
|
2020-07-09 12:24:44 +02:00
|
|
|
|
|
|
|
assert.Equal(t, "oldRoot", folder.Root)
|
|
|
|
assert.Equal(t, "oldPath", folder.Path)
|
|
|
|
assert.Equal(t, "OldPath", folder.FolderTitle)
|
|
|
|
|
|
|
|
err = folder.SetForm(folderForm)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, "", folder.Root)
|
|
|
|
assert.Equal(t, "", folder.Path)
|
|
|
|
assert.Equal(t, "Beautiful beach", folder.FolderTitle)
|
|
|
|
})
|
|
|
|
}
|
2021-02-24 15:31:10 +01:00
|
|
|
|
|
|
|
func TestFolder_Create(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
folder := Folder{FolderTitle: "Holiday 2020", Root: RootOriginals, Path: "2020/Greece"}
|
|
|
|
err := folder.Create()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
result := FindFolder(RootOriginals, "2020/Greece")
|
|
|
|
assert.Equal(t, "2020-greece", result.Slug())
|
|
|
|
assert.Equal(t, "Holiday 2020", result.Title())
|
|
|
|
})
|
|
|
|
}
|