2020-03-14 18:47:38 +03:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTokenize(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
input string
|
|
|
|
tokens []token
|
|
|
|
}{
|
|
|
|
{"status:", nil},
|
|
|
|
{":value", nil},
|
|
|
|
|
2020-08-20 13:00:34 +03:00
|
|
|
{"status:open", []token{newTokenKV("status", "open")}},
|
|
|
|
{"status:closed", []token{newTokenKV("status", "closed")}},
|
2020-03-14 18:47:38 +03:00
|
|
|
|
2020-08-20 13:00:34 +03:00
|
|
|
{"author:rene", []token{newTokenKV("author", "rene")}},
|
|
|
|
{`author:"René Descartes"`, []token{newTokenKV("author", "René Descartes")}},
|
2020-03-14 18:47:38 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
`status:open status:closed author:rene author:"René Descartes"`,
|
|
|
|
[]token{
|
2020-08-20 13:00:34 +03:00
|
|
|
newTokenKV("status", "open"),
|
|
|
|
newTokenKV("status", "closed"),
|
|
|
|
newTokenKV("author", "rene"),
|
|
|
|
newTokenKV("author", "René Descartes"),
|
2020-03-14 18:47:38 +03:00
|
|
|
},
|
|
|
|
},
|
2020-03-28 21:22:27 +03:00
|
|
|
|
|
|
|
// quotes
|
2020-08-20 13:00:34 +03:00
|
|
|
{`key:"value value"`, []token{newTokenKV("key", "value value")}},
|
|
|
|
{`key:'value value'`, []token{newTokenKV("key", "value value")}},
|
2020-03-28 21:22:27 +03:00
|
|
|
// unmatched quotes
|
|
|
|
{`key:'value value`, nil},
|
|
|
|
{`key:value value'`, nil},
|
2020-08-20 13:00:34 +03:00
|
|
|
|
|
|
|
// full text search
|
|
|
|
{"search", []token{newTokenSearch("search")}},
|
|
|
|
{"search more terms", []token{
|
|
|
|
newTokenSearch("search"),
|
|
|
|
newTokenSearch("more"),
|
|
|
|
newTokenSearch("terms"),
|
|
|
|
}},
|
|
|
|
{"search \"more terms\"", []token{
|
|
|
|
newTokenSearch("search"),
|
|
|
|
newTokenSearch("more terms"),
|
|
|
|
}},
|
2020-03-14 18:47:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tokens, err := tokenize(tc.input)
|
|
|
|
if tc.tokens == nil {
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, tokens)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.tokens, tokens)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|