2019-12-05 19:21:35 +01:00
|
|
|
package form
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-05-15 23:07:25 +02:00
|
|
|
"time"
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-05-15 21:51:00 +02:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2019-05-03 18:57:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPhotoSearchForm(t *testing.T) {
|
2019-12-05 19:21:35 +01:00
|
|
|
form := &PhotoSearch{}
|
2019-05-03 18:57:28 +02:00
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
assert.IsType(t, new(PhotoSearch), form)
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
2019-05-15 21:51:00 +02:00
|
|
|
|
|
|
|
func TestParseQueryString(t *testing.T) {
|
|
|
|
|
2019-07-16 11:17:33 +02:00
|
|
|
t.Run("valid query", func(t *testing.T) {
|
2020-05-14 19:03:12 +02:00
|
|
|
form := &PhotoSearch{Query: "label:cat query:\"fooBar baz\" before:2019-01-15 camera:23 favorite:false dist:25000 lat:33.45343166666667"}
|
2019-05-15 21:51:00 +02:00
|
|
|
|
2019-07-16 10:41:30 +02:00
|
|
|
err := form.ParseQueryString()
|
2019-05-15 21:51:00 +02:00
|
|
|
|
2019-07-16 10:41:30 +02:00
|
|
|
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 10:41:30 +02:00
|
|
|
assert.Equal(t, "cat", form.Label)
|
|
|
|
assert.Equal(t, "foobar baz", form.Query)
|
|
|
|
assert.Equal(t, 23, form.Camera)
|
|
|
|
assert.Equal(t, time.Date(2019, 01, 15, 0, 0, 0, 0, time.UTC), form.Before)
|
2020-05-14 19:03:12 +02:00
|
|
|
assert.Equal(t, false, form.Favorite)
|
2019-07-16 10:41:30 +02:00
|
|
|
assert.Equal(t, uint(0x61a8), form.Dist)
|
2020-04-26 11:41:54 +02:00
|
|
|
assert.Equal(t, float32(33.45343), form.Lat)
|
2019-07-16 10:41:30 +02:00
|
|
|
})
|
2019-07-16 11:17:33 +02:00
|
|
|
t.Run("valid query 2", func(t *testing.T) {
|
2020-05-14 19:03:12 +02:00
|
|
|
form := &PhotoSearch{Query: "chroma:200 title:\"test\" after:2018-01-15 duplicate:false favorite:true lng:33.45343166666667"}
|
2019-07-16 11:17:33 +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")
|
|
|
|
}
|
|
|
|
|
2020-04-11 12:14:37 +02:00
|
|
|
assert.Equal(t, uint8(200), form.Chroma)
|
2020-03-26 09:41:33 +01:00
|
|
|
assert.Equal(t, "test", form.Title)
|
2019-07-16 11:17:33 +02:00
|
|
|
assert.Equal(t, time.Date(2018, 01, 15, 0, 0, 0, 0, time.UTC), form.After)
|
|
|
|
assert.Equal(t, false, form.Duplicate)
|
2020-04-26 11:41:54 +02:00
|
|
|
assert.Equal(t, float32(33.45343), form.Lng)
|
2019-07-16 11:17:33 +02:00
|
|
|
})
|
2019-12-15 13:23:45 +01:00
|
|
|
t.Run("valid query with umlauts", func(t *testing.T) {
|
2020-03-26 09:41:33 +01:00
|
|
|
form := &PhotoSearch{Query: "title:\"tübingen\""}
|
2019-12-15 13:23:45 +01: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")
|
|
|
|
}
|
|
|
|
|
2020-03-26 09:41:33 +01:00
|
|
|
assert.Equal(t, "tübingen", form.Title)
|
2019-12-15 13:23:45 +01:00
|
|
|
})
|
2019-07-16 10:41:30 +02:00
|
|
|
t.Run("query for invalid filter", func(t *testing.T) {
|
2019-12-05 19:21:35 +01:00
|
|
|
form := &PhotoSearch{Query: "xxx:false"}
|
2019-07-16 10:41: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 10:41: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 := &PhotoSearch{Query: "favorite:cat"}
|
2019-07-16 10:41: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 10:41:30 +02:00
|
|
|
|
2020-05-14 19:03:12 +02:00
|
|
|
assert.True(t, form.Favorite)
|
2019-07-16 10:41:30 +02:00
|
|
|
})
|
|
|
|
t.Run("query for lat with invalid type", func(t *testing.T) {
|
2019-12-05 19:21:35 +01:00
|
|
|
form := &PhotoSearch{Query: "lat:cat"}
|
2019-07-16 10:41: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 10:41:30 +02:00
|
|
|
log.Debugf("%+v\n", form)
|
|
|
|
|
|
|
|
assert.Equal(t, "strconv.ParseFloat: parsing \"cat\": invalid syntax", err.Error())
|
|
|
|
})
|
|
|
|
t.Run("query for dist with invalid type", func(t *testing.T) {
|
2019-12-05 19:21:35 +01:00
|
|
|
form := &PhotoSearch{Query: "dist:cat"}
|
2019-07-16 10:41: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 10:41:30 +02:00
|
|
|
log.Debugf("%+v\n", form)
|
|
|
|
|
|
|
|
assert.Equal(t, "strconv.Atoi: parsing \"cat\": invalid syntax", err.Error())
|
|
|
|
})
|
|
|
|
t.Run("query for camera with invalid type", func(t *testing.T) {
|
2019-12-05 19:21:35 +01:00
|
|
|
form := &PhotoSearch{Query: "camera:cat"}
|
2019-07-16 10:41: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 10:41:30 +02:00
|
|
|
log.Debugf("%+v\n", form)
|
|
|
|
|
|
|
|
assert.Equal(t, "strconv.Atoi: parsing \"cat\": invalid syntax", err.Error())
|
|
|
|
})
|
|
|
|
t.Run("query for before with invalid type", func(t *testing.T) {
|
2019-12-05 19:21:35 +01:00
|
|
|
form := &PhotoSearch{Query: "before:cat"}
|
2019-07-16 10:41: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 10:41:30 +02:00
|
|
|
log.Debugf("%+v\n", form)
|
|
|
|
|
|
|
|
assert.Equal(t, "Could not find format for \"cat\"", err.Error())
|
|
|
|
})
|
2019-05-15 21:51:00 +02:00
|
|
|
}
|
2020-05-06 17:05:06 +02:00
|
|
|
|
|
|
|
func TestNewPhotoSearch(t *testing.T) {
|
|
|
|
r := NewPhotoSearch("cat")
|
|
|
|
assert.IsType(t, PhotoSearch{}, r)
|
|
|
|
}
|