-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest_zim_creator.py
712 lines (582 loc) · 24.3 KB
/
test_zim_creator.py
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4 nu
import base64
import datetime
import io
import pathlib
import random
import shutil
import subprocess
import sys
import tempfile
import time
import pytest
from libzim.writer import Compression # pyright: ignore
from zimscraperlib.constants import (
DEFAULT_DEV_ZIM_METADATA,
MANDATORY_ZIM_METADATA_KEYS,
UTF8,
)
from zimscraperlib.download import save_large_file, stream_file
from zimscraperlib.filesystem import delete_callback
from zimscraperlib.zim import Archive, Creator, StaticItem, URLItem
from zimscraperlib.zim.providers import FileLikeProvider, URLProvider
class SpecialURLProvider(URLProvider):
"""prevents crash on invalid size"""
def get_size(self) -> int:
return self.size or 0
class SpecialURLProviderItem(StaticItem):
def get_contentprovider(self):
return SpecialURLProvider(self.url)
class FileLikeProviderItem(StaticItem):
def get_contentprovider(self):
if not self.fileobj:
raise AttributeError("fileobj cannot be None")
return FileLikeProvider(self.fileobj)
def test_zim_creator(tmp_path, png_image, html_file, html_str: str, html_str_cn: str):
fpath = tmp_path / "test.zim"
main_path = "welcome"
tags = ";".join(["toto", "tata"])
with open(png_image, "rb") as fh:
png_data = fh.read()
with Creator(fpath, main_path).config_dev_metadata(
Tags=tags, Illustration_48x48_at_1=png_data # pyright: ignore
) as creator:
# verbatim HTML from string
creator.add_item_for("welcome", "wel", content=html_str, is_front=True)
# verbatim HTML from bytes
creator.add_item_for(
"welcome1", "wel1", content=html_str.encode(), is_front=True
)
creator.add_item_for(
"welcome2", "wel2", content=html_str_cn.encode("gb2312"), is_front=True
)
# verbatim HTML from file
creator.add_item_for("welcome3", "wel3", fpath=html_file)
creator.add_item_for("welcome4", "wel4", fpath=html_file)
# single binary image
creator.add_item_for(
"images/yahoo.png", "Home !!", fpath=png_image, is_front=True
)
# redirect to our main page (no title)
creator.add_redirect("home", "welcome")
# redirect to our main page (with a custom title)
creator.add_redirect("home2", "welcome", "Home !!")
creator.add_redirect("home3", "welcome3", "Home !!", is_front=True)
creator.add_redirect("home4", "welcome4", "Home !!", is_front=False)
# ensure args requirement are checked
with pytest.raises(ValueError, match="One of fpath or content is required"):
creator.add_item_for("images/yahoo.png")
assert fpath.exists()
reader = Archive(fpath)
assert reader.get_text_metadata("Title") == DEFAULT_DEV_ZIM_METADATA["Title"]
assert reader.get_text_metadata("Language") == DEFAULT_DEV_ZIM_METADATA["Language"]
assert reader.get_text_metadata("Tags") == tags
assert reader.main_entry.get_item().path == f"{main_path}"
# make sure we have our image
assert reader.get_item("images/yahoo.png")
# make sure we have our redirects
assert reader.get_entry_by_path("home2").is_redirect
assert reader.get_entry_by_path("home2").get_redirect_entry().path == f"{main_path}"
# make sure titles were indexed (html with title for xapian ; redirects are not)
# see https://github.com/openzim/python-libzim/issues/125
# see https://github.com/openzim/libzim/issues/642
assert "home2" not in list(reader.get_suggestions("Home")) # no is_front > False
# assert "home3" in list(reader.get_suggestions("Home")) # is_front=True
assert "home4" not in list(reader.get_suggestions("Home")) # is_front=False
assert "images/yahoo.png" in list(reader.get_suggestions("Home")) # is_frontTrue
# make sure full text was indexed
assert reader.get_search_results_count("PDF doc") >= 1
# ensure non-rewritten articles have not been rewritten
assert bytes(reader.get_item("welcome").content).decode(UTF8) == html_str
assert bytes(reader.get_item("welcome1").content).decode(UTF8) == html_str
assert bytes(reader.get_item("welcome2").content).decode("gb2312") == html_str_cn
assert bytes(reader.get_item("welcome3").content).decode(UTF8) == html_str
# ensure illustration is present and corrext
assert reader.has_illustration()
assert bytes(reader.get_illustration_item().content) == png_data
def test_create_without_workaround(tmp_path):
fpath = tmp_path / "test.zim"
with Creator(
fpath, "welcome", workaround_nocancel=False
).config_dev_metadata() as creator:
with pytest.raises(RuntimeError, match="AttributeError"):
creator.add_item("hello")
def test_noindexlanguage(tmp_path):
fpath = tmp_path / "test.zim"
creator = Creator(fpath, "welcome").config_dev_metadata(Language="bam")
creator.config_indexing(False)
with creator as creator:
creator.add_item(StaticItem(path="welcome", content="hello"))
creator.add_item_for("index", "Index", content="-", mimetype="text/html")
reader = Archive(fpath)
assert reader.get_text_metadata("Language") == "bam"
# html content triggers both title and content xapian indexes
# but since indexing is disabled, we should only have title one
assert reader.has_title_index
assert not reader.has_fulltext_index
def test_add_item_for(tmp_path):
fpath = tmp_path / "test.zim"
# test without mimetype
with Creator(fpath, "welcome").config_dev_metadata() as creator:
creator.add_item_for(path="welcome", title="hello", content="hello")
# test missing fpath and content
with Creator(fpath, "welcome").config_dev_metadata() as creator:
with pytest.raises(ValueError):
creator.add_item_for(path="welcome", title="hello")
def test_add_item_for_delete(tmp_path, html_file):
fpath = tmp_path / "test.zim"
local_path = pathlib.Path(tmp_path / "somefile.html")
# copy file to local path
shutil.copyfile(html_file, local_path)
with Creator(fpath, "welcome").config_dev_metadata() as creator:
creator.add_item_for(fpath=local_path, path="index", delete_fpath=True)
assert not local_path.exists()
reader = Archive(fpath)
assert reader.get_item("index")
def test_add_item_for_delete_fail(tmp_path, png_image):
fpath = tmp_path / "test.zim"
local_path = pathlib.Path(tmp_path / "somefile.png")
# copy file to local path
shutil.copyfile(png_image, local_path)
with Creator(fpath, "welcome").config_dev_metadata() as creator:
creator.add_item(
StaticItem(
filepath=local_path,
path="index",
),
callback=(delete_callback, local_path),
)
assert not local_path.exists()
reader = Archive(fpath)
assert reader.get_item("index")
def test_add_item_empty_content(tmp_path):
fpath = tmp_path / "test.zim"
# test with incorrect content type
with Creator(fpath, "welcome").config_dev_metadata() as creator:
creator.add_item_for(
path="welcome",
title="hello",
content="",
)
def test_add_item_for_unsupported_content_type(tmp_path):
fpath = tmp_path / "test.zim"
# test with incorrect content type
with Creator(fpath, "welcome").config_dev_metadata() as creator:
with pytest.raises(RuntimeError):
creator.add_item_for(
path="welcome",
title="hello",
mimetype="text/plain",
content=123, # pyright: ignore[reportArgumentType]
)
def test_compression(tmp_path):
fpath = tmp_path / "test.zim"
with Creator(
tmp_path / "test.zim", "welcome", compression="zstd"
).config_dev_metadata() as creator:
creator.add_item(StaticItem(path="welcome", content="hello"))
with Creator(
fpath, "welcome", compression=Compression.zstd # pyright: ignore
).config_dev_metadata() as creator:
creator.add_item(StaticItem(path="welcome", content="hello"))
def test_double_finish(tmp_path):
fpath = tmp_path / "test.zim"
with Creator(fpath, "welcome").config_dev_metadata() as creator:
creator.add_item(StaticItem(path="welcome", content="hello"))
# ensure we can finish an already finished creator
creator.finish()
def test_cannot_finish(tmp_path):
creator = Creator(tmp_path / "test.zim", "")
creator.can_finish = False
creator.finish()
def test_sourcefile_removal(tmp_path, html_file):
fpath = tmp_path / "test.zim"
with Creator(fpath, "").config_dev_metadata() as creator:
# using a temp dir so file still have a meaningful name
tmpdir = tempfile.TemporaryDirectory(dir=tmp_path) # can't use contextmgr
# copy html to folder
src_path = pathlib.Path(tmpdir.name, "source.html")
shutil.copyfile(html_file, src_path)
creator.add_item(StaticItem(filepath=src_path, path=src_path.name, ref=tmpdir))
del tmpdir
assert not src_path.exists()
def test_sourcefile_removal_std(tmp_path, html_file):
fpath = tmp_path / "test.zim"
paths = []
with Creator(fpath, "").config_dev_metadata() as creator:
for idx in range(0, 4):
# copy html to folder
paths.append(pathlib.Path(tmp_path / f"source{idx}.html"))
shutil.copyfile(html_file, paths[-1])
creator.add_item(
StaticItem(
filepath=paths[-1],
path=paths[-1].name,
mimetype="text/html",
),
callback=(delete_callback, paths[-1]),
)
for path in paths:
assert not path.exists()
def test_sourcefile_noremoval(tmp_path, html_file):
# copy html to folder
src_path = tmp_path / "source.html"
shutil.copyfile(html_file, src_path)
fpath = tmp_path / "test.zim"
with Creator(fpath, "").config_dev_metadata() as creator:
creator.add_item(StaticItem(path=src_path.name, filepath=src_path))
assert src_path.exists()
def test_urlitem_badurl(tmp_path):
with Creator(tmp_path / "test.zim", "").config_dev_metadata() as creator:
with pytest.raises(IOError, match="Unable to access URL"):
creator.add_item(URLItem(url="httpo://hello:helloe:hello/"))
def test_urlitem_html(tmp_path, gzip_html_url):
file_path = tmp_path / "file.html"
save_large_file(gzip_html_url, file_path)
with open(file_path, "rb") as fh:
file_bytes = fh.read()
fpath = tmp_path / "test.zim"
with Creator(fpath, "").config_dev_metadata() as creator:
creator.add_item(URLItem(url=gzip_html_url))
zim = Archive(fpath)
assert bytes(zim.get_item("wiki/Main_Page").content) == file_bytes
def test_urlitem_nonhtmlgzip(tmp_path, gzip_nonhtml_url):
file_path = tmp_path / "file.txt"
save_large_file(gzip_nonhtml_url, file_path)
with open(file_path, "rb") as fh:
file_bytes = fh.read()
fpath = tmp_path / "test.zim"
with Creator(fpath, "").config_dev_metadata() as creator:
creator.add_item(URLItem(url=gzip_nonhtml_url))
with Creator(fpath, "").config_dev_metadata() as creator:
creator.add_item(URLItem(url=gzip_nonhtml_url, use_disk=True))
zim = Archive(fpath)
assert bytes(zim.get_item("robots.txt").content) == file_bytes
def test_urlitem_binary(tmp_path, png_image_url):
file_path = tmp_path / "file.png"
save_large_file(png_image_url, file_path)
with open(file_path, "rb") as fh:
file_bytes = fh.read()
fpath = tmp_path / "test.zim"
with Creator(fpath, "").config_dev_metadata() as creator:
creator.add_item(URLItem(url=png_image_url))
zim = Archive(fpath)
assert (
bytes(zim.get_item("static/images/project-logos/commonswiki.png").content)
== file_bytes
)
def test_urlitem_staticcontent(tmp_path, gzip_nonhtml_url):
fpath = tmp_path / "test.zim"
with Creator(fpath, "").config_dev_metadata() as creator:
creator.add_item(URLItem(url=gzip_nonhtml_url, content="hello"))
zim = Archive(fpath)
assert bytes(zim.get_item("robots.txt").content) == b"hello"
def test_filelikeprovider_nosize(tmp_path, png_image_url):
fileobj = io.BytesIO()
stream_file(png_image_url, byte_stream=fileobj)
fpath = tmp_path / "test.zim"
with Creator(fpath, "").config_dev_metadata() as creator:
creator.add_item(
FileLikeProviderItem(fileobj=fileobj, path="one.png") # pyright: ignore
)
zim = Archive(fpath)
assert bytes(zim.get_item("one.png").content) == fileobj.getvalue()
def test_urlprovider(tmp_path, png_image_url):
file_path = tmp_path / "file.png"
save_large_file(png_image_url, file_path)
with open(file_path, "rb") as fh:
file_bytes = fh.read()
fpath = tmp_path / "test.zim"
with Creator(fpath, "").config_dev_metadata() as creator:
creator.add_item(
SpecialURLProviderItem(url=png_image_url, path="one.png") # pyright: ignore
)
zim = Archive(fpath)
assert bytes(zim.get_item("one.png").content) == file_bytes
def test_urlprovider_nolength(tmp_path, png_image_url, png_image):
# save url's content locally using external tool
png_image = tmp_path / "original.png"
save_large_file(png_image_url, png_image)
with open(png_image, "rb") as fh:
png_image_bytes = fh.read()
# create and start an http server without Content-Length support
server_fpath = tmp_path / "httpd.py"
port = random.randint(10000, 20000) # noqa: S311
server_code = """
from http.server import BaseHTTPRequestHandler, HTTPServer
class handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "image/png")
if "gzip" in self.path:
self.send_header("Content-Encoding", "gzip")
self.end_headers()
with open("{src}", "rb") as fh:
self.wfile.write(fh.read())
with HTTPServer(('', {port}), handler) as server:
server.serve_forever()
"""
with open(server_fpath, "w") as fh:
fh.write(
server_code.replace("{port}", str(port)).replace("{src}", str(png_image))
)
httpd = subprocess.Popen([sys.executable, server_fpath])
time.sleep(2) # allow http server to start
fpath = tmp_path / "test.zim"
try:
with tempfile.TemporaryDirectory() as tmp_dir, Creator(
fpath, ""
).config_dev_metadata() as creator:
tmp_dir = pathlib.Path(tmp_dir) # noqa: PLW2901
creator.add_item(
URLItem(
url=f"http://localhost:{port}/hoho.png",
path="B",
tmp_dir=tmp_dir,
use_disk=True,
)
)
creator.add_item(
URLItem(url=f"http://localhost:{port}/home.png", tmp_dir=tmp_dir)
)
creator.add_item(
SpecialURLProviderItem(
url=f"http://localhost:{port}/home.png", # pyright: ignore
mimetype="image/png", # pyright: ignore
)
)
finally:
httpd.terminate()
zim = Archive(fpath)
assert bytes(zim.get_item("home.png").content) == png_image_bytes
assert bytes(zim.get_item("B").content) == png_image_bytes
def test_item_callback(tmp_path, html_file):
fpath = tmp_path / "test.zim"
class Store:
called = False
def cb():
Store.called = True
with Creator(fpath, "").config_dev_metadata() as creator:
creator.add_item(
StaticItem(path=html_file.name, filepath=html_file), callback=cb
)
assert Store.called is True
def test_compess_hints(tmp_path, html_file):
with Creator(tmp_path / "test.zim", "").config_dev_metadata() as creator:
creator.add_item_for(
path=html_file.name,
fpath=html_file,
delete_fpath=True,
should_compress=True,
)
def test_callback_and_remove(tmp_path, html_file):
class Store:
called = 0
def cb(*args): # noqa: ARG001
Store.called += 1
# duplicate test file as we'll want to remove twice
html_file2 = html_file.with_suffix(f".2{html_file.suffix}")
shutil.copyfile(html_file, html_file2)
with Creator(tmp_path / "test.zim", "").config_dev_metadata() as creator:
creator.add_item_for(
path=html_file.name, fpath=html_file, delete_fpath=True, callback=cb
)
creator.add_item_for(
path=html_file2.name,
fpath=html_file2,
delete_fpath=True,
callback=(cb, html_file.name),
)
assert not html_file.exists()
assert Store.called
assert Store.called == 2
def test_duplicates(tmp_path):
with Creator(tmp_path / "test.zim", "").config_dev_metadata() as creator:
creator.add_item_for(path="A", content="A")
creator.add_item_for(path="C", content="C")
creator.add_redirect(path="B", target_path="A")
with pytest.raises(RuntimeError, match="existing dirent's title"):
creator.add_item_for(path="A", content="test2")
with pytest.raises(RuntimeError, match="existing dirent's title"):
creator.add_redirect(path="B", target_path="C")
def test_ignore_duplicates(tmp_path):
with Creator(
tmp_path / "test.zim", "", ignore_duplicates=True
).config_dev_metadata() as creator:
creator.add_item_for(path="A", content="A")
creator.add_item_for(path="A", content="A2")
creator.add_redirect(path="B", target_path="A")
creator.add_redirect(path="B", target_path="C")
def test_without_metadata(tmp_path):
with pytest.raises(ValueError, match="Mandatory metadata are not all set."):
Creator(tmp_path, "").start()
def test_check_metadata(tmp_path):
with pytest.raises(ValueError, match="Counter cannot be set"):
Creator(tmp_path, "").config_dev_metadata(Counter=1).start() # pyright: ignore
with pytest.raises(ValueError, match="Description is too long."):
Creator(tmp_path, "").config_dev_metadata(Description="T" * 90).start()
with pytest.raises(ValueError, match="LongDescription is too long."):
Creator(tmp_path, "").config_dev_metadata(LongDescription="T" * 5000).start()
def test_relax_metadata(tmp_path):
Creator(tmp_path, "", disable_metadata_checks=True).config_dev_metadata(
Description="T" * 90
).start()
@pytest.mark.parametrize(
"tags",
[
(
"wikipedia;_category:wikipedia;_pictures:no;_videos:no;_details:yes;"
"_ftindex:yes"
),
(
[
"wikipedia",
"_category:wikipedia",
"_pictures:no",
"_videos:no",
"_details:yes",
"_ftindex:yes",
]
),
],
)
def test_config_metadata(tmp_path, png_image, tags):
fpath = tmp_path / "test_config.zim"
with open(png_image, "rb") as fh:
png_data = fh.read()
creator = Creator(fpath, "").config_metadata(
Name="wikipedia_fr_football",
Title="English Wikipedia",
Creator="English speaking Wikipedia contributors",
Publisher="Wikipedia user Foobar",
Date="2009-11-21",
Description="All articles (without images) from the english Wikipedia",
LongDescription="This ZIM file contains all articles (without images)"
" from the english Wikipedia by 2009-11-10. The topics are...",
Language="eng",
License="CC-BY",
Tags=tags,
Flavour="nopic",
Source="https://en.wikipedia.org/",
Scraper="mwoffliner 1.2.3",
Illustration_48x48_at_1=png_data,
TestMetadata="Test Metadata",
)
with creator:
pass
assert fpath.exists()
reader = Archive(fpath)
assert reader.get_text_metadata("Name") == "wikipedia_fr_football"
assert reader.get_text_metadata("Title") == "English Wikipedia"
assert (
reader.get_text_metadata("Creator") == "English speaking Wikipedia contributors"
)
assert reader.get_text_metadata("Publisher") == "Wikipedia user Foobar"
assert reader.get_text_metadata("Date") == "2009-11-21"
assert (
reader.get_text_metadata("Description")
== "All articles (without images) from the english Wikipedia"
)
assert (
reader.get_text_metadata("LongDescription")
== "This ZIM file contains all articles (without images)"
" from the english Wikipedia by 2009-11-10. The topics are..."
)
assert reader.get_text_metadata("Language") == "eng"
assert reader.get_text_metadata("License") == "CC-BY"
assert (
reader.get_text_metadata("Tags")
== "wikipedia;_category:wikipedia;_pictures:no;_videos:no;"
"_details:yes;_ftindex:yes"
)
assert reader.get_text_metadata("Flavour") == "nopic"
assert reader.get_text_metadata("Source") == "https://en.wikipedia.org/"
assert reader.get_text_metadata("Scraper") == "mwoffliner 1.2.3"
assert reader.get_metadata("Illustration_48x48@1") == png_data
assert reader.get_text_metadata("TestMetadata") == "Test Metadata"
@pytest.mark.parametrize(
"name,value,valid",
[
("Name", 4, False),
("Title", 4, False),
("Creator", 4, False),
("Publisher", 4, False),
("Description", 4, False),
("LongDescription", 4, False),
("License", 4, False),
("Relation", 4, False),
("Relation", 4, False),
("Flavour", 4, False),
("Source", 4, False),
("Scraper", 4, False),
("Title", "X" * 30, True),
("Title", "X" * 31, False),
("Date", 4, False),
("Date", datetime.datetime.now(), True), # noqa: DTZ005
("Date", datetime.datetime(1969, 12, 31, 23, 59), True), # noqa: DTZ001
("Date", datetime.date(1969, 12, 31), True),
("Date", datetime.date.today(), True), # noqa: DTZ011
("Date", "1969-12-31", True),
("Date", "1969-13-31", False),
("Date", "2023/02/29", False),
("Language", "xxx", False),
("Language", "rmr", False),
("Language", "eng", True),
("Language", "fra", True),
("Language", "bam", True),
("Language", "fr", False),
("Language", "en", False),
("Language", "fra,eng", True),
("Language", "fra,eng,bam", True),
("Language", "fra,en,bam", False),
("Language", "eng,", False),
("Language", "eng, fra", False),
("Counter", "1", False),
("Description", "X" * 80, True),
("Description", "X" * 81, False),
("LongDescription", "X" * 4000, True),
("LongDescription", "X" * 4001, False),
("Tags", 4, False),
("Tags", ["wikipedia", 4, "football"], False),
("Tags", ("wikipedia", "football"), True),
("Tags", ["wikipedia", "football"], True),
("Tags", "wikipedia;football", True),
# 1x1 PNG image
(
"Illustration_48x48@1",
base64.b64decode(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAGXRFWHRTb2Z0d2FyZQBB"
"ZG9iZSBJbWFnZVJlYWR5ccllPAAAAA9JREFUeNpi+P//P0CAAQAF/gL+Lc6J7gAAAABJ"
"RU5ErkJggg=="
),
False,
),
(
"Illustration_48x48@1",
DEFAULT_DEV_ZIM_METADATA["Illustration_48x48_at_1"],
True,
),
(
"Illustration_96x96@1",
DEFAULT_DEV_ZIM_METADATA["Illustration_48x48_at_1"],
False,
),
]
+ [(name, "", False) for name in MANDATORY_ZIM_METADATA_KEYS],
)
def test_validate_metadata(tmp_path, name, value, valid):
if valid:
Creator(tmp_path / "_.zim", "").validate_metadata(name, value)
else:
with pytest.raises(ValueError):
Creator(tmp_path / "_.zim", "").validate_metadata(name, value)
def test_config_indexing(tmp_path):
with pytest.raises(ValueError):
Creator(tmp_path / "_.zim", "").config_indexing(True, "fr")
with pytest.raises(ValueError):
Creator(tmp_path / "_.zim", "").config_indexing(True, "")
assert Creator(tmp_path / "_.zim", "").config_indexing(True, "fra")
assert Creator(tmp_path / "_.zim", "").config_indexing(True, "bam")
assert Creator(tmp_path / "_.zim", "").config_indexing(False, "bam")
assert Creator(tmp_path / "_.zim", "").config_indexing(False)