Skip to content

Commit

Permalink
Mutualize test code base + add pyright hint
Browse files Browse the repository at this point in the history
pyright ignore is mandatory because the type checker has no idea about
the impact of validate_metadata/validate_tag. This might be fixed by a
shared logic and a TypeGuard but is not available until Python 3.10 ;
other solution would be to transfer metadata to a way more typed
container after validation.
  • Loading branch information
benoit74 committed Feb 12, 2024
1 parent 38daa9c commit 3d807f8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 52 deletions.
4 changes: 3 additions & 1 deletion src/zimscraperlib/zim/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ def add_metadata(
if name == "Date" and isinstance(content, (datetime.date, datetime.datetime)):
content = content.strftime("%Y-%m-%d").encode("UTF-8")
if name == "Tags" and not isinstance(content, str):
content = ";".join(content)
content = ";".join(
content # pyright: ignore (to be fixed in 3.10 with a TypeGuard)
)
super().add_metadata(name, content, mimetype)

def config_metadata(
Expand Down
72 changes: 21 additions & 51 deletions tests/zim/test_zim_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,26 @@ def test_check_metadata(tmp_path):
Creator(tmp_path, "").config_dev_metadata(LongDescription="T" * 5000).start()


def test_config_metadata(tmp_path, png_image):
@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()
Expand All @@ -529,8 +548,7 @@ def test_config_metadata(tmp_path, png_image):
" from the english Wikipedia by 2009-11-10. The topics are...",
Language="eng",
License="CC-BY",
Tags="wikipedia;_category:wikipedia;_pictures:no;_videos:no;"
"_details:yes;_ftindex:yes",
Tags=tags,
Flavour="nopic",
Source="https://en.wikipedia.org/",
Scraper="mwoffliner 1.2.3",
Expand Down Expand Up @@ -573,54 +591,6 @@ def test_config_metadata(tmp_path, png_image):
assert reader.get_text_metadata("TestMetadata") == "Test Metadata"


def test_creator_metadata_list_as_tags(tmp_path, png_image):
"""
Test creator metadata setup when "Tags" is a list of strings.
The test case is based on test_config_metadata()
This is also a regression test for #125.
"""
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=[
"wikipedia",
"_category:wikipedia",
"_pictures:no",
"_videos:no",
"_details:yes",
"_ftindex:yes",
],
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("Tags")
== "wikipedia;_category:wikipedia;_pictures:no;_videos:no;"
"_details:yes;_ftindex:yes"
)


@pytest.mark.parametrize(
"name,value,valid",
[
Expand Down

0 comments on commit 3d807f8

Please sign in to comment.