2013-12-21 23:59:11 +00:00
|
|
|
/*
|
|
|
|
Copyright 2013 The Camlistore Authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package search
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2013-12-24 03:11:55 +00:00
|
|
|
|
|
|
|
"camlistore.org/pkg/context"
|
2013-12-21 23:59:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var parseExprTests = []struct {
|
|
|
|
name string
|
|
|
|
in string
|
|
|
|
inList []string
|
|
|
|
want *SearchQuery
|
|
|
|
errContains string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty search",
|
|
|
|
inList: []string{"", " ", "\n"},
|
|
|
|
want: &SearchQuery{
|
|
|
|
Constraint: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
SkipHidden: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2013-12-23 03:26:49 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
in: "is:pano",
|
|
|
|
want: &SearchQuery{
|
|
|
|
Constraint: &Constraint{
|
|
|
|
Logical: &LogicalConstraint{
|
|
|
|
Op: "and",
|
|
|
|
A: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
SkipHidden: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
B: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
Attr: "camliContent",
|
|
|
|
ValueInSet: &Constraint{
|
|
|
|
File: &FileConstraint{
|
|
|
|
IsImage: true,
|
|
|
|
WHRatio: &FloatConstraint{
|
2014-02-08 22:41:37 +00:00
|
|
|
Min: 2.0,
|
2013-12-23 03:26:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
in: "width:0-640",
|
|
|
|
want: &SearchQuery{
|
|
|
|
Constraint: &Constraint{
|
|
|
|
Logical: &LogicalConstraint{
|
|
|
|
Op: "and",
|
|
|
|
A: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
SkipHidden: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
B: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
Attr: "camliContent",
|
|
|
|
ValueInSet: &Constraint{
|
|
|
|
File: &FileConstraint{
|
|
|
|
IsImage: true,
|
|
|
|
Width: &IntConstraint{
|
|
|
|
ZeroMin: true,
|
|
|
|
Max: 640,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-03-07 21:27:40 +00:00
|
|
|
{
|
2014-03-14 02:01:55 +00:00
|
|
|
name: "tag with spaces",
|
|
|
|
in: `tag:"Foo Bar"`,
|
2014-03-07 21:27:40 +00:00
|
|
|
want: &SearchQuery{
|
|
|
|
Constraint: &Constraint{
|
|
|
|
Logical: &LogicalConstraint{
|
|
|
|
Op: "and",
|
|
|
|
A: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
SkipHidden: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
B: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
Attr: "tag",
|
|
|
|
Value: "Foo Bar",
|
|
|
|
SkipHidden: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2014-03-14 02:01:55 +00:00
|
|
|
name: "attribute search",
|
|
|
|
in: "attr:foo:bar",
|
2014-03-07 21:27:40 +00:00
|
|
|
want: &SearchQuery{
|
|
|
|
Constraint: &Constraint{
|
|
|
|
Logical: &LogicalConstraint{
|
|
|
|
Op: "and",
|
|
|
|
A: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
SkipHidden: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
B: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
Attr: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
SkipHidden: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2014-03-14 02:01:55 +00:00
|
|
|
name: "attribute search with space in value",
|
|
|
|
in: `attr:foo:"fun bar"`,
|
2014-03-07 21:27:40 +00:00
|
|
|
want: &SearchQuery{
|
|
|
|
Constraint: &Constraint{
|
|
|
|
Logical: &LogicalConstraint{
|
|
|
|
Op: "and",
|
|
|
|
A: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
SkipHidden: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
B: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
Attr: "foo",
|
|
|
|
Value: "fun bar",
|
|
|
|
SkipHidden: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2013-12-21 23:59:11 +00:00
|
|
|
{
|
|
|
|
in: "tag:funny",
|
|
|
|
want: &SearchQuery{
|
|
|
|
Constraint: &Constraint{
|
2013-12-22 23:11:31 +00:00
|
|
|
Logical: &LogicalConstraint{
|
|
|
|
Op: "and",
|
|
|
|
A: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
SkipHidden: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
B: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
Attr: "tag",
|
|
|
|
Value: "funny",
|
|
|
|
SkipHidden: true,
|
2014-01-11 01:35:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
in: "title:Doggies",
|
|
|
|
want: &SearchQuery{
|
|
|
|
Constraint: &Constraint{
|
|
|
|
Logical: &LogicalConstraint{
|
|
|
|
Op: "and",
|
|
|
|
A: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
SkipHidden: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
B: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
Attr: "title",
|
|
|
|
ValueMatches: &StringConstraint{
|
|
|
|
Contains: "Doggies",
|
|
|
|
CaseInsensitive: true,
|
|
|
|
},
|
|
|
|
SkipHidden: true,
|
2013-12-22 23:11:31 +00:00
|
|
|
},
|
|
|
|
},
|
2013-12-21 23:59:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2013-12-22 23:11:31 +00:00
|
|
|
|
2014-03-14 02:01:55 +00:00
|
|
|
{
|
|
|
|
in: "childrenof:sha1-f00ba4",
|
|
|
|
want: &SearchQuery{
|
|
|
|
Constraint: &Constraint{
|
|
|
|
Logical: &LogicalConstraint{
|
|
|
|
Op: "and",
|
|
|
|
A: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
SkipHidden: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
B: &Constraint{
|
|
|
|
Permanode: &PermanodeConstraint{
|
|
|
|
Relation: &RelationConstraint{
|
|
|
|
Relation: "parent",
|
|
|
|
Any: &Constraint{
|
|
|
|
BlobRefPrefix: "sha1-f00ba4",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2013-12-21 23:59:11 +00:00
|
|
|
// TODO: at least 'x' will go away eventually.
|
2013-12-22 23:11:31 +00:00
|
|
|
/*
|
|
|
|
{
|
|
|
|
inList: []string{"x", "bogus:operator"},
|
|
|
|
errContains: "unknown expression",
|
|
|
|
},
|
|
|
|
*/
|
2013-12-21 23:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseExpression(t *testing.T) {
|
|
|
|
qj := func(sq *SearchQuery) []byte {
|
|
|
|
v, err := json.MarshalIndent(sq, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
for _, tt := range parseExprTests {
|
|
|
|
ins := tt.inList
|
|
|
|
if len(ins) == 0 {
|
|
|
|
ins = []string{tt.in}
|
|
|
|
}
|
|
|
|
for _, in := range ins {
|
2013-12-24 03:11:55 +00:00
|
|
|
got, err := parseExpression(context.TODO(), in)
|
2013-12-21 23:59:11 +00:00
|
|
|
if err != nil {
|
|
|
|
if tt.errContains != "" && strings.Contains(err.Error(), tt.errContains) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Errorf("parseExpression(%q) error: %v", in, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tt.errContains != "" {
|
|
|
|
t.Errorf("parseExpression(%q) succeeded; want error containing %q", in, tt.errContains)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("parseExpression(%q) got:\n%s\n\nwant:%s\n", in, qj(got), qj(tt.want))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-25 19:04:02 +00:00
|
|
|
|
|
|
|
func TestSplitExpr(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in string
|
|
|
|
want []string
|
|
|
|
}{
|
|
|
|
{"", nil},
|
|
|
|
{"foo", []string{"foo"}},
|
|
|
|
{"foo bar", []string{"foo", "bar"}},
|
|
|
|
{" foo bar ", []string{"foo", "bar"}},
|
|
|
|
{`foo:"quoted string" bar`, []string{`foo:quoted string`, "bar"}},
|
2014-03-07 21:27:40 +00:00
|
|
|
{`foo:"quoted \"-containing"`, []string{`foo:quoted "-containing`}},
|
2014-01-25 19:04:02 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
got := splitExpr(tt.in)
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("split(%s) = %q; want %q", tt.in, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTokenizeExpr(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in string
|
|
|
|
want []string
|
|
|
|
}{
|
|
|
|
{"", nil},
|
|
|
|
{"foo", []string{"foo"}},
|
|
|
|
{"foo bar", []string{"foo", " ", "bar"}},
|
|
|
|
{" foo bar ", []string{" ", "foo", " ", "bar", " "}},
|
|
|
|
{" -foo bar", []string{" ", "-", "foo", " ", "bar"}},
|
|
|
|
{`-"quote"foo`, []string{"-", `"quote"`, "foo"}},
|
|
|
|
{`foo:"quoted string" bar`, []string{"foo:", `"quoted string"`, " ", "bar"}},
|
2014-03-07 21:27:40 +00:00
|
|
|
{`"quoted \"-containing"`, []string{`"quoted \"-containing"`}},
|
2014-01-25 19:04:02 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
got := tokenizeExpr(tt.in)
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("tokens(%s) = %q; want %q", tt.in, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|