photoprism/internal/form/search_labels_test.go

104 lines
2.2 KiB
Go
Raw Normal View History

package form
2019-07-16 11:18:30 +02:00
import (
"testing"
2019-07-16 11:18:30 +02:00
"github.com/stretchr/testify/assert"
)
func TestLabelSearchForm(t *testing.T) {
form := &SearchLabels{}
2019-07-16 11:18:30 +02:00
assert.IsType(t, new(SearchLabels), form)
2019-07-16 11:18:30 +02:00
}
func TestParseQueryStringLabel(t *testing.T) {
t.Run("valid query", func(t *testing.T) {
form := &SearchLabels{Query: "name:cat favorite:true all:false q:\"query text\""}
2019-07-16 11:18:30 +02:00
err := form.ParseQueryString()
// log.Debugf("%+v\n", form)
2019-07-16 11:18:30 +02:00
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)
assert.Equal(t, true, form.Favorite)
assert.Equal(t, 0, form.Count)
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) {
form := &SearchLabels{Query: "slug:cat favorite:false"}
2019-07-16 11:18:30 +02:00
err := form.ParseQueryString()
// log.Debugf("%+v\n", form)
2019-07-16 11:18:30 +02:00
if err != nil {
t.Fatal("err should be nil")
}
2019-07-16 11:18:30 +02:00
assert.Equal(t, "cat", form.Slug)
assert.Equal(t, false, form.Favorite)
assert.Equal(t, 0, form.Count)
assert.Equal(t, 0, form.Offset)
assert.Equal(t, "", form.Order)
2019-07-16 11:18:30 +02:00
})
t.Run("valid query with umlauts", func(t *testing.T) {
form := &SearchLabels{Query: "q:\"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:18:30 +02:00
t.Run("query for invalid filter", func(t *testing.T) {
form := &SearchLabels{Query: "xxx:false"}
2019-07-16 11:18:30 +02:00
err := form.ParseQueryString()
if err == nil {
t.Fatal("err should NOT be nil")
}
// log.Debugf("%+v\n", form)
2019-07-16 11:18:30 +02:00
assert.Equal(t, "unknown filter: xxx", err.Error())
2019-07-16 11:18:30 +02:00
})
t.Run("query for favorites with uncommon bool value", func(t *testing.T) {
form := &SearchLabels{Query: "favorite:0.99"}
2019-07-16 11:18:30 +02:00
err := form.ParseQueryString()
if err != nil {
t.Fatal("err should be nil")
}
2019-07-16 11:18:30 +02:00
assert.False(t, form.Favorite)
2019-07-16 11:18:30 +02:00
})
t.Run("query for count with invalid type", func(t *testing.T) {
form := &SearchLabels{Query: "all:2019-01-15"}
2019-07-16 11:18:30 +02:00
err := form.ParseQueryString()
if err != nil {
t.Fatal(err)
}
assert.True(t, form.All)
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, SearchLabels{}, r)
2020-05-06 17:05:06 +02:00
}