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 BooleanPrefixQueryTestCase(TestCase):
21 Unit tests ported from Java Lucene
24 def getCount(self, r, q):
26 if BooleanQuery.instance_(q):
27 return len(BooleanQuery.cast_(q).getClauses())
28 elif ConstantScoreQuery.instance_(q):
29 iter = ConstantScoreQuery.cast_(q).getFilter().getDocIdSet(r).iterator()
31 while iter.nextDoc() != DocIdSetIterator.NO_MORE_DOCS:
36 self.fail("unexpected query " + q)
40 directory = RAMDirectory()
41 categories = ["food", "foodanddrink", "foodanddrinkandgoodtimes",
45 writer = IndexWriter(directory, WhitespaceAnalyzer(), True,
46 IndexWriter.MaxFieldLength.LIMITED)
47 for category in categories:
49 doc.add(Field("category", category, Field.Store.YES,
50 Field.Index.NOT_ANALYZED))
51 writer.addDocument(doc)
55 reader = IndexReader.open(directory, True)
56 query = PrefixQuery(Term("category", "foo"))
57 rw1 = query.rewrite(reader)
60 bq.add(query, BooleanClause.Occur.MUST)
62 rw2 = bq.rewrite(reader)
66 self.assertEqual(self.getCount(reader, rw1), self.getCount(reader, rw2),
67 "Number of Clauses Mismatch")
70 if __name__ == "__main__":
73 if '-loop' in sys.argv:
74 sys.argv.remove('-loop')