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 TermRangeQueryTestCase(TestCase):
 
  21     Unit tests ported from Java Lucene
 
  24     def _initializeIndex(self, values):
 
  26         writer = IndexWriter(self.dir, WhitespaceAnalyzer(), True,
 
  27                              IndexWriter.MaxFieldLength.LIMITED)
 
  29             self._insertDoc(writer, value)
 
  32     def _insertDoc(self, writer, content):
 
  36         doc.add(Field("id", "id" + str(self.docCount),
 
  37                       Field.Store.YES, Field.Index.NOT_ANALYZED))
 
  38         doc.add(Field("content", content,
 
  39                       Field.Store.NO, Field.Index.ANALYZED))
 
  41         writer.addDocument(doc)
 
  44     def _addDoc(self, content):
 
  46         writer = IndexWriter(self.dir, WhitespaceAnalyzer(), False,
 
  47                              IndexWriter.MaxFieldLength.LIMITED)
 
  48         self._insertDoc(writer, content)
 
  54         self.dir = RAMDirectory()
 
  56     def testExclusive(self):
 
  58         query = TermRangeQuery("content", "A", "C", False, False)
 
  60         self._initializeIndex(["A", "B", "C", "D"])
 
  61         searcher = IndexSearcher(self.dir, True)
 
  62         topDocs = searcher.search(query, 50)
 
  63         self.assertEqual(1, topDocs.totalHits,
 
  64                          "A,B,C,D, only B in range")
 
  67         self._initializeIndex(["A", "B", "D"])
 
  68         searcher = IndexSearcher(self.dir, True)
 
  69         topDocs = searcher.search(query, 50)
 
  70         self.assertEqual(1, topDocs.totalHits,
 
  71                          "A,B,D, only B in range")
 
  75         searcher = IndexSearcher(self.dir, True)
 
  76         topDocs = searcher.search(query, 50)
 
  77         self.assertEqual(1, topDocs.totalHits,
 
  78                          "C added, still only B in range")
 
  81     def testInclusive(self):
 
  83         query = TermRangeQuery("content", "A", "C", True, True)
 
  85         self._initializeIndex(["A", "B", "C", "D"])
 
  86         searcher = IndexSearcher(self.dir, True)
 
  87         topDocs = searcher.search(query, 50)
 
  88         self.assertEqual(3, topDocs.totalHits,
 
  89                          "A,B,C,D - A,B,C in range")
 
  92         self._initializeIndex(["A", "B", "D"])
 
  93         searcher = IndexSearcher(self.dir, True)
 
  94         topDocs = searcher.search(query, 50)
 
  95         self.assertEqual(2, topDocs.totalHits,
 
  96                          "A,B,D - A and B in range")
 
 100         searcher = IndexSearcher(self.dir, True)
 
 101         topDocs = searcher.search(query, 50)
 
 102         self.assertEqual(3, topDocs.totalHits,
 
 103                          "C added - A, B, C in range")
 
 107 if __name__ == "__main__":
 
 110     if '-loop' in sys.argv:
 
 111         sys.argv.remove('-loop')