add tests for hooks

This commit is contained in:
Ivan Schaller 2022-08-14 16:34:15 +02:00
parent c2d9ca9f72
commit f389f2777f
5 changed files with 214 additions and 4 deletions

View file

@ -2,6 +2,22 @@
class YourAPI:
"""Your API Class.
Get infos for a manga from example.org
Args:
url_uuid (str): URL or UUID of the manga
language (str): Manga language with country codes. "en" --> english
forcevol (bool): Force naming of volumes. Useful for mangas where chapters reset each volume
Attributes:
api_name (str): Name of the API
manga_uuid (str): UUID of the manga, without the url part
manga_title (str): The title of the manga, sanitized for all filesystems
chapter_list (list): A list of all available chapters for the language
"""
# api information - example
api_base_url = "https://api.mangadex.org"
img_base_url = "https://uploads.mangadex.org"

View file

@ -21,6 +21,7 @@ class Mangadex:
forcevol (bool): Force naming of volumes. Useful for mangas where chapters reset each volume
Attributes:
api_name (str): Name of the API
manga_uuid (str): UUID of the manga, without the url part
manga_data (dict): Infos of the manga. Name, title etc
manga_title (str): The title of the manga, sanitized for all filesystems

View file

@ -78,6 +78,7 @@ class MangaDLP:
self.manga_title = self.api.manga_title
# get chapter list
self.manga_chapter_list = self.api.chapter_list
self.manga_total_chapters = len(self.manga_chapter_list)
self.manga_path = Path(f"{self.download_path}/{self.manga_title}")
def pre_checks(self) -> None:
@ -137,7 +138,7 @@ class MangaDLP:
log.info(f"{print_divider}")
log.lean(f"Manga Name: {self.manga_title}")
log.info(f"Manga UUID: {self.manga_uuid}")
log.info(f"Total chapters: {len(self.manga_chapter_list)}")
log.info(f"Total chapters: {self.manga_total_chapters}")
# list chapters if list_chapters is true
if self.list_chapters:
@ -168,7 +169,7 @@ class MangaDLP:
"manga_uuid": self.manga_uuid,
"manga_title": self.manga_title,
"language": self.language,
"total_chapters": len(self.manga_chapter_list),
"total_chapters": self.manga_total_chapters,
"chapters_to_download": chapters_to_download,
"file_format": self.file_format,
"forcevol": self.forcevol,

View file

@ -4,7 +4,7 @@ import logging
# prepare custom levels and default config of logger
def prepare_logger():
logging.basicConfig(
format="%(asctime)s | [%(levelname)s][%(name)s]: %(message)s",
format="%(asctime)s | [%(levelname)s] [%(name)s]: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=20,
handlers=[logging.StreamHandler()],
@ -26,7 +26,7 @@ def format_logger(verbosity: int):
)
else:
logging.basicConfig(
format="%(asctime)s | [%(levelname)s][%(name)s]: %(message)s",
format="%(asctime)s | [%(levelname)s] [%(name)s]: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
force=True,
)

192
tests/test_05_hooks.py Normal file
View file

