1 # ====================================================================
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13 # ====================================================================
15 from unittest import TestCase, main
19 class BooleanTestMixin(object):
21 def getBooleanQuery(self, clauses, disableCoord):
23 extra_query = TermQuery(Term("all", "extra_clause"))
24 extra_clause = BooleanClause(extra_query, BooleanClause.Occur.SHOULD)
25 clauses.add(extra_clause)
27 return super(BooleanTestMixin, self).getBooleanQuery(clauses,
31 class PythonQueryParserTestCase(TestCase):
33 def testOverrideBooleanQuery(self):
35 class TestQueryParser(BooleanTestMixin, PythonQueryParser):
36 def getFieldQuery_quoted(_self, field, queryText, quoted):
37 return super(TestQueryParser, _self).getFieldQuery_quoted_super(field, queryText, quoted)
39 qp = TestQueryParser(Version.LUCENE_CURRENT, 'all',
40 StandardAnalyzer(Version.LUCENE_CURRENT))
41 q = qp.parse("foo bar")
42 self.assertEquals(str(q), "all:foo all:bar all:extra_clause")
45 class PythonMultiFieldQueryParserTestCase(TestCase):
47 def testOverrideBooleanQuery(self):
49 class TestQueryParser(BooleanTestMixin, PythonMultiFieldQueryParser):
50 def getFieldQuery_quoted(_self, field, queryText, quoted):
51 return super(TestQueryParser, _self).getFieldQuery_quoted_super(field, queryText, quoted)
53 qp = TestQueryParser(Version.LUCENE_CURRENT, ['one', 'two'],
54 StandardAnalyzer(Version.LUCENE_CURRENT))
55 q = qp.parse(Version.LUCENE_CURRENT, "foo bar", ['one', 'two'],
56 [BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD],
57 StandardAnalyzer(Version.LUCENE_CURRENT))
58 self.assertEquals(str(q), "(one:foo one:bar) (two:foo two:bar)")
61 if __name__ == "__main__":
64 if '-loop' in sys.argv:
65 sys.argv.remove('-loop')