diff options
author | Wolfgang Müller | 2024-11-15 13:39:54 +0100 |
---|---|---|
committer | Wolfgang Müller | 2024-11-15 13:39:54 +0100 |
commit | 3b62b56c706622b7361e267d92f0408a29bee812 (patch) | |
tree | b40ffb637a747aeb9ce8b8c8992511f0380a1584 | |
parent | 105850a66e961fa9f8f521ab65d49f3860ff6cd3 (diff) | |
download | hircine-3b62b56c706622b7361e267d92f0408a29bee812.tar.gz |
backend/tests: Add tests for the E-Hentai API
-rw-r--r-- | poetry.lock | 19 | ||||
-rw-r--r-- | pyproject.toml | 1 | ||||
-rw-r--r-- | tests/plugins/scrapers/test_ehentai_api.py | 140 |
3 files changed, 159 insertions, 1 deletions
diff --git a/poetry.lock b/poetry.lock index 9bc9b4d..2c1153d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -996,6 +996,23 @@ socks = ["PySocks (>=1.5.6,!=1.5.7)"] use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"] [[package]] +name = "requests-mock" +version = "1.12.1" +description = "Mock out responses from the requests package" +optional = false +python-versions = ">=3.5" +files = [ + {file = "requests-mock-1.12.1.tar.gz", hash = "sha256:e9e12e333b525156e82a3c852f22016b9158220d2f47454de9cae8a77d371401"}, + {file = "requests_mock-1.12.1-py2.py3-none-any.whl", hash = "sha256:b1e37054004cdd5e56c84454cc7df12b25f90f382159087f4b6915aaeef39563"}, +] + +[package.dependencies] +requests = ">=2.22,<3" + +[package.extras] +fixture = ["fixtures"] + +[[package]] name = "ruff" version = "0.7.3" description = "An extremely fast Python linter and code formatter, written in Rust." @@ -1667,4 +1684,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "64f6b231ad48842e46bba074c1b09071f10702ea7398c48290b362bc968c3e00" +content-hash = "f8bc11cee9e10f6c14a5446d03c7fc5427cff64f094d216540d62c625bb61ca6" diff --git a/pyproject.toml b/pyproject.toml index f4847ac..f4ff147 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,6 +35,7 @@ pytest-cov = "^6.0.0" sphinx = "^8.1.3" furo = "^2024.1.29" sphinx-issues = "^5.0.0" +requests-mock = "^1.12.1" [build-system] requires = ["poetry-core"] diff --git a/tests/plugins/scrapers/test_ehentai_api.py b/tests/plugins/scrapers/test_ehentai_api.py new file mode 100644 index 0000000..dd283e1 --- /dev/null +++ b/tests/plugins/scrapers/test_ehentai_api.py @@ -0,0 +1,140 @@ +from datetime import date + +import pytest + +import hircine.enums as enums +import hircine.plugins.scrapers.ehentai_api as ehentai_api +from hircine.scraper import ScrapeError +from hircine.scraper.types import ( + URL, + Artist, + Censorship, + Circle, + Date, + Language, + OriginalTitle, + Rating, + Tag, + Title, + World, +) + + +def test_does_scrape(requests_mock, gen_comic): + comic = next(gen_comic) + comic.url = "https://exhentai.org/g/1025913/fdaabef1a2" + + scraper = ehentai_api.EHentaiAPIScraper(comic) + + requests_mock.post( + ehentai_api.API_URL, + text=""" + { + "gmetadata": [ + { + "gid": 1025913, + "token": "fdaabef1a2", + "title": "(C91) [Animachine (Shimahara)] Iya na Kao Sarenagara Opantsu Misete Moraitai Manga | A manga about girl showing you her panties while making a disgusted face [English] [葛の寺]", + "title_jpn": "(C91) [アニマルマシーン (40原)] 嫌な顔されながらおパンツ見せてもらいたい漫画 [英訳]", + "category": "Non-H", + "thumb": "https://ehgt.org/51/17/5117cde63cc14436c5ad7f2dd06abb52c86aff65-23642001-2866-4047-png_250.jpg", + "uploader": "葛の寺", + "posted": "1486182349", + "filecount": "23", + "filesize": 528093263, + "expunged": false, + "rating": "4.72", + "torrentcount": "1", + "torrents": [ + { + "hash": "30c7124efca83bf0db1b9fd5ab4511da5f28a60b", + "added": "1486121301", + "name": "(C91) [Animachine (Shimahara)] Iya na Kao Sarenagara Opantsu Misete Moraitai Manga A manga about girl showing you her panties while making a disgusted face [English] [葛の寺].zip", + "tsize": "20800", + "fsize": "528093242" + } + ], + "tags": [ + "language:english", + "language:translated", + "parody:iya na kao sare nagara opantsu misete moraitai", + "group:animachine", + "artist:shimahara", + "female:femdom", + "female:schoolgirl uniform", + "other:full color" + ], + "parent_gid": "1025875", + "parent_key": "cfe6adccb8", + "first_gid": "1025646", + "first_key": "098b4a982a" + } + ] + } + """, # noqa: E501 + ) + + assert scraper.is_available + assert scraper.id == 1025913 + assert scraper.token == "fdaabef1a2" + + assert set(scraper.collect()) == set( + [ + Artist(name="shimahara"), + Censorship(value=enums.Censorship.NONE), + Circle(name="animachine"), + Date(value=date(2017, 2, 4)), + Language(value=enums.Language.EN), + OriginalTitle(value="嫌な顔されながらおパンツ見せてもらいたい漫画"), + Rating(value=enums.Rating.QUESTIONABLE), + Tag(namespace="female", tag="femdom"), + Tag(namespace="female", tag="schoolgirl uniform"), + Tag(namespace="other", tag="full color"), + Title( + value="A manga about girl showing you her panties while making a disgusted face" # noqa: E501 + ), + URL("https://exhentai.org/g/1025913/fdaabef1a2"), + World(name="iya na kao sare nagara opantsu misete moraitai"), + ] + ) + + +def test_is_not_available_with_wrong_url(gen_comic): + comic = next(gen_comic) + comic.url = "https://example.com" + + scraper = ehentai_api.EHentaiAPIScraper(comic) + + assert not scraper.is_available + + +def test_raises_scrape_error_with_invalid_json(requests_mock, gen_comic): + comic = next(gen_comic) + comic.url = "https://exhentai.org/g/1025913/fdaabef1a2" + + scraper = ehentai_api.EHentaiAPIScraper(comic) + + requests_mock.post(ehentai_api.API_URL, text="{") + + assert scraper.is_available + assert scraper.id == 1025913 + assert scraper.token == "fdaabef1a2" + + with pytest.raises(ScrapeError, match="Could not parse JSON response"): + assert set(scraper.collect()) == set() + + +def test_raises_scrape_error_with_error_code(requests_mock, gen_comic): + comic = next(gen_comic) + comic.url = "https://exhentai.org/g/1025913/fdaabef1a2" + + scraper = ehentai_api.EHentaiAPIScraper(comic) + + requests_mock.post(ehentai_api.API_URL, status_code=500) + + assert scraper.is_available + assert scraper.id == 1025913 + assert scraper.token == "fdaabef1a2" + + with pytest.raises(ScrapeError, match="Request failed with status code 500"): + assert set(scraper.collect()) == set() |