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
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAlbumSearchForm(t *testing.T) {
|
2021-11-26 14:28:50 +01:00
|
|
|
form := &SearchAlbums{}
|
2019-07-16 11:17:52 +02:00
|
|
|
|
2021-11-26 14:28:50 +01:00
|
|
|
assert.IsType(t, new(SearchAlbums), form)
|
2019-07-16 11:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseQueryStringAlbum(t *testing.T) {
|
|
|
|
t.Run("valid query", func(t *testing.T) {
|
2023-01-02 16:17:59 +01:00
|
|
|
form := &SearchAlbums{Query: "slug:album1 favorite:true", Year: "2020"}
|
2019-07-16 11:17:52 +02:00
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
2021-08-29 16:16:49 +02:00
|
|
|
// log.Debugf("%+v\n", form)
|
2019-07-16 11:17:52 +02:00
|
|
|
|
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)
|
2020-05-14 19:03:12 +02:00
|
|
|
assert.Equal(t, true, form.Favorite)
|
2023-01-02 16:17:59 +01:00
|
|
|
assert.Equal(t, "2020", form.Year)
|
2022-08-01 15:57:19 +02:00
|
|
|
assert.Equal(t, 0, form.Count)
|
2019-07-16 11:17:52 +02:00
|
|
|
})
|
|
|
|
t.Run("valid query 2", func(t *testing.T) {
|
2022-08-01 15:57:19 +02:00
|
|
|
form := &SearchAlbums{Query: "title:album1 favorite:false q:\"query text\""}
|
2019-07-16 11:17:52 +02:00
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
2021-08-29 16:16:49 +02:00
|
|
|
// log.Debugf("%+v\n", form)
|
2019-07-16 11:17:52 +02:00
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("err should be nil")
|
|
|
|
}
|
|
|
|
|
2020-05-26 09:02:19 +02:00
|
|
|
assert.Equal(t, "album1", form.Title)
|
2020-05-14 19:03:12 +02:00
|
|
|
assert.Equal(t, false, form.Favorite)
|
2023-01-02 16:17:59 +01:00
|
|
|
assert.Equal(t, "", form.Year)
|
2022-08-01 15:57:19 +02:00
|
|
|
assert.Equal(t, 0, form.Offset)
|
|
|
|
assert.Equal(t, "", form.Order)
|
2019-07-16 11:17:52 +02:00
|
|
|
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) {
|
2023-01-02 16:17:59 +01:00
|
|
|
form := &SearchAlbums{Query: "q:\"tübingen\" year:1999"}
|
2019-12-15 13:24:27 +01:00
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
2021-08-29 16:16:49 +02:00
|
|
|
// log.Debugf("%+v\n", form)
|
2019-12-15 13:24:27 +01:00
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("err should be nil")
|
|
|
|
}
|
|
|
|
|
2019-12-15 13:24:27 +01:00
|
|
|
assert.Equal(t, "tübingen", form.Query)
|
2023-01-02 16:17:59 +01:00
|
|
|
assert.Equal(t, "1999", form.Year)
|
2019-12-15 13:24:27 +01:00
|
|
|
})
|
2019-07-16 11:17:52 +02:00
|
|
|
t.Run("query for invalid filter", func(t *testing.T) {
|
2021-11-26 14:28:50 +01:00
|
|
|
form := &SearchAlbums{Query: "xxx:false"}
|
2019-07-16 11:17:52 +02:00
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
if err == nil {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2021-08-29 16:16:49 +02:00
|
|
|
// log.Debugf("%+v\n", form)
|
2019-07-16 11:17:52 +02:00
|
|
|
|
2022-08-01 15:57:19 +02:00
|
|
|
assert.Equal(t, "unknown filter: xxx", err.Error())
|
2019-07-16 11:17:52 +02:00
|
|
|
})
|
2020-01-31 15:29:06 +01:00
|
|
|
t.Run("query for favorites with uncommon bool value", func(t *testing.T) {
|
2021-11-26 14:28:50 +01:00
|
|
|
form := &SearchAlbums{Query: "favorite:cat"}
|
2019-07-16 11:17:52 +02:00
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("err should be nil")
|
|
|
|
}
|
2019-07-16 11:17:52 +02:00
|
|
|
|
2020-05-14 19:03:12 +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) {
|
2021-11-26 14:28:50 +01:00
|
|
|
form := &SearchAlbums{Query: "count:cat"}
|
2019-07-16 11:17:52 +02:00
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
2022-08-01 15:57:19 +02:00
|
|
|
assert.Error(t, err)
|
2019-07-16 11:17:52 +02:00
|
|
|
})
|
|
|
|
}
|
2020-05-06 17:05:06 +02:00
|
|
|
|
|
|
|
func TestNewAlbumSearch(t *testing.T) {
|
|
|
|
r := NewAlbumSearch("holiday")
|
2021-11-26 14:28:50 +01:00
|
|
|
assert.IsType(t, SearchAlbums{}, r)
|
2020-05-06 17:05:06 +02:00
|
|
|
}
|