diff options
Diffstat (limited to '')
-rw-r--r-- | tests/scrapers/test_scraper.py | 17 | ||||
-rw-r--r-- | tests/scrapers/test_scraper_utils.py | 57 | ||||
-rw-r--r-- | tests/scrapers/test_types.py | 34 |
3 files changed, 98 insertions, 10 deletions
diff --git a/tests/scrapers/test_scraper.py b/tests/scrapers/test_scraper.py index 6f6f29d..d0cef7b 100644 --- a/tests/scrapers/test_scraper.py +++ b/tests/scrapers/test_scraper.py @@ -9,10 +9,18 @@ class MockScraper(Scraper): yield "bar" +class NoneScraper(Scraper): + is_available = True + + def scrape(self): + yield lambda: "foo" + yield None + + class WarningScraper(Scraper): is_available = True - def warn(self, str): + def warn(self, msg): raise ScrapeWarning("Invalid input") def scrape(self): @@ -53,3 +61,10 @@ def test_scraper_collects_literal(): generator = scraper.collect() assert set(generator) == set(["literal"]) + + +def test_scraper_collect_ignores_none(): + scraper = NoneScraper(None) + generator = scraper.collect() + + assert set(generator) == set(["foo"]) diff --git a/tests/scrapers/test_scraper_utils.py b/tests/scrapers/test_scraper_utils.py index 193cf2a..30b9796 100644 --- a/tests/scrapers/test_scraper_utils.py +++ b/tests/scrapers/test_scraper_utils.py @@ -1,24 +1,30 @@ -from hircine.scraper.utils import parse_dict +import json +import os +from zipfile import ZipFile + +import pytest + +from hircine.scraper.utils import open_archive_file, parse_dict def test_parse_dict(): - dict = { + data = { "scalar": "foo", "list": ["bar", "baz"], "dict": {"nested_scalar": "qux", "nested_list": ["plugh", "xyzzy"]}, } - def id(type): - return lambda item: f"{type}_{item}" + def annotate(tag): + return lambda item: f"{tag}_{item}" parsers = { - "scalar": id("scalar"), - "list": id("list"), - "dict": {"nested_scalar": id("scalar"), "nested_list": id("list")}, - "missing": id("missing"), + "scalar": annotate("scalar"), + "list": annotate("list"), + "dict": {"nested_scalar": annotate("scalar"), "nested_list": annotate("list")}, + "missing": annotate("missing"), } - assert [f() for f in parse_dict(parsers, dict)] == [ + assert [f() for f in parse_dict(parsers, data)] == [ "scalar_foo", "list_bar", "list_baz", @@ -26,3 +32,36 @@ def test_parse_dict(): "list_plugh", "list_xyzzy", ] + + +@pytest.mark.parametrize( + "check_sidecar", + [ + (False), + (True), + ], + ids=[ + "zip", + "sidecar", + ], +) +def test_open_archive_file(gen_archive, tmpdir, check_sidecar): + archive = next(gen_archive) + archive.path = os.path.join(tmpdir, "archive.zip") + + zip_data = {"zip": "data"} + sidecar_data = {"sidecar": "data"} + + with open(f"{archive.path}.info.json", "x") as handle: + json.dump(sidecar_data, handle) + + with ZipFile(archive.path, "x") as ziph: + ziph.writestr("info.json", json.dumps(zip_data)) + + with open_archive_file(archive, "info.json", check_sidecar=check_sidecar) as file: + data = json.load(file) + + if check_sidecar: + assert data == sidecar_data + else: + assert data == zip_data diff --git a/tests/scrapers/test_types.py b/tests/scrapers/test_types.py index ed937e7..33f9f89 100644 --- a/tests/scrapers/test_types.py +++ b/tests/scrapers/test_types.py @@ -1,6 +1,8 @@ from datetime import date import pytest + +import hircine.enums as enums from hircine.api.types import ScrapedComic from hircine.scraper import ScrapeWarning from hircine.scraper.types import ( @@ -129,3 +131,35 @@ def test_scraped_comic_silently_ignores_empty(item, attr, empty): comic = ScrapedComic.from_generator(gen()) assert getattr(comic, attr) == empty + + +@pytest.mark.parametrize( + "input,want", + [ + ("EN", Language(value=enums.Language.EN)), + ("de", Language(value=enums.Language.DE)), + ], +) +def test_language_from_iso_639_3(input, want): + assert Language.from_iso_639_3(input) == want + + +def test_language_from_iso_639_3_fails(): + with pytest.raises(ScrapeWarning, match="Could not parse language code:"): + Language.from_iso_639_3("ENG") + + +@pytest.mark.parametrize( + "input,want", + [ + ("English", Language(value=enums.Language.EN)), + ("german", Language(value=enums.Language.DE)), + ], +) +def test_language_from_name(input, want): + assert Language.from_name(input) == want + + +def test_language_from_name_fails(): + with pytest.raises(ScrapeWarning, match="Could not parse language name:"): + Language.from_name("nonexistent") |