2019-12-05 19:21:35 +01:00
|
|
|
package form
|
2019-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
import (
|
2019-12-11 14:10:20 +01:00
|
|
|
"testing"
|
|
|
|
|
2019-07-16 11:18:30 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLabelSearchForm(t *testing.T) {
|
2019-12-05 19:21:35 +01:00
|
|
|
form := &LabelSearch{}
|
2019-07-16 11:18:30 +02:00
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
assert.IsType(t, new(LabelSearch), form)
|
2019-07-16 11:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseQueryStringLabel(t *testing.T) {
|
|
|
|
|
|
|
|
t.Run("valid query", func(t *testing.T) {
|
2020-05-14 19:03:12 +02:00
|
|
|
form := &LabelSearch{Query: "name:cat favorite:true count:10 all:false query:\"query text\""}
|
2019-07-16 11:18:30 +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:18:30 +02:00
|
|
|
assert.Equal(t, "cat", form.Name)
|
2020-05-14 19:03:12 +02:00
|
|
|
assert.Equal(t, true, form.Favorite)
|
2019-07-16 11:18:30 +02:00
|
|
|
assert.Equal(t, 10, form.Count)
|
2019-12-17 07:13:09 +01:00
|
|
|
assert.Equal(t, false, form.All)
|
2019-07-16 11:18:30 +02:00
|
|
|
assert.Equal(t, "query text", form.Query)
|
|
|
|
})
|
|
|
|
t.Run("valid query 2", func(t *testing.T) {
|
2020-05-14 19:03:12 +02:00
|
|
|
form := &LabelSearch{Query: "slug:cat favorite:false offset:2 order:oldest"}
|
2019-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
|
|
|
log.Debugf("%+v\n", form)
|
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("err should be nil")
|
|
|
|
}
|
2019-07-16 11:18:30 +02:00
|
|
|
assert.Equal(t, "cat", form.Slug)
|
2020-05-14 19:03:12 +02:00
|
|
|
assert.Equal(t, false, form.Favorite)
|
2019-07-16 11:18:30 +02:00
|
|
|
assert.Equal(t, 2, form.Offset)
|
|
|
|
assert.Equal(t, "oldest", form.Order)
|
|
|
|
})
|
2019-12-15 13:24:10 +01:00
|
|
|
t.Run("valid query with umlauts", func(t *testing.T) {
|
|
|
|
form := &LabelSearch{Query: "query:\"tübingen\""}
|
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
|
|
|
log.Debugf("%+v\n", form)
|
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("err should be nil")
|
|
|
|
}
|
|
|
|
|
2019-12-15 13:24:10 +01:00
|
|
|
assert.Equal(t, "tübingen", form.Query)
|
|
|
|
})
|
2019-07-16 11:18:30 +02:00
|
|
|
t.Run("query for invalid filter", func(t *testing.T) {
|
2019-12-05 19:21:35 +01:00
|
|
|
form := &LabelSearch{Query: "xxx:false"}
|
2019-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("err should NOT be nil")
|
|
|
|
}
|
|
|
|
|
2019-07-16 11:18:30 +02:00
|
|
|
log.Debugf("%+v\n", form)
|
|
|
|
|
|
|
|
assert.Equal(t, "unknown filter: Xxx", err.Error())
|
|
|
|
})
|
2020-01-31 15:29:06 +01:00
|
|
|
t.Run("query for favorites with uncommon bool value", func(t *testing.T) {
|
2020-05-14 19:03:12 +02:00
|
|
|
form := &LabelSearch{Query: "favorite:0.99"}
|
2019-07-16 11:18:30 +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:18:30 +02:00
|
|
|
|
2020-05-14 19:03:12 +02:00
|
|
|
assert.True(t, form.Favorite)
|
2019-07-16 11:18:30 +02:00
|
|
|
})
|
|
|
|
t.Run("query for count with invalid type", func(t *testing.T) {
|
2019-12-15 13:24:10 +01:00
|
|
|
form := &LabelSearch{Query: "count:2019-01-15"}
|
2019-07-16 11:18:30 +02:00
|
|
|
|
|
|
|
err := form.ParseQueryString()
|
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("err should NOT be nil")
|
|
|
|
}
|
|
|
|
|
2019-07-16 11:18:30 +02:00
|
|
|
log.Debugf("%+v\n", form)
|
|
|
|
|
2019-12-15 13:24:10 +01:00
|
|
|
assert.Equal(t, "strconv.Atoi: parsing \"2019-01-15\": invalid syntax", err.Error())
|
2019-07-16 11:18:30 +02:00
|
|
|
})
|
|
|
|
}
|
2020-05-06 17:05:06 +02:00
|
|
|
|
|
|
|
func TestNewLabelSearch(t *testing.T) {
|
|
|
|
r := NewLabelSearch("cat")
|
|
|
|
assert.IsType(t, LabelSearch{}, r)
|
|
|
|
}
|