PyLucene 3.4.0-1 import
[pylucene.git] / test / test_CachingWrapperFilter.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 CachingWrapperFilterTestCase(TestCase):
20     """
21     Unit tests ported from Java Lucene
22     """
23
24     def testCachingWorks(self):
25
26         dir = RAMDirectory()
27         writer = IndexWriter(dir, StandardAnalyzer(Version.LUCENE_CURRENT),
28                              True, IndexWriter.MaxFieldLength.LIMITED)
29         writer.close()
30
31         reader = IndexReader.open(dir, True)
32
33         class mockFilter(PythonFilter):
34             def __init__(self):
35                 super(mockFilter, self).__init__()
36                 self._wasCalled = False
37             def getDocIdSet(self, reader):
38                 self._wasCalled = True;
39                 return DocIdBitSet(BitSet())
40             def clear(self):
41                 self._wasCalled = False
42             def wasCalled(self):
43                 return self._wasCalled
44
45         filter = mockFilter()
46         cacher = CachingWrapperFilter(filter)
47
48         # first time, nested filter is called
49         cacher.getDocIdSet(reader)
50         self.assert_(filter.wasCalled(), "first time")
51
52         # second time, nested filter should not be called
53         filter.clear()
54         cacher.getDocIdSet(reader)
55         self.assert_(not filter.wasCalled(), "second time")
56
57         reader.close()
58
59
60 if __name__ == "__main__":
61     import sys, lucene
62     lucene.initVM()
63     if '-loop' in sys.argv:
64         sys.argv.remove('-loop')
65         while True:
66             try:
67                 main()
68             except:
69                 pass
70     else:
71          main()