From a495162ab6d0bf324c300eca398532ee397cf9a1 Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Thu, 14 Nov 2024 23:22:58 +0100 Subject: backend/tests: Add tests for gallery_dl scrapers --- tests/plugins/scrapers/test_gallery_dl.py | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/plugins/scrapers/test_gallery_dl.py (limited to 'tests/plugins/scrapers/test_gallery_dl.py') diff --git a/tests/plugins/scrapers/test_gallery_dl.py b/tests/plugins/scrapers/test_gallery_dl.py new file mode 100644 index 0000000..b4e7a4a --- /dev/null +++ b/tests/plugins/scrapers/test_gallery_dl.py @@ -0,0 +1,52 @@ +import json +import os +from zipfile import ZipFile + +import pytest + +import hircine.plugins.scrapers.gallery_dl +from hircine.plugins.scrapers.gallery_dl import GalleryDLScraper +from hircine.scraper.types import Title + + +class MockHandler: + source = "mock" + + def scrape(self, data): + yield Title(data["title"]) + + +@pytest.fixture +def archive_file(tmpdir): + file = os.path.join(tmpdir, "archive.zip") + + with ZipFile(file, "x") as ziph: + ziph.writestr("info.json", json.dumps({"category": "mock", "title": "test"})) + + yield file + + +def test_does_scrape(monkeypatch, archive_file, gen_comic): + comic = next(gen_comic) + comic.archive.path = archive_file + + monkeypatch.setattr( + hircine.plugins.scrapers.gallery_dl, "HANDLERS", {"mock": MockHandler} + ) + + scraper = GalleryDLScraper(comic) + + assert scraper.is_available + assert scraper.source == MockHandler.source + assert scraper.name == f"gallery-dl info.json ({MockHandler.source})" + assert set(scraper.collect()) == set([Title(value="test")]) + + +def test_does_not_scrape_on_error(tmpdir, monkeypatch, gen_comic): + comic = next(gen_comic) + comic.archive.path = os.path.join(tmpdir, "nonexistent.zip") + + scraper = GalleryDLScraper(comic) + + assert scraper.data == {} + assert not scraper.is_available -- cgit v1.2.3-2-gb3c3