-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtest_indexing.py
More file actions
366 lines (322 loc) · 12.7 KB
/
test_indexing.py
File metadata and controls
366 lines (322 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import io
import pathlib
import libzim.writer # pyright: ignore[reportMissingModuleSource]
import pytest
from zimscraperlib.zim import Archive, Creator
from zimscraperlib.zim.indexing import IndexData, get_pdf_index_data
from zimscraperlib.zim.items import StaticItem
def test_indexing_html_with_libzim(tmp_path: pathlib.Path, html_file: pathlib.Path):
"""Two HTML entries automatically indexed by libzim"""
fpath = tmp_path / "test.zim"
main_path = "welcome"
with Creator(fpath, main_path).config_dev_metadata() as creator:
creator.add_item_for(
fpath=html_file,
path="welcome",
mimetype="text/html",
)
creator.add_item_for(
content="foo",
path="welcome2",
mimetype="text/html",
)
assert fpath.exists()
reader = Archive(fpath)
assert reader.get_search_results_count("link") == 1
assert reader.get_search_results_count("foo") == 1
def test_indexing_disabled(tmp_path: pathlib.Path, html_file: pathlib.Path):
"""One HTML entry is disabled from libzim indexing"""
fpath = tmp_path / "test.zim"
main_path = "welcome"
with Creator(fpath, main_path).config_dev_metadata() as creator:
creator.add_item_for(
fpath=html_file,
path="welcome",
mimetype="text/html",
auto_index=False,
)
creator.add_item(
StaticItem(
content="foo",
path="welcome2",
mimetype="text/html",
)
)
assert fpath.exists()
reader = Archive(fpath)
assert reader.get_search_results_count("link") == 0
assert reader.get_search_results_count("foo") == 1
def test_indexing_custom(tmp_path: pathlib.Path, html_file: pathlib.Path):
"""One HTML entry has custom indexing data"""
fpath = tmp_path / "test.zim"
main_path = "welcome"
with Creator(fpath, main_path).config_dev_metadata() as creator:
creator.add_item_for(
fpath=html_file,
path="welcome",
mimetype="text/html",
index_data=IndexData(title="blu", content="bar"),
)
creator.add_item(
StaticItem(
content="foo",
path="welcome2",
mimetype="text/html",
)
)
assert fpath.exists()
reader = Archive(fpath)
assert reader.get_search_results_count("bar") == 1
assert reader.get_search_results_count("foo") == 1
assert "welcome" in list(reader.get_suggestions("blu"))
def test_indexing_item_is_front(tmp_path: pathlib.Path, png_image: pathlib.Path):
"""Create a ZIM with a single item with customized title and content for indexing"""
fpath = tmp_path / "test.zim"
main_path = "welcome"
with Creator(fpath, main_path).config_dev_metadata() as creator:
creator.add_item(
StaticItem(
filepath=png_image,
path="welcome",
title="brain food", # title used for suggestions
index_data=IndexData(
title="screen", content="car" # title and content used for search
),
hints={libzim.writer.Hint.FRONT_ARTICLE: True},
)
)
assert fpath.exists()
reader = Archive(fpath)
assert "welcome" in list(reader.get_suggestions("brain"))
assert "welcome" in list(reader.get_suggestions("food"))
assert "welcome" not in list(reader.get_suggestions("screen"))
assert "welcome" not in list(reader.get_suggestions("car"))
assert reader.get_search_results_count("screen") == 1
assert reader.get_search_results_count("car") == 1
assert reader.get_search_results_count("brain") == 0
assert reader.get_search_results_count("food") == 0
def test_indexing_item_not_front(tmp_path: pathlib.Path, png_image: pathlib.Path):
fpath = tmp_path / "test.zim"
main_path = "welcome"
with Creator(fpath, main_path).config_dev_metadata() as creator:
creator.add_item(
StaticItem(
filepath=png_image,
path="welcome",
title="brain food", # title used for suggestions
index_data=IndexData(
title="screen", content="car" # title and content used for search
),
hints={libzim.writer.Hint.FRONT_ARTICLE: False}, # mark as not front
)
)
assert fpath.exists()
reader = Archive(fpath)
# "brain" works as a suggestion but "food" doesn't work because since no front
# article is present in the zim file, libzim doesn't create a title xapian index.
# so, when searching suggestion, libzim is fallback to a binary search on the title
# and return only article starting by the query.
# see https://github.com/openzim/libzim/issues/902#issuecomment-2223050129
assert "welcome" in list(reader.get_suggestions("brain"))
assert "welcome" not in list(reader.get_suggestions("food"))
assert "welcome" not in list(reader.get_suggestions("screen"))
assert "welcome" not in list(reader.get_suggestions("car"))
assert reader.get_search_results_count("screen") >= 1
assert reader.get_search_results_count("car") >= 1
assert reader.get_search_results_count("brain") == 0
assert reader.get_search_results_count("food") == 0
def _assert_pdf_zim(fpath: pathlib.Path):
assert fpath.exists()
reader = Archive(fpath)
assert "welcome" in list(reader.get_suggestions("microsoft"))
assert "welcome" in list(reader.get_suggestions("c8clark"))
assert "welcome" not in list(reader.get_suggestions("appropriate"))
assert "welcome" not in list(reader.get_suggestions("the"))
assert "welcome" not in list(reader.get_suggestions("brain"))
assert "welcome" not in list(reader.get_suggestions("food"))
assert reader.get_search_results_count("appropriate") == 1
assert reader.get_search_results_count("the") == 1
assert (
reader.get_search_results_count("microsoft") == 1 # title is used as index data
)
assert (
reader.get_search_results_count("c8clark") == 1 # title is used as index data
)
def _assert_png_zim(fpath: pathlib.Path):
assert fpath.exists()
reader = Archive(fpath)
assert "welcome" in list(reader.get_suggestions("brain"))
assert "welcome" in list(reader.get_suggestions("food"))
assert "welcome" not in list(reader.get_suggestions("bar"))
assert "welcome" not in list(reader.get_suggestions("feed"))
def test_indexing_item_pdf_filepath(
tmp_path: pathlib.Path, encrypted_pdf_file: pathlib.Path
):
"""A PDF item can be automatically indexed"""
fpath = tmp_path / "test.zim"
main_path = "welcome"
with Creator(fpath, main_path).config_dev_metadata() as creator:
creator.add_item(
StaticItem(
filepath=encrypted_pdf_file,
path="welcome",
hints={libzim.writer.Hint.FRONT_ARTICLE: True},
)
)
_assert_pdf_zim(fpath)
def test_indexing_item_pdf_fileobj(
tmp_path: pathlib.Path, encrypted_pdf_file: pathlib.Path
):
"""A PDF item can be automatically indexed"""
fpath = tmp_path / "test.zim"
main_path = "welcome"
with Creator(fpath, main_path).config_dev_metadata() as creator:
fileobj = io.BytesIO()
fileobj.write(encrypted_pdf_file.read_bytes())
creator.add_item(
StaticItem(
fileobj=fileobj,
path="welcome",
hints={libzim.writer.Hint.FRONT_ARTICLE: True},
)
)
_assert_pdf_zim(fpath)
def test_indexing_item_pdf_content(
tmp_path: pathlib.Path, encrypted_pdf_file: pathlib.Path
):
"""A PDF item can be automatically indexed"""
fpath = tmp_path / "test.zim"
main_path = "welcome"
with Creator(fpath, main_path).config_dev_metadata() as creator:
creator.add_item(
StaticItem(
content=encrypted_pdf_file.read_bytes(),
path="welcome",
title=None, # title not set will be set to PDF title
hints={libzim.writer.Hint.FRONT_ARTICLE: True},
)
)
_assert_pdf_zim(fpath)
def test_indexing_item_png_filepath(tmp_path: pathlib.Path, png_image: pathlib.Path):
"""A PNG item cannot automatically be indexed but it works properly"""
fpath = tmp_path / "test.zim"
main_path = "welcome"
with Creator(fpath, main_path).config_dev_metadata() as creator:
creator.add_item(
StaticItem(
filepath=png_image,
path="welcome",
title="brain food",
hints={libzim.writer.Hint.FRONT_ARTICLE: True},
)
)
def test_indexing_item_png_fileobj(tmp_path: pathlib.Path, png_image: pathlib.Path):
"""A PNG item cannot automatically be indexed but it works properly"""
fpath = tmp_path / "test.zim"
main_path = "welcome"
with Creator(fpath, main_path).config_dev_metadata() as creator:
fileobj = io.BytesIO()
fileobj.write(png_image.read_bytes())
creator.add_item(
StaticItem(
fileobj=fileobj,
path="welcome",
title="brain food",
hints={libzim.writer.Hint.FRONT_ARTICLE: True},
)
)
_assert_png_zim(fpath)
def test_indexing_item_png_content(tmp_path: pathlib.Path, png_image: pathlib.Path):
"""A PNG item cannot automatically be indexed but it works properly"""
fpath = tmp_path / "test.zim"
main_path = "welcome"
with Creator(fpath, main_path).config_dev_metadata() as creator:
creator.add_item(
StaticItem(
content=png_image.read_bytes(),
path="welcome",
title="brain food",
hints={libzim.writer.Hint.FRONT_ARTICLE: True},
)
)
_assert_png_zim(fpath)
@pytest.mark.parametrize(
"pdf_no, expected_title, expected_word_count",
[
(1, "Microsoft Word - Placeholder Documentation.docx - c8clark", 11),
(
2,
"Military Dermatology (Redacted) - Office of the Surgeon General - 1994",
304419,
),
],
)
def test_get_pdf_index_data(
pdf_no: int,
expected_title: str,
expected_word_count: int,
encrypted_pdf_file: pathlib.Path,
encrypted_pdf_content: pathlib.Path,
big_pdf_file: pathlib.Path,
big_pdf_content: pathlib.Path,
):
index_data = get_pdf_index_data(
filepath=encrypted_pdf_file if pdf_no == 1 else big_pdf_file
)
assert index_data.get_title() == expected_title
# actual index content is dependent on the MuPDF version used by PyMuPDF
# this checks that index is large-enough
content_size = len(
(encrypted_pdf_content if pdf_no == 1 else big_pdf_content).read_text()
)
assert len(index_data.get_content()) >= content_size * 0.9
assert index_data.has_indexdata()
assert index_data.get_wordcount() == expected_word_count
assert index_data.get_keywords() == ""
def test_indexing_item_pdf_custom_title(
tmp_path: pathlib.Path, encrypted_pdf_file: pathlib.Path
):
"""Test case with a custom title is passed, it is not overwritten by PDF title"""
fpath = tmp_path / "test.zim"
main_path = "welcome"
with Creator(fpath, main_path).config_dev_metadata() as creator:
creator.add_item(
StaticItem(
filepath=encrypted_pdf_file,
path="welcome",
title="brain food",
hints={libzim.writer.Hint.FRONT_ARTICLE: True},
)
)
assert fpath.exists()
reader = Archive(fpath)
assert "welcome" not in list(reader.get_suggestions("microsoft"))
assert "welcome" not in list(reader.get_suggestions("c8clark"))
assert "welcome" not in list(reader.get_suggestions("appropriate"))
assert "welcome" not in list(reader.get_suggestions("the"))
assert "welcome" in list(reader.get_suggestions("brain"))
assert "welcome" in list(reader.get_suggestions("food"))
assert reader.get_search_results_count("appropriate") == 1
assert reader.get_search_results_count("the") == 1
assert reader.get_search_results_count("microsoft") == 1
assert reader.get_search_results_count("c8clark") == 1
@pytest.mark.parametrize(
"content, wordcount, expected_wordcount",
[
("foo", None, 1),
("foo bar", None, 2),
(" foo bar ", None, 2),
(
"foo bar",
123,
123,
), # wordcount is passed so it is not automatically computed
],
)
def test_index_data_wordcount(
content: str, wordcount: int | None, expected_wordcount: int
):
assert (
IndexData(title="foo", content=content, wordcount=wordcount).get_wordcount()
== expected_wordcount
)