From edaef40ad1fec372711178ca9e7ca8e51bcf1951 Mon Sep 17 00:00:00 2001 From: benoit74 Date: Tue, 16 Jan 2024 10:15:18 +0100 Subject: [PATCH] Refactor code and add more tests --- src/kolibri2zim/entrypoint.py | 10 ++++++---- tests/conftest.py | 31 +++++++++++++++++++++++++------ tests/test_sanitize_inputs.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/kolibri2zim/entrypoint.py b/src/kolibri2zim/entrypoint.py index 163bebe..30f0032 100755 --- a/src/kolibri2zim/entrypoint.py +++ b/src/kolibri2zim/entrypoint.py @@ -5,9 +5,10 @@ import sys from kolibri2zim.constants import NAME, SCRAPER, Global, get_logger, set_debug +from kolibri2zim.scraper import Kolibri2Zim -def main(): +def parse_args(raw_args): parser = argparse.ArgumentParser( prog=NAME, description="Scraper to create ZIM files from Kolibri channels", @@ -200,13 +201,14 @@ def main(): action="version", version=SCRAPER, ) + return parser.parse_args(raw_args) + - args = parser.parse_args() +def main(): + args = parse_args(sys.argv[1:]) set_debug(args.debug) logger = get_logger() - from kolibri2zim.scraper import Kolibri2Zim - try: scraper = Kolibri2Zim(**dict(args._get_kwargs())) sys.exit(scraper.run()) diff --git a/tests/conftest.py b/tests/conftest.py index af47abf..33eff3e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,9 +6,6 @@ from kolibri2zim.scraper import Kolibri2Zim, KolibriDB from kolibri2zim.scraper import options as expected_options_keys -CHANNEL_NAME = "channel_name" -CHANNEL_DESCRIPTION = "a description" - class FakeDb(KolibriDB): def __init__( @@ -30,10 +27,32 @@ def get_channel_metadata(self, _): @pytest.fixture() -def scraper_generator() -> Generator[Callable[..., Kolibri2Zim], None, None]: +def channel_name() -> Generator[str, None, None]: + yield "channel name" + + +@pytest.fixture() +def channel_description() -> Generator[str, None, None]: + yield "channel description" + + +@pytest.fixture() +def channel_author() -> Generator[str, None, None]: + yield "channel author" + + +@pytest.fixture() +def zim_name() -> Generator[str, None, None]: + yield "a_name" + + +@pytest.fixture() +def scraper_generator( + channel_name, channel_description +) -> Generator[Callable[..., Kolibri2Zim], None, None]: def _scraper( - channel_name: str = CHANNEL_NAME, - channel_description: str = CHANNEL_DESCRIPTION, + channel_name: str = channel_name, + channel_description: str = channel_description, channel_author: str | None = None, additional_options: dict[str, Any] | None = None, ) -> Kolibri2Zim: diff --git a/tests/test_sanitize_inputs.py b/tests/test_sanitize_inputs.py index 0569c09..5f0a18b 100644 --- a/tests/test_sanitize_inputs.py +++ b/tests/test_sanitize_inputs.py @@ -1,13 +1,16 @@ import random +import re import string from collections.abc import Callable import pytest +from conftest import FakeDb from zimscraperlib.constants import MAXIMUM_DESCRIPTION_METADATA_LENGTH as MAX_DESC_LEN from zimscraperlib.constants import ( MAXIMUM_LONG_DESCRIPTION_METADATA_LENGTH as MAX_LONG_DESC_LEN, ) +from kolibri2zim.entrypoint import parse_args from kolibri2zim.scraper import Kolibri2Zim @@ -191,3 +194,31 @@ def test_description( assert scraper.description == expected_description assert scraper.long_description == expected_long_description + + +def test_no_required_args(): + with pytest.raises(expected_exception=SystemExit): + parse_args([]) + + +def test_defaults_args(channel_name, channel_description, channel_author, zim_name): + args = parse_args(["--name", zim_name]) + scraper = Kolibri2Zim(**dict(args._get_kwargs())) + scraper.db = FakeDb( + channel_name=channel_name, + channel_description=channel_description, + channel_author=channel_author, + ) + scraper.sanitize_inputs() + assert scraper.language == "eng" + assert scraper.publisher == "openZIM" + assert scraper.author == channel_author + assert scraper.title == channel_name + assert scraper.description == channel_description + assert scraper.name == zim_name + assert re.match( + pattern=f"{zim_name}_\\d{{4}}-\\d{{2}}\\.zim", string=scraper.clean_fname + ) + # We compare sets because ordering does not matter + assert set(scraper.tags) == {"_category:other", "kolibri", "_videos:yes"} + assert len(scraper.tags) == 3