2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2019-07-16 12:44:13 +02:00
|
|
|
|
|
|
|
import (
|
2020-05-07 17:29:35 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2019-07-16 12:44:13 +02:00
|
|
|
"testing"
|
2019-12-04 12:11:11 +01:00
|
|
|
"time"
|
2019-12-11 14:10:20 +01:00
|
|
|
|
|
|
|
"github.com/gosimple/slug"
|
2020-04-26 14:31:33 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-12-11 14:10:20 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-07-16 12:44:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewAlbum(t *testing.T) {
|
|
|
|
t.Run("name Christmas 2018", func(t *testing.T) {
|
2020-05-29 12:21:17 +02:00
|
|
|
album := NewAlbum("Christmas 2018", TypeAlbum)
|
2020-05-26 09:02:19 +02:00
|
|
|
assert.Equal(t, "Christmas 2018", album.AlbumTitle)
|
2019-07-16 12:44:13 +02:00
|
|
|
assert.Equal(t, "christmas-2018", album.AlbumSlug)
|
|
|
|
})
|
|
|
|
t.Run("name empty", func(t *testing.T) {
|
2020-05-29 12:21:17 +02:00
|
|
|
album := NewAlbum("", TypeAlbum)
|
2019-12-04 12:11:11 +01:00
|
|
|
|
|
|
|
defaultName := time.Now().Format("January 2006")
|
|
|
|
defaultSlug := slug.Make(defaultName)
|
|
|
|
|
2020-05-26 09:02:19 +02:00
|
|
|
assert.Equal(t, defaultName, album.AlbumTitle)
|
2019-12-04 12:11:11 +01:00
|
|
|
assert.Equal(t, defaultSlug, album.AlbumSlug)
|
2019-07-16 12:44:13 +02:00
|
|
|
})
|
|
|
|
}
|
2019-12-17 18:25:13 +01:00
|
|
|
|
2020-04-26 14:31:33 +02:00
|
|
|
func TestAlbum_SetName(t *testing.T) {
|
2019-12-17 18:25:13 +01:00
|
|
|
t.Run("valid name", func(t *testing.T) {
|
2020-05-29 12:21:17 +02:00
|
|
|
album := NewAlbum("initial name", TypeAlbum)
|
2020-05-26 09:02:19 +02:00
|
|
|
assert.Equal(t, "initial name", album.AlbumTitle)
|
2019-12-17 18:25:13 +01:00
|
|
|
assert.Equal(t, "initial-name", album.AlbumSlug)
|
2020-05-26 09:02:19 +02:00
|
|
|
album.SetTitle("New Album Name")
|
|
|
|
assert.Equal(t, "New Album Name", album.AlbumTitle)
|
2019-12-17 18:25:13 +01:00
|
|
|
assert.Equal(t, "new-album-name", album.AlbumSlug)
|
|
|
|
})
|
|
|
|
t.Run("empty name", func(t *testing.T) {
|
2020-05-29 12:21:17 +02:00
|
|
|
album := NewAlbum("initial name", TypeAlbum)
|
2020-05-26 09:02:19 +02:00
|
|
|
assert.Equal(t, "initial name", album.AlbumTitle)
|
2019-12-17 18:25:13 +01:00
|
|
|
assert.Equal(t, "initial-name", album.AlbumSlug)
|
2020-04-26 14:31:33 +02:00
|
|
|
|
2020-05-26 09:02:19 +02:00
|
|
|
album.SetTitle("")
|
2020-04-26 14:31:33 +02:00
|
|
|
expected := album.CreatedAt.Format("January 2006")
|
2020-05-26 09:02:19 +02:00
|
|
|
assert.Equal(t, expected, album.AlbumTitle)
|
2020-04-26 14:31:33 +02:00
|
|
|
assert.Equal(t, slug.Make(expected), album.AlbumSlug)
|
|
|
|
})
|
|
|
|
t.Run("long name", func(t *testing.T) {
|
|
|
|
longName := `A value in decimal degrees to a precision of 4 decimal places is precise to 11.132 meters at the
|
|
|
|
equator. A value in decimal degrees to 5 decimal places is precise to 1.1132 meter at the equator. Elevation also
|
|
|
|
introduces a small error. At 6,378 m elevation, the radius and surface distance is increased by 0.001 or 0.1%.
|
|
|
|
Because the earth is not flat, the precision of the longitude part of the coordinates increases
|
|
|
|
the further from the equator you get. The precision of the latitude part does not increase so much,
|
|
|
|
more strictly however, a meridian arc length per 1 second depends on the latitude at the point in question.
|
|
|
|
The discrepancy of 1 second meridian arc length between equator and pole is about 0.3 metres because the earth
|
|
|
|
is an oblate spheroid.`
|
|
|
|
expected := txt.Clip(longName, txt.ClipDefault)
|
|
|
|
slugExpected := txt.Clip(longName, txt.ClipSlug)
|
2020-05-29 12:21:17 +02:00
|
|
|
album := NewAlbum(longName, TypeAlbum)
|
2020-05-26 09:02:19 +02:00
|
|
|
assert.Equal(t, expected, album.AlbumTitle)
|
2020-04-26 14:31:33 +02:00
|
|
|
assert.Contains(t, album.AlbumSlug, slug.Make(slugExpected))
|
2019-12-17 18:25:13 +01:00
|
|
|
})
|
|
|
|
}
|
2020-05-07 17:29:35 +02:00
|
|
|
|
|
|
|
func TestAlbum_Save(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
2020-05-29 12:21:17 +02:00
|
|
|
album := NewAlbum("Old Name", TypeAlbum)
|
2020-05-07 17:29:35 +02:00
|
|
|
|
2020-05-26 09:02:19 +02:00
|
|
|
assert.Equal(t, "Old Name", album.AlbumTitle)
|
2020-05-07 17:29:35 +02:00
|
|
|
assert.Equal(t, "old-name", album.AlbumSlug)
|
|
|
|
|
2020-05-26 09:02:19 +02:00
|
|
|
album2 := Album{ID: 123, AlbumTitle: "New name", AlbumDescription: "new description"}
|
2020-05-07 17:29:35 +02:00
|
|
|
|
|
|
|
albumForm, err := form.NewAlbum(album2)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-05-08 19:34:29 +02:00
|
|
|
t.Fatal(err)
|
2020-05-07 17:29:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
err = album.SaveForm(albumForm)
|
2020-05-07 17:29:35 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-05-08 19:34:29 +02:00
|
|
|
t.Fatal(err)
|
2020-05-07 17:29:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 09:02:19 +02:00
|
|
|
assert.Equal(t, "New name", album.AlbumTitle)
|
2020-05-07 17:29:35 +02:00
|
|
|
assert.Equal(t, "new description", album.AlbumDescription)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2020-06-01 09:45:24 +02:00
|
|
|
|
|
|
|
func TestAddPhotoToAlbums(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
err := AddPhotoToAlbums("pt9jtxrexxvl0yh0", []string{"at6axuzitogaaiax"})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
a := Album{AlbumUID: "at6axuzitogaaiax"}
|
|
|
|
|
|
|
|
if err := a.Find(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var entries []PhotoAlbum
|
|
|
|
|
|
|
|
if err := Db().Where("album_uid = ? AND photo_uid = ?", "at6axuzitogaaiax", "pt9jtxrexxvl0yh0").Find(&entries).Error; err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entries) < 1 {
|
|
|
|
t.Error("at least one album entry expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
// t.Logf("photo album entries: %+v", entries)
|
|
|
|
})
|
|
|
|
}
|