Skip to content

Commit

Permalink
Refactor code and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit74 committed Jan 16, 2024
1 parent 950cf09 commit edaef40
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/kolibri2zim/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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:])

Check warning on line 208 in src/kolibri2zim/entrypoint.py

View check run for this annotation

Codecov / codecov/patch

src/kolibri2zim/entrypoint.py#L208

Added line #L208 was not covered by tests
set_debug(args.debug)
logger = get_logger()

from kolibri2zim.scraper import Kolibri2Zim

try:
scraper = Kolibri2Zim(**dict(args._get_kwargs()))
sys.exit(scraper.run())
Expand Down
31 changes: 25 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand All @@ -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:
Expand Down
31 changes: 31 additions & 0 deletions tests/test_sanitize_inputs.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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

0 comments on commit edaef40

Please sign in to comment.