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 # Test reusableTokenStream, using ReusableAnalyzerBase:
20 class MyDeletionPolicy(PythonIndexDeletionPolicy):
23 onCommitCalled = False
25 def onInit(self, commits):
26 self.onInitCalled = True
28 def onCommit(self, commits):
29 self.onCommitCalled = True
32 class IndexDeletionPolicyTestCase(TestCase):
34 def testIndexDeletionPolicy(self):
37 config = IndexWriterConfig(Version.LUCENE_CURRENT,
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)
47 writer.addDocument(doc)
50 # now we called commit
51 self.assertTrue(policy.onCommitCalled)
53 # external IR sees 1 commit:
54 self.assertEquals(1, IndexReader.listCommits(dir).size())
57 writer.addDocument(doc)
60 # external IR sees 2 commits:
61 self.assertEquals(2, IndexReader.listCommits(dir).size())
65 # open same index, make sure both commits survived:
66 config = IndexWriterConfig(Version.LUCENE_CURRENT,
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())
76 self.assertEquals(2, IndexReader.listCommits(dir).size())
78 if __name__ == "__main__":
81 if '-loop' in sys.argv:
82 sys.argv.remove('-loop')