2019-12-05 19:21:35 +01:00
|
|
|
package form
|
2019-07-16 11:17:52 +02:00
|
|
|
|
|
|
|
import (
|
2019-12-11 14:10:20 +01:00
|
|
|
"testing"
|
|
|
|
|
2019-07-16 11:17:52 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAlbumSearchForm(t *testing.T) {
|
2019-12-05 19:21:35 +01:00
|
|
|
form := &AlbumSearch{}
|
2019-07-16 11:17:52 +02:00
|
|
|
|
2019-12-05 19:21:35 +01: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) {
|
2019-12-05 19:21:35 +01:00
|
|
|
form := &AlbumSearch{Query: "slug:album1 favorites:true count:10"}
|
2019-07-16 11:17:52 +02:00
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
|
|
|
log.Debugf("%+v\n", form)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "album1", form.Slug)
|
|
|
|
assert.Equal(t, true, form.Favorites)
|
|
|
|
assert.Equal(t, 10, form.Count)
|
|
|
|
})
|
|
|
|
t.Run("valid query 2", func(t *testing.T) {
|
2019-12-05 19:21:35 +01:00
|
|
|
form := &AlbumSearch{Query: "name:album1 favorites:false offset:100 order:newest query:\"query text\""}
|
2019-07-16 11:17:52 +02:00
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
|
|
|
log.Debugf("%+v\n", form)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "album1", form.Name)
|
|
|
|
assert.Equal(t, false, form.Favorites)
|
|
|
|
assert.Equal(t, 100, form.Offset)
|
|
|
|
assert.Equal(t, "newest", form.Order)
|
|
|
|
assert.Equal(t, "query text", form.Query)
|
|
|
|
})
|
2019-12-15 13:24:27 +01:00
|
|
|
t.Run("valid query with umlauts", func(t *testing.T) {
|
|
|
|
form := &AlbumSearch{Query: "query:\"tübingen\""}
|
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
|
|
|
log.Debugf("%+v\n", form)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
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) {
|
2019-12-05 19:21:35 +01:00
|
|
|
form := &AlbumSearch{Query: "xxx:false"}
|
2019-07-16 11:17:52 +02:00
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
|
|
|
log.Debugf("%+v\n", form)
|
|
|
|
|
|
|
|
assert.Equal(t, "unknown filter: Xxx", err.Error())
|
|
|
|
})
|
|
|
|
t.Run("query for favorites with invalid type", func(t *testing.T) {
|
2019-12-05 19:21:35 +01:00
|
|
|
form := &AlbumSearch{Query: "favorites:cat"}
|
2019-07-16 11:17:52 +02:00
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
|
|
|
log.Debugf("%+v\n", form)
|
|
|
|
|
|
|
|
assert.Equal(t, "not a bool value: Favorites", err.Error())
|
|
|
|
})
|
|
|
|
t.Run("query for count with invalid type", func(t *testing.T) {
|
2019-12-05 19:21:35 +01:00
|
|
|
form := &AlbumSearch{Query: "count:cat"}
|
2019-07-16 11:17:52 +02:00
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
|
|
|
log.Debugf("%+v\n", form)
|
|
|
|
|
|
|
|
assert.Equal(t, "strconv.Atoi: parsing \"cat\": invalid syntax", err.Error())
|
|
|
|
})
|
|
|
|
}
|