2022-06-18 00:01:55 +02:00
|
|
|
import shutil
|
2022-05-16 16:09:17 +02:00
|
|
|
from pathlib import Path
|
2022-06-18 00:01:55 +02:00
|
|
|
|
2022-05-16 16:09:17 +02:00
|
|
|
import pytest
|
|
|
|
import requests
|
2022-06-18 00:01:55 +02:00
|
|
|
|
2022-05-16 16:09:17 +02:00
|
|
|
import mangadlp.downloader as downloader
|
|
|
|
|
|
|
|
|
|
|
|
def test_downloader():
|
|
|
|
urls = [
|
|
|
|
"https://uploads.mangadex.org/data/f1117c5e7aff315bc3429a8791c89d63/A1-c111d78b798f1dda1879334a3478f7ae4503578e8adf1af0fcc4e14d2a396ad4.png",
|
|
|
|
"https://uploads.mangadex.org/data/f1117c5e7aff315bc3429a8791c89d63/A2-717ec3c83e8e05ed7b505941431a417ebfed6a005f78b89650efd3b088b951ec.png",
|
|
|
|
"https://uploads.mangadex.org/data/f1117c5e7aff315bc3429a8791c89d63/A3-95f1b873d75f7fb820cf293df903ca37264d4af8963f44d154418c529c737547.png",
|
|
|
|
"https://uploads.mangadex.org/data/f1117c5e7aff315bc3429a8791c89d63/A4-defb89c1919b7721d3b09338f175186cabe4e292e4925818a6982581378f1966.png",
|
|
|
|
"https://uploads.mangadex.org/data/f1117c5e7aff315bc3429a8791c89d63/A5-8d852ab3e9ddb070d8ba70bc5c04d78012032975b3a69603cc88a4a8d12652d4.png",
|
|
|
|
]
|
|
|
|
chapter_path = Path("tests/test_folder1")
|
|
|
|
chapter_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
images = []
|
2022-06-25 14:28:42 +02:00
|
|
|
downloader.download_chapter(urls, str(chapter_path), 2, True)
|
2022-05-16 16:09:17 +02:00
|
|
|
for file in chapter_path.iterdir():
|
|
|
|
images.append(file.name)
|
|
|
|
|
2022-06-25 14:28:42 +02:00
|
|
|
images.sort()
|
2022-06-18 00:01:55 +02:00
|
|
|
assert images == ["001.png", "002.png", "003.png", "004.png", "005.png"]
|
2022-05-16 16:09:17 +02:00
|
|
|
# cleanup
|
|
|
|
shutil.rmtree(chapter_path, ignore_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
def test_downloader_fail(monkeypatch):
|
|
|
|
images = [
|
|
|
|
"https://uploads.mangadex.org/data/f1117c5e7aff315bc3429a8791c89d63/A1-c111d78b798f1dda1879334a3478f7ae4503578e8adf1af0fcc4e14d2a396ad4.png",
|
|
|
|
"https://uploads.mangadex.org/data/f1117c5e7aff315bc3429a8791c89d63/A2-717ec3c83e8e05ed7b505941431a417ebfed6a005f78b89650efd3b088b951ec.png",
|
|
|
|
"https://uploads.mangadex.org/data/f1117c5e7aff315bc3429a8791c89d63/A3-95f1b873d75f7fb820cf293df903ca37264d4af8963f44d154418c529c737547.png",
|
|
|
|
"https://uploads.mangadex.org/data/f1117c5e7aff315bc3429a8791c89d63/A4-defb89c1919b7721d3b09338f175186cabe4e292e4925818a6982581378f1966.png",
|
|
|
|
"https://uploads.mangadex.org/data/f1117c5e7aff315bc3429a8791c89d63/A5-8d852ab3e9ddb070d8ba70bc5c04d78012032975b3a69603cc88a4a8d12652d4.png",
|
|
|
|
]
|
|
|
|
fail_url = (
|
|
|
|
"https://_uploads.mangadex.org/data/f1117c5e7aff315bc3429a8791c89d63/A4-defb89c1919b7721d3b09338f175186cabe4e292e4925818a6982581378f1966.png",
|
|
|
|
)
|
|
|
|
chapter_path = Path("tests/test_folder1")
|
|
|
|
chapter_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
monkeypatch.setattr(requests, "get", fail_url)
|
|
|
|
with pytest.raises(ConnectionError) as e:
|
2022-06-25 14:28:42 +02:00
|
|
|
downloader.download_chapter(images, str(chapter_path), 2, True)
|
2022-05-16 16:09:17 +02:00
|
|
|
|
|
|
|
assert e.type == ConnectionError
|
|
|
|
# cleanup
|
|
|
|
shutil.rmtree(chapter_path, ignore_errors=True)
|