@ -0,0 +1,192 @@
import shutil
import subprocess
import time
from pathlib import Path
import pytest
@pytest.fixture
def wait_10s():
print("sleeping 10 seconds because of api timeouts")
time.sleep(10)
@pytest.fixture
def wait_20s():
print("sleeping 20 seconds because of api timeouts")
time.sleep(20)
def test_manga_pre_hook(wait_10s):
url_uuid = "https://mangadex.org/title/0aea9f43-d4a9-4bf7-bebc-550a512f9b95/shikimori-s-not-just-a-cutie"
manga_path = Path("tests/Shikimori's Not Just a Cutie")
language = "en"
chapters = "1"
download_path = "tests"
manga_pre_hook = "touch tests/manga-pre.txt"
hook_file = Path("tests/manga-pre.txt")
command_args = [
"-u",
url_uuid,
"-l",
language,
"-c",
chapters,
"--path",
download_path,
"--debug",
"--hook-manga-pre",
manga_pre_hook,
]
script_path = "manga-dlp.py"
command = ["python3", script_path] + command_args
assert subprocess.call(command) == 0
assert hook_file.is_file()
# cleanup
shutil.rmtree(manga_path, ignore_errors=True)
hook_file.unlink()
def test_manga_post_hook(wait_10s):
url_uuid = "https://mangadex.org/title/0aea9f43-d4a9-4bf7-bebc-550a512f9b95/shikimori-s-not-just-a-cutie"
manga_path = Path("tests/Shikimori's Not Just a Cutie")
language = "en"
chapters = "1"
download_path = "tests"
manga_post_hook = "touch tests/manga-post.txt"
hook_file = Path("tests/manga-post.txt")
command_args = [
"-u",
url_uuid,
"-l",
language,
"-c",
chapters,
"--path",
download_path,
"--debug",
"--hook-manga-post",
manga_post_hook,
]
script_path = "manga-dlp.py"
command = ["python3", script_path] + command_args
assert subprocess.call(command) == 0
assert hook_file.is_file()
# cleanup
shutil.rmtree(manga_path, ignore_errors=True)
hook_file.unlink()
def test_chapter_pre_hook(wait_10s):
url_uuid = "https://mangadex.org/title/0aea9f43-d4a9-4bf7-bebc-550a512f9b95/shikimori-s-not-just-a-cutie"
manga_path = Path("tests/Shikimori's Not Just a Cutie")
language = "en"
chapters = "1"
download_path = "tests"
chapter_pre_hook = "touch tests/chapter-pre.txt"
hook_file = Path("tests/chapter-pre.txt")
command_args = [
"-u",
url_uuid,
"-l",
language,
"-c",
chapters,
"--path",
download_path,
"--debug",
"--hook-chapter-pre",
chapter_pre_hook,
]
script_path = "manga-dlp.py"
command = ["python3", script_path] + command_args
assert subprocess.call(command) == 0
assert hook_file.is_file()
# cleanup
shutil.rmtree(manga_path, ignore_errors=True)
hook_file.unlink()
def test_chapter_post_hook(wait_10s):
url_uuid = "https://mangadex.org/title/0aea9f43-d4a9-4bf7-bebc-550a512f9b95/shikimori-s-not-just-a-cutie"
manga_path = Path("tests/Shikimori's Not Just a Cutie")
language = "en"
chapters = "1"
download_path = "tests"
chapter_post_hook = "touch tests/chapter-post.txt"
hook_file = Path("tests/chapter-post.txt")
command_args = [
"-u",
url_uuid,
"-l",
language,
"-c",
chapters,
"--path",
download_path,
"--debug",
"--hook-chapter-post",
chapter_post_hook,
]
script_path = "manga-dlp.py"
command = ["python3", script_path] + command_args
assert subprocess.call(command) == 0
assert hook_file.is_file()
# cleanup
shutil.rmtree(manga_path, ignore_errors=True)
hook_file.unlink()
def test_all_hooks(wait_10s):
url_uuid = "https://mangadex.org/title/0aea9f43-d4a9-4bf7-bebc-550a512f9b95/shikimori-s-not-just-a-cutie"
manga_path = Path("tests/Shikimori's Not Just a Cutie")
language = "en"
chapters = "1"
download_path = "tests"
manga_pre_hook = "touch tests/manga-pre2.txt"
manga_post_hook = "touch tests/manga-post2.txt"
chapter_pre_hook = "touch tests/chapter-pre2.txt"
chapter_post_hook = "touch tests/chapter-post2.txt"
command_args = [
"-u",
url_uuid,
"-l",
language,
"-c",
chapters,
"--path",
download_path,
"--debug",
"--hook-manga-pre",
manga_pre_hook,
"--hook-manga-post",
manga_post_hook,
"--hook-chapter-pre",
chapter_pre_hook,
"--hook-chapter-post",
chapter_post_hook,
]
script_path = "manga-dlp.py"
command = ["python3", script_path] + command_args
assert subprocess.call(command) == 0
assert Path("tests/manga-pre2.txt").is_file()
assert Path("tests/manga-post2.txt").is_file()
assert Path("tests/chapter-pre2.txt").is_file()
assert Path("tests/chapter-post2.txt").is_file()
# cleanup
shutil.rmtree(manga_path, ignore_errors=True)
Path("tests/manga-pre2.txt").unlink()
Path("tests/manga-post2.txt").unlink()
Path("tests/chapter-pre2.txt").unlink()
Path("tests/chapter-post2.txt").unlink()