pylucene 3.5.0-3
[pylucene.git] / test / test_IndexDeletionPolicy.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 # Test reusableTokenStream, using ReusableAnalyzerBase:
20 class MyDeletionPolicy(PythonIndexDeletionPolicy):
21
22     onInitCalled = False
23     onCommitCalled = False
24     
25     def onInit(self, commits):
26       self.onInitCalled = True
27
28     def onCommit(self, commits):
29       self.onCommitCalled = True
30     
31
32 class IndexDeletionPolicyTestCase(TestCase):
33
34     def testIndexDeletionPolicy(self):
35
36         dir = RAMDirectory()
37         config = IndexWriterConfig(Version.LUCENE_CURRENT,
38                                    WhitespaceAnalyzer())
39         policy = MyDeletionPolicy()
40         config.setIndexDeletionPolicy(policy)
41         writer = IndexWriter(dir, config)
42         # no commits exist in the index yet
43         self.assertFalse(policy.onInitCalled)
44         # we haven't called commit yet
45         self.assertFalse(policy.onCommitCalled)
46         doc = Document()
47         writer.addDocument(doc)
48         writer.commit()
49
50         # now we called commit
51         self.assertTrue(policy.onCommitCalled)
52
53         # external IR sees 1 commit:
54         self.assertEquals(1, IndexReader.listCommits(dir).size())
55
56         # commit again:
57         writer.addDocument(doc)
58         writer.commit()
59
60         # external IR sees 2 commits:
61         self.assertEquals(2, IndexReader.listCommits(dir).size())
62
63         writer.close()
64
65         # open same index, make sure both commits survived:
66         config = IndexWriterConfig(Version.LUCENE_CURRENT,
67                                    WhitespaceAnalyzer())
68         policy = MyDeletionPolicy()
69         config.setIndexDeletionPolicy(policy)
70         writer = IndexWriter(dir, config)
71         self.assertTrue(policy.onInitCalled)
72         self.assertFalse(policy.onCommitCalled)
73         self.assertEquals(2, IndexReader.listCommits(dir).size())
74         writer.close()
75
76         self.assertEquals(2, IndexReader.listCommits(dir).size())
77
78 if __name__ == "__main__":
79     import sys, lucene
80     lucene.initVM()
81     if '-loop' in sys.argv:
82         sys.argv.remove('-loop')
83         while True:
84             try:
85                 main()
86             except:
87                 pass
88     else:
89          main()