photoprism/internal/form/album_search_test.go

105 lines
2.2 KiB
Go
Raw Normal View History

package form
2019-07-16 11:17:52 +02:00
import (
"testing"
2019-07-16 11:17:52 +02:00
"github.com/stretchr/testify/assert"
)
func TestAlbumSearchForm(t *testing.T) {
form := &AlbumSearch{}
2019-07-16 11:17:52 +02:00
assert.IsType(t, new(AlbumSearch), form)
2019-07-16 11:17:52 +02:00
}
func TestParseQueryStringAlbum(t *testing.T) {
t.Run("valid query", func(t *testing.T) {
form := &AlbumSearch{Query: "slug:album1 favorite:true count:10"}
2019-07-16 11:17:52 +02:00
err := form.ParseQueryString()
log.Debugf("%+v\n", form)
2020-05-06 17:05:06 +02:00
if err != nil {
t.Fatal("err should be nil")
}
2019-07-16 11:17:52 +02:00
assert.Equal(t, "album1", form.Slug)
assert.Equal(t, true, form.Favorite)
2019-07-16 11:17:52 +02:00
assert.Equal(t, 10, form.Count)
})
t.Run("valid query 2", func(t *testing.T) {
form := &AlbumSearch{Query: "title:album1 favorite:false offset:100 order:newest query:\"query text\""}
2019-07-16 11:17:52 +02:00
err := form.ParseQueryString()
log.Debugf("%+v\n", form)
if err != nil {
t.Fatal("err should be nil")
}
assert.Equal(t, "album1", form.Title)
assert.Equal(t, false, form.Favorite)
2019-07-16 11:17:52 +02:00
assert.Equal(t, 100, form.Offset)
assert.Equal(t, "newest", form.Order)
assert.Equal(t, "query text", form.Query)
})
t.Run("valid query with umlauts", func(t *testing.T) {
form := &AlbumSearch{Query: "query:\"tübingen\""}
err := form.ParseQueryString()
log.Debugf("%+v\n", form)
if err != nil {
t.Fatal("err should be nil")
}
assert.Equal(t, "tübingen", form.Query)
})
2019-07-16 11:17:52 +02:00
t.Run("query for invalid filter", func(t *testing.T) {
form := &AlbumSearch{Query: "xxx:false"}
2019-07-16 11:17:52 +02:00
err := form.ParseQueryString()
if err == nil {
t.FailNow()
}
2019-07-16 11:17:52 +02:00
log.Debugf("%+v\n", form)
assert.Equal(t, "unknown filter: Xxx", err.Error())
})
t.Run("query for favorites with uncommon bool value", func(t *testing.T) {
form := &AlbumSearch{Query: "favorite:cat"}
2019-07-16 11:17:52 +02:00
err := form.ParseQueryString()
if err != nil {
t.Fatal("err should be nil")
}
2019-07-16 11:17:52 +02:00
assert.True(t, form.Favorite)
2019-07-16 11:17:52 +02:00
})
t.Run("query for count with invalid type", func(t *testing.T) {
form := &AlbumSearch{Query: "count:cat"}
2019-07-16 11:17:52 +02:00
err := form.ParseQueryString()
if err == nil {
t.FailNow()
}
2019-07-16 11:17:52 +02:00
log.Debugf("%+v\n", form)
assert.Equal(t, "strconv.Atoi: parsing \"cat\": invalid syntax", err.Error())
})
}
2020-05-06 17:05:06 +02:00
func TestNewAlbumSearch(t *testing.T) {
r := NewAlbumSearch("holiday")
assert.IsType(t, AlbumSearch{}, r)
}