PyLucene 3.4.0-1 import
[pylucene.git] / test / test_BooleanOr.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 BooleanOrTestCase(TestCase):
20     """
21     Unit tests ported from Java Lucene
22     """
23
24     def __init__(self, *args):
25
26         super(BooleanOrTestCase, self).__init__(*args)
27
28         self.FIELD_T = "T"
29         self.FIELD_C = "C"
30
31         self.t1 = TermQuery(Term(self.FIELD_T, "files"))
32         self.t2 = TermQuery(Term(self.FIELD_T, "deleting"))
33         self.c1 = TermQuery(Term(self.FIELD_C, "production"))
34         self.c2 = TermQuery(Term(self.FIELD_C, "optimize"))
35
36         self.searcher = None
37
38     def setUp(self):
39
40         rd = RAMDirectory()
41         writer = IndexWriter(rd, StandardAnalyzer(Version.LUCENE_CURRENT),
42                              True, IndexWriter.MaxFieldLength.LIMITED)
43
44         d = Document()
45         d.add(Field(self.FIELD_T,
46                     "Optimize not deleting all files",
47                     Field.Store.YES, Field.Index.ANALYZED))
48         d.add(Field(self.FIELD_C,
49                     "Deleted When I run an optimize in our production environment.",
50                     Field.Store.YES, Field.Index.ANALYZED))
51
52         writer.addDocument(d)
53         writer.close()
54
55         self.searcher = IndexSearcher(rd, True)
56
57     def search(self, q):
58         return self.searcher.search(q, 50).totalHits
59
60     def testElements(self):
61
62         self.assertEqual(1, self.search(self.t1))
63         self.assertEqual(1, self.search(self.t2))
64         self.assertEqual(1, self.search(self.c1))
65         self.assertEqual(1, self.search(self.c2))
66
67     def testFlat(self):
68
69         q = BooleanQuery()
70         q.add(BooleanClause(self.t1, BooleanClause.Occur.SHOULD))
71         q.add(BooleanClause(self.t2, BooleanClause.Occur.SHOULD))
72         q.add(BooleanClause(self.c1, BooleanClause.Occur.SHOULD))
73         q.add(BooleanClause(self.c2, BooleanClause.Occur.SHOULD))
74         self.assertEqual(1, self.search(q))
75
76     def testParenthesisMust(self):
77
78         q3 = BooleanQuery()
79         q3.add(BooleanClause(self.t1, BooleanClause.Occur.SHOULD))
80         q3.add(BooleanClause(self.t2, BooleanClause.Occur.SHOULD))
81         q4 = BooleanQuery()
82         q4.add(BooleanClause(self.c1, BooleanClause.Occur.MUST))
83         q4.add(BooleanClause(self.c2, BooleanClause.Occur.MUST))
84         q2 = BooleanQuery()
85         q2.add(q3, BooleanClause.Occur.SHOULD)
86         q2.add(q4, BooleanClause.Occur.SHOULD)
87         self.assertEqual(1, self.search(q2))
88
89     def testParenthesisMust2(self):
90
91         q3 = BooleanQuery()
92         q3.add(BooleanClause(self.t1, BooleanClause.Occur.SHOULD))
93         q3.add(BooleanClause(self.t2, BooleanClause.Occur.SHOULD))
94         q4 = BooleanQuery()
95         q4.add(BooleanClause(self.c1, BooleanClause.Occur.SHOULD))
96         q4.add(BooleanClause(self.c2, BooleanClause.Occur.SHOULD))
97         q2 = BooleanQuery()
98         q2.add(q3, BooleanClause.Occur.SHOULD)
99         q2.add(q4, BooleanClause.Occur.MUST)
100         self.assertEqual(1, self.search(q2))
101
102     def testParenthesisShould(self):
103
104         q3 = BooleanQuery()
105         q3.add(BooleanClause(self.t1, BooleanClause.Occur.SHOULD))
106         q3.add(BooleanClause(self.t2, BooleanClause.Occur.SHOULD))
107         q4 = BooleanQuery()
108         q4.add(BooleanClause(self.c1, BooleanClause.Occur.SHOULD))
109         q4.add(BooleanClause(self.c2, BooleanClause.Occur.SHOULD))
110         q2 = BooleanQuery()
111         q2.add(q3, BooleanClause.Occur.SHOULD)
112         q2.add(q4, BooleanClause.Occur.SHOULD)
113         self.assertEqual(1, self.search(q2))
114
115
116 if __name__ == "__main__":
117     import sys, lucene
118     lucene.initVM()
119     if '-loop' in sys.argv:
120         sys.argv.remove('-loop')
121         while True:
122             try:
123                 main()
124             except:
125                 pass
126     else:
127          main()