PyLucene 3.4.0-1 import
[pylucene.git] / test / test_BinaryDocument.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 TestBinaryDocument(TestCase):
20
21     binaryValStored = "this text will be stored as a byte array in the index"
22     binaryValCompressed = "this text will be also stored and compressed as a byte array in the index"
23   
24     def testBinaryFieldInIndex(self):
25
26         bytes = JArray('byte')(self.binaryValStored)
27         binaryFldStored = Field("binaryStored", bytes, 
28                                 Field.Store.YES)
29         stringFldStored = Field("stringStored", self.binaryValStored,
30                                 Field.Store.YES, Field.Index.NO,
31                                 Field.TermVector.NO)
32
33         try:
34             # binary fields with store off are not allowed
35             Field("fail", bytes, Field.Store.NO)
36             self.fail()
37         except JavaError, e:
38             self.assertEqual(e.getJavaException().getClass().getName(),
39                              'java.lang.IllegalArgumentException')
40     
41         doc = Document()
42         doc.add(binaryFldStored)
43         doc.add(stringFldStored)
44
45         # test for field count
46         self.assertEqual(2, doc.fields.size())
47     
48         # add the doc to a ram index
49         dir = RAMDirectory()
50         writer = IndexWriter(dir, StandardAnalyzer(Version.LUCENE_CURRENT),
51                              True, IndexWriter.MaxFieldLength.LIMITED)
52         writer.addDocument(doc)
53         writer.close()
54     
55         # open a reader and fetch the document
56         reader = IndexReader.open(dir, False)
57         docFromReader = reader.document(0)
58         self.assert_(docFromReader is not None)
59     
60         # fetch the binary stored field and compare it's content with the
61         # original one
62         bytes = docFromReader.getBinaryValue("binaryStored")
63         binaryFldStoredTest = bytes.string_
64         self.assertEqual(binaryFldStoredTest, self.binaryValStored)
65         
66         # fetch the string field and compare it's content with the original
67         # one
68         stringFldStoredTest = docFromReader.get("stringStored")
69         self.assertEqual(stringFldStoredTest, self.binaryValStored)
70     
71         # delete the document from index
72         reader.deleteDocument(0)
73         self.assertEqual(0, reader.numDocs())
74     
75         reader.close()
76         dir.close()
77   
78     def testCompressionTools(self):
79
80         bytes = JArray('byte')(self.binaryValCompressed)
81         binaryFldCompressed = Field("binaryCompressed", CompressionTools.compress(bytes), Field.Store.YES)
82         stringFldCompressed = Field("stringCompressed", CompressionTools.compressString(self.binaryValCompressed), Field.Store.YES)
83     
84         doc = Document()
85         doc.add(binaryFldCompressed)
86         doc.add(stringFldCompressed)
87     
88         # add the doc to a ram index
89         dir = RAMDirectory()
90         writer = IndexWriter(dir, StandardAnalyzer(Version.LUCENE_CURRENT),
91                              True, IndexWriter.MaxFieldLength.LIMITED)
92         writer.addDocument(doc)
93         writer.close()
94     
95         # open a reader and fetch the document
96         reader = IndexReader.open(dir, False)
97         docFromReader = reader.document(0)
98         self.assert_(docFromReader is not None)
99     
100         # fetch the binary compressed field and compare it's content with
101         # the original one
102         bytes = CompressionTools.decompress(docFromReader.getBinaryValue("binaryCompressed"))
103         binaryFldCompressedTest = bytes.string_
104         self.assertEqual(binaryFldCompressedTest, self.binaryValCompressed)
105         self.assertEqual(CompressionTools.decompressString(docFromReader.getBinaryValue("stringCompressed")), self.binaryValCompressed)
106
107         reader.close()
108         dir.close()
109
110
111 if __name__ == '__main__':
112     import sys, lucene
113     lucene.initVM()
114     if '-loop' in sys.argv:
115         sys.argv.remove('-loop')
116         while True:
117             try:
118                 main()
119             except:
120                 pass
121     else:
122         main()