summaryrefslogblamecommitdiffstatshomepage
path: root/tests/plugins/scrapers/test_gallery_dl.py
blob: f5bdb88f60d00f80e757cb89487d92bea4756698 (plain) (tree)











































                                                                                     
                                                     






                                                                
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, 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