PyLucene 3.4.0-1 import
[pylucene.git] / test / test_TermRangeQuery.py
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
5 #
6 #       http://www.apache.org/licenses/LICENSE-2.0
7 #
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 # ====================================================================
14
15 from unittest import TestCase, main
16 from lucene import *
17
18
19 class TermRangeQueryTestCase(TestCase):
20     """
21     Unit tests ported from Java Lucene
22     """
23
24     def _initializeIndex(self, values):
25
26         writer = IndexWriter(self.dir, WhitespaceAnalyzer(), True,
27                              IndexWriter.MaxFieldLength.LIMITED)
28         for value in values:
29             self._insertDoc(writer, value)
30         writer.close()
31
32     def _insertDoc(self, writer, content):
33
34         doc = Document()
35
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))
40
41         writer.addDocument(doc)
42         self.docCount += 1
43
44     def _addDoc(self, content):
45
46         writer = IndexWriter(self.dir, WhitespaceAnalyzer(), False,
47                              IndexWriter.MaxFieldLength.LIMITED)
48         self._insertDoc(writer, content)
49         writer.close()
50
51     def setUp(self):
52
53         self.docCount = 0
54         self.dir = RAMDirectory()
55
56     def testExclusive(self):
57
58         query = TermRangeQuery("content", "A", "C", False, False)
59
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")
65         searcher.close()
66
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")
72         searcher.close()
73
74         self._addDoc("C")
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")
79         searcher.close()
80
81     def testInclusive(self):
82
83         query = TermRangeQuery("content", "A", "C", True, True)
84
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")
90         searcher.close()
91
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")
97         searcher.close()
98
99         self._addDoc("C")
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")
104         searcher.close()
105
106
107 if __name__ == "__main__":
108     import sys, lucene
109     lucene.initVM()
110     if '-loop' in sys.argv:
111         sys.argv.remove('-loop')
112         while True:
113             try:
114                 main()
115             except:
116                 pass
117     else:
118          main()