|
4 | 4 | import base64
|
5 | 5 | import datetime
|
6 | 6 | import io
|
| 7 | +import logging |
7 | 8 | import pathlib
|
8 | 9 | import random
|
9 | 10 | import shutil
|
10 | 11 | import subprocess
|
11 | 12 | import sys
|
12 | 13 | import tempfile
|
13 | 14 | import time
|
| 15 | +from unittest.mock import call, patch |
14 | 16 |
|
15 | 17 | import pytest
|
16 | 18 | from libzim.writer import Compression # pyright: ignore
|
@@ -540,6 +542,105 @@ def test_check_metadata(tmp_path):
|
540 | 542 | Creator(tmp_path, "").config_dev_metadata(LongDescription="T" * 5000).start()
|
541 | 543 |
|
542 | 544 |
|
| 545 | +@pytest.mark.parametrize( |
| 546 | + "tags", |
| 547 | + [ |
| 548 | + ( |
| 549 | + "wikipedia;_category:wikipedia;_pictures:no;_videos:no;_details:yes;" |
| 550 | + "_ftindex:yes" |
| 551 | + ), |
| 552 | + ( |
| 553 | + [ |
| 554 | + "wikipedia", |
| 555 | + "_category:wikipedia", |
| 556 | + "_pictures:no", |
| 557 | + "_videos:no", |
| 558 | + "_details:yes", |
| 559 | + "_ftindex:yes", |
| 560 | + ] |
| 561 | + ), |
| 562 | + ], |
| 563 | +) |
| 564 | +@patch("zimscraperlib.zim.creator.logger", autospec=True) |
| 565 | +def test_start_logs_metadata_log_contents(mocked_logger, png_image, tags, tmp_path): |
| 566 | + mocked_logger.isEnabledFor.side_effect = lambda level: level == logging.DEBUG |
| 567 | + fpath = tmp_path / "test_config.zim" |
| 568 | + with open(png_image, "rb") as fh: |
| 569 | + png_data = fh.read() |
| 570 | + creator = Creator(fpath, "", disable_metadata_checks=True).config_metadata( |
| 571 | + Name="wikipedia_fr_football", |
| 572 | + Title="English Wikipedia", |
| 573 | + Creator="English speaking Wikipedia contributors", |
| 574 | + Publisher="Wikipedia user Foobar", |
| 575 | + Date="2009-11-21", |
| 576 | + Description="All articles (without images) from the english Wikipedia", |
| 577 | + LongDescription="This ZIM file contains all articles (without images)" |
| 578 | + " from the english Wikipedia by 2009-11-10. The topics are...", |
| 579 | + Language="eng", |
| 580 | + License="CC-BY", |
| 581 | + Tags=tags, |
| 582 | + Flavour="nopic", |
| 583 | + Source="https://en.wikipedia.org/", |
| 584 | + Scraper="mwoffliner 1.2.3", |
| 585 | + Illustration_48x48_at_1=png_data, |
| 586 | + TestMetadata="Test Metadata", |
| 587 | + ) |
| 588 | + |
| 589 | + class NotPrintable: |
| 590 | + def __str__(self): |
| 591 | + raise ValueError("Not printable I said") |
| 592 | + |
| 593 | + creator._metadata.update( |
| 594 | + { |
| 595 | + "Illustration_96x96@1": b"%PDF-1.5\n%\xe2\xe3\xcf\xd3", |
| 596 | + "Chars": b"\xc5\xa1\xc9\x94\xc9\x9b", |
| 597 | + "Chars-32": b"\xff\xfe\x00\x00a\x01\x00\x00T\x02\x00\x00[\x02\x00\x00", |
| 598 | + "Video": b"\x00\x00\x00 ftypisom\x00\x00\x02\x00isomiso2avc1mp41\x00", |
| 599 | + "Toupie": NotPrintable(), |
| 600 | + } |
| 601 | + ) |
| 602 | + creator._log_metadata() |
| 603 | + # /!\ this must be alpha sorted |
| 604 | + mocked_logger.debug.assert_has_calls( |
| 605 | + [ |
| 606 | + call("Metadata: Chars = šɔɛ"), |
| 607 | + call( |
| 608 | + "Metadata: Chars-32 is a 16 bytes text/plain blob " |
| 609 | + "not decodable as an UTF-8 string" |
| 610 | + ), |
| 611 | + call("Metadata: Creator = English speaking Wikipedia contributors"), |
| 612 | + call("Metadata: Date = 2009-11-21"), |
| 613 | + call( |
| 614 | + "Metadata: Description = All articles (without images) from the " |
| 615 | + "english Wikipedia" |
| 616 | + ), |
| 617 | + call("Metadata: Flavour = nopic"), |
| 618 | + call("Metadata: Illustration_48x48@1 is a 3274 bytes 48x48px PNG Image"), |
| 619 | + call( |
| 620 | + "Metadata: Illustration_96x96@1 is a 14 bytes " |
| 621 | + "application/pdf blob not recognized as an Image" |
| 622 | + ), |
| 623 | + call("Metadata: Language = eng"), |
| 624 | + call("Metadata: License = CC-BY"), |
| 625 | + call( |
| 626 | + "Metadata: LongDescription = This ZIM file contains all articles " |
| 627 | + "(without images) from the english Wikipedia by 2009-11-10. " |
| 628 | + "The topics are..." |
| 629 | + ), |
| 630 | + call("Metadata: Name = wikipedia_fr_football"), |
| 631 | + call("Metadata: Publisher = Wikipedia user Foobar"), |
| 632 | + call("Metadata: Relation = None"), |
| 633 | + call("Metadata: Scraper = mwoffliner 1.2.3"), |
| 634 | + call("Metadata: Source = https://en.wikipedia.org/"), |
| 635 | + call(f"Metadata: Tags = {tags}"), |
| 636 | + call("Metadata: TestMetadata = Test Metadata"), |
| 637 | + call("Metadata: Title = English Wikipedia"), |
| 638 | + call("Metadata: Toupie is unexpected data type: NotPrintable"), |
| 639 | + call("Metadata: Video is a 33 bytes video/mp4 blob"), |
| 640 | + ] |
| 641 | + ) |
| 642 | + |
| 643 | + |
543 | 644 | def test_relax_metadata(tmp_path):
|
544 | 645 | Creator(tmp_path, "", disable_metadata_checks=True).config_dev_metadata(
|
545 | 646 | Description="T" * 90
|
|
0 commit comments