Debian Codegraph @

petl/test/io/test_whoosh.py

raw
1 # -*- coding: utf-8 -*-
2 from __future__ import absolute_import, print_function, division
3
4
5 import os
6 import tempfile
7
8 import pytest
9
10 from petl.test.helpers import ieq
11 import petl as etl
12 from petl.io.whoosh import fromtextindex, totextindex, appendtextindex, \
13 searchtextindex
14
15
16 try:
17 import whoosh # noqa: F401
18 except ImportError as e:
19 pytest.skip('SKIP whoosh tests: %s' % e, allow_module_level=True)
20 else:
21
22 from whoosh.index import create_in
23 from whoosh.fields import *
24 import datetime
25
26 def test_fromindex_dirname():
27
28 dirname = tempfile.mkdtemp()
29
30 schema = Schema(title=TEXT(stored=True), path=ID(stored=True),
31 content=TEXT)
32
33 ix = create_in(dirname, schema)
34 writer = ix.writer()
35 writer.add_document(title=u"First document", path=u"/a",
36 content=u"This is the first document we've added!")
37 writer.add_document(title=u"Second document", path=u"/b",
38 content=u"The second one is even more interesting!")
39 writer.commit()
40
41 # N.B., fields get sorted
42 expect = ((u'path', u'title'),
43 (u'/a', u'First document'),
44 (u'/b', u'Second document'))
45 actual = fromtextindex(dirname)
46 ieq(expect, actual)
47
48 def test_fromindex_index():
49
50 dirname = tempfile.mkdtemp()
51
52 schema = Schema(title=TEXT(stored=True), path=ID(stored=True),
53 content=TEXT)
54
55 ix = create_in(dirname, schema)
56 writer = ix.writer()
57 writer.add_document(title=u"First document", path=u"/a",
58 content=u"This is the first document we've added!")
59 writer.add_document(title=u"Second document", path=u"/b",
60 content=u"The second one is even more interesting!")
61 writer.commit()
62
63 # N.B., fields get sorted
64 expect = ((u'path', u'title'),
65 (u'/a', u'First document'),
66 (u'/b', u'Second document'))
67 actual = fromtextindex(ix)
68 ieq(expect, actual)
69
70 def test_fromindex_docnum_field():
71
72 dirname = tempfile.mkdtemp()
73 schema = Schema(title=TEXT(stored=True), path=ID(stored=True),
74 content=TEXT)
75
76 ix = create_in(dirname, schema)
77 writer = ix.writer()
78 writer.add_document(title=u"First document", path=u"/a",
79 content=u"This is the first document we've added!")
80 writer.add_document(title=u"Second document", path=u"/b",
81 content=u"The second one is even more interesting!")
82 writer.commit()
83
84 # N.B., fields get sorted
85 expect = ((u'docnum', u'path', u'title'),
86 (0, u'/a', u'First document'),
87 (1, u'/b', u'Second document'))
88 actual = fromtextindex(dirname, docnum_field='docnum')
89 ieq(expect, actual)
90
91 def test_toindex_dirname():
92
93 dirname = tempfile.mkdtemp()
94
95 # name fields in ascending order as whoosh sorts fields on the way out
96 tbl = (('f0', 'f1', 'f2', 'f3', 'f4'),
97 (u'AAA', 12, 4.3, True, datetime.datetime.now()),
98 (u'BBB', 6, 3.4, False, datetime.datetime(1900, 1, 31)),
99 (u'CCC', 42, 7.8, True, datetime.datetime(2100, 12, 25)))
100
101 schema = Schema(f0=TEXT(stored=True),
102 f1=NUMERIC(int, stored=True),
103 f2=NUMERIC(float, stored=True),
104 f3=BOOLEAN(stored=True),
105 f4=DATETIME(stored=True))
106
107 totextindex(tbl, dirname, schema=schema)
108
109 actual = fromtextindex(dirname)
110 ieq(tbl, actual)
111
112 def test_toindex_index():
113
114 dirname = tempfile.mkdtemp()
115
116 # name fields in ascending order as whoosh sorts fields on the way out
117 tbl = (('f0', 'f1', 'f2', 'f3', 'f4'),
118 (u'AAA', 12, 4.3, True, datetime.datetime.now()),
119 (u'BBB', 6, 3.4, False, datetime.datetime(1900, 1, 31)),
120 (u'CCC', 42, 7.8, True, datetime.datetime(2100, 12, 25)))
121
122 schema = Schema(f0=TEXT(stored=True),
123 f1=NUMERIC(int, stored=True),
124 f2=NUMERIC(float, stored=True),
125 f3=BOOLEAN(stored=True),
126 f4=DATETIME(stored=True))
127 index = create_in(dirname, schema)
128
129 totextindex(tbl, index)
130
131 actual = fromtextindex(index)
132 ieq(tbl, actual)
133
134 def test_appendindex_dirname():
135
136 dirname = tempfile.mkdtemp()
137
138 # name fields in ascending order as whoosh sorts fields on the way out
139 tbl = (('f0', 'f1', 'f2', 'f3', 'f4'),
140 (u'AAA', 12, 4.3, True, datetime.datetime.now()),
141 (u'BBB', 6, 3.4, False, datetime.datetime(1900, 1, 31)),
142 (u'CCC', 42, 7.8, True, datetime.datetime(2100, 12, 25)))
143
144 schema = Schema(f0=TEXT(stored=True),
145 f1=NUMERIC(int, stored=True),
146 f2=NUMERIC(float, stored=True),
147 f3=BOOLEAN(stored=True),
148 f4=DATETIME(stored=True))
149
150 totextindex(tbl, dirname, schema=schema)
151 appendtextindex(tbl, dirname)
152
153 actual = fromtextindex(dirname)
154 expect = tbl + tbl[1:]
155 ieq(expect, actual)
156
157 def test_appendindex_index():
158
159 dirname = tempfile.mkdtemp()
160
161 # name fields in ascending order as whoosh sorts fields on the way out
162 tbl = (('f0', 'f1', 'f2', 'f3', 'f4'),
163 (u'AAA', 12, 4.3, True, datetime.datetime.now()),
164 (u'BBB', 6, 3.4, False, datetime.datetime(1900, 1, 31)),
165 (u'CCC', 42, 7.8, True, datetime.datetime(2100, 12, 25)))
166
167 schema = Schema(f0=TEXT(stored=True),
168 f1=NUMERIC(int, stored=True),
169 f2=NUMERIC(float, stored=True),
170 f3=BOOLEAN(stored=True),
171 f4=DATETIME(stored=True))
172 index = create_in(dirname, schema)
173
174 totextindex(tbl, index)
175 appendtextindex(tbl, index)
176
177 actual = fromtextindex(index)
178 expect = tbl + tbl[1:]
179 ieq(expect, actual)
180
181 def test_searchindex():
182
183 dirname = tempfile.mkdtemp()
184 schema = Schema(title=TEXT(stored=True), path=ID(stored=True),
185 content=TEXT)
186
187 ix = create_in(dirname, schema)
188 writer = ix.writer()
189 writer.add_document(title=u"Oranges", path=u"/a",
190 content=u"This is the first document we've added!")
191 writer.add_document(title=u"Apples", path=u"/b",
192 content=u"The second document is even more "
193 u"interesting!")
194 writer.commit()
195
196 # N.B., fields get sorted
197 expect = ((u'path', u'title'),
198 (u'/a', u'Oranges'))
199 # N.B., by default whoosh does not do stemming
200 actual = searchtextindex(dirname, 'oranges')
201 ieq(expect, actual)
202 actual = searchtextindex(dirname, 'add*')
203 ieq(expect, actual)
204
205 expect = ((u'path', u'title'),
206 (u'/a', u'Oranges'),
207 (u'/b', u'Apples'))
208 actual = searchtextindex(dirname, 'doc*')
209 ieq(expect, actual)
210
211 def test_integration():
212
213 dirname = tempfile.mkdtemp()
214 schema = Schema(title=TEXT(stored=True), path=ID(stored=True),
215 content=TEXT)
216
217 ix = create_in(dirname, schema)
218 writer = ix.writer()
219 writer.add_document(title=u"First document", path=u"/a",
220 content=u"This is the first document we've added!")
221 writer.add_document(title=u"Second document", path=u"/b",
222 content=u"The second one is even more interesting!")
223 writer.commit()
224
225 # N.B., fields get sorted
226 expect = ((u'path', u'title'),
227 (u'/a', u'first document'),
228 (u'/b', u'second document'))
229 actual = etl.fromtextindex(dirname).convert('title', 'lower')
230 ieq(expect, actual)
231
232 # TODO test_searchindexpage
233 # TODO test_searchindex_multifield_query
234 # TODO test_searchindex_nontext_query