// Copyright (c) 2014 Couchbase, Inc. // 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 bleve import ( "reflect" "testing" ) func TestQuerySyntaxParserValid(t *testing.T) { fivePointOh := 5.0 theTruth := true theFalsehood := false tests := []struct { input string result Query mapping *IndexMapping }{ { input: "test", mapping: NewIndexMapping(), result: NewBooleanQuery( nil, []Query{ NewMatchQuery("test"), }, nil), }, { input: `"test phrase 1"`, mapping: NewIndexMapping(), result: NewBooleanQuery( nil, []Query{ NewMatchPhraseQuery("test phrase 1"), }, nil), }, { input: "field:test", mapping: NewIndexMapping(), result: NewBooleanQuery( nil, []Query{ NewMatchQuery("test").SetField("field"), }, nil), }, // - is allowed inside a term, just not the start { input: "field:t-est", mapping: NewIndexMapping(), result: NewBooleanQuery( nil, []Query{ NewMatchQuery("t-est").SetField("field"), }, nil), }, // + is allowed inside a term, just not the start { input: "field:t+est", mapping: NewIndexMapping(), result: NewBooleanQuery( nil, []Query{ NewMatchQuery("t+est").SetField("field"), }, nil), }, // > is allowed inside a term, just not the start { input: "field:t>est", mapping: NewIndexMapping(), result: NewBooleanQuery( nil, []Query{ NewMatchQuery("t>est").SetField("field"), }, nil), }, // < is allowed inside a term, just not the start { input: "field:t5`, mapping: NewIndexMapping(), result: NewBooleanQuery( nil, []Query{ NewNumericRangeInclusiveQuery(&fivePointOh, nil, &theFalsehood, nil).SetField("field"), }, nil), }, { input: `field:>=5`, mapping: NewIndexMapping(), result: NewBooleanQuery( nil, []Query{ NewNumericRangeInclusiveQuery(&fivePointOh, nil, &theTruth, nil).SetField("field"), }, nil), }, { input: `field:<5`, mapping: NewIndexMapping(), result: NewBooleanQuery( nil, []Query{ NewNumericRangeInclusiveQuery(nil, &fivePointOh, nil, &theFalsehood).SetField("field"), }, nil), }, { input: `field:<=5`, mapping: NewIndexMapping(), result: NewBooleanQuery( nil, []Query{ NewNumericRangeInclusiveQuery(nil, &fivePointOh, nil, &theTruth).SetField("field"), }, nil), }, } // turn on lexer debugging // debugLexer = true // logger = log.New(os.Stderr, "bleve", log.LstdFlags) for _, test := range tests { q, err := parseQuerySyntax(test.input, test.mapping) if err != nil { t.Error(err) } if !reflect.DeepEqual(q, test.result) { t.Errorf("Expected %#v, got %#v: for %s", test.result, q, test.input) t.Errorf("Expected %#v, got %#v: for %s", test.result.(*booleanQuery).Should.(*disjunctionQuery).Disjuncts[0], q.(*booleanQuery).Should.(*disjunctionQuery).Disjuncts[0], test.input) } } } func TestQuerySyntaxParserInvalid(t *testing.T) { tests := []struct { input string }{ {"^"}, {"^5"}, {"field:-text"}, {"field:+text"}, {"field:>text"}, {"field:>=text"}, {"field: