add more tests

This commit is contained in:
Ivan Schaller 2022-05-11 14:22:57 +02:00
parent 48de793c63
commit c80d68fe8e
6 changed files with 195 additions and 64 deletions

View file

@ -1,4 +1,5 @@
import pytest
import requests
from mangadlp.api.mangadex import Mangadex
@ -7,8 +8,9 @@ def test_uuid_link():
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
lang = "en"
forcevol = False
verbose = False
verbose = True
test = Mangadex(url, lang, forcevol, verbose)
assert test.manga_uuid == "a96676e5-8ae2-425e-b549-7f15dd34a6d8"
@ -16,17 +18,31 @@ def test_uuid_pure():
url = "a96676e5-8ae2-425e-b549-7f15dd34a6d8"
lang = "en"
forcevol = False
verbose = False
verbose = True
test = Mangadex(url, lang, forcevol, verbose)
assert test.manga_uuid == "a96676e5-8ae2-425e-b549-7f15dd34a6d8"
def test_uuid_link_false():
url = "https://mangadex.org/title/a966-76e-5-8a-e2-42-5e-b-549-7f15dd-34a6d8/komi-san-wa-komyushou-desu"
lang = "en"
forcevol = False
verbose = True
with pytest.raises(SystemExit) as e:
Mangadex(url, lang, forcevol, verbose)
assert e.type == SystemExit
assert e.value.code == 1
def test_title():
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
lang = "en"
forcevol = False
verbose = False
verbose = True
test = Mangadex(url, lang, forcevol, verbose)
assert test.manga_title == "Komi-san wa Komyushou Desu"
@ -34,13 +50,14 @@ def test_chapter_infos():
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
lang = "en"
forcevol = False
verbose = False
verbose = True
test = Mangadex(url, lang, forcevol, verbose)
chapter_infos = test.get_chapter_infos("1")
chapter_uuid = chapter_infos["uuid"]
chapter_name = chapter_infos["name"]
chapter_num = chapter_infos["chapter"]
chapter_volume = chapter_infos["volume"]
assert [chapter_uuid, chapter_name, chapter_volume, chapter_num] == [
"e86ec2c4-c5e4-4710-bfaa-7604f00939c7",
"A Normal Person",
@ -49,20 +66,63 @@ def test_chapter_infos():
]
def test_non_existing_manga():
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-999999999999/komi-san-wa-komyushou-desu"
lang = "en"
forcevol = False
verbose = True
with pytest.raises(SystemExit) as e:
Mangadex(url, lang, forcevol, verbose)
assert e.type == SystemExit
assert e.value.code == 1
def test_api_failure(monkeypatch):
fail_url = (
"https://api.mangadex.nonexistant/manga/a96676e5-8ae2-425e-b549-7f15dd34a6d8"
)
monkeypatch.setattr(requests, "get", fail_url)
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
lang = "en"
forcevol = False
verbose = True
with pytest.raises(SystemExit) as e:
Mangadex(url, lang, forcevol, verbose)
assert e.type == SystemExit
assert e.value.code == 1
def test_chapter_lang_en():
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
lang = "en"
forcevol = False
verbose = False
verbose = True
test = Mangadex(url, lang, forcevol, verbose)
assert test.check_chapter_lang() > 0
def test_empty_chapter_lang():
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
lang = "ch"
forcevol = False
verbose = True
with pytest.raises(SystemExit) as e:
Mangadex(url, lang, forcevol, verbose)
Mangadex(url, lang, forcevol, verbose).check_chapter_lang()
assert e.type == KeyError or e.type == SystemExit
assert e.value.code == 1
def test_not_existing_lang():
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
lang = "zz"
forcevol = False
verbose = False
verbose = True
with pytest.raises(SystemExit) as e:
Mangadex(url, lang, forcevol, verbose)
assert e.type == SystemExit
@ -73,8 +133,9 @@ def test_filename():
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
lang = "en"
forcevol = False
verbose = False
verbose = True
test = Mangadex(url, lang, forcevol, verbose)
assert test.get_filename("1") == "Ch. 1 - A Normal Person"
@ -82,8 +143,9 @@ def test_filename_forcevol():
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
lang = "en"
forcevol = True
verbose = False
verbose = True
test = Mangadex(url, lang, forcevol, verbose)
assert test.get_filename("1:4") == "Vol. 1 Ch. 4 - Bad at This"
@ -91,7 +153,7 @@ def test_create_chapter_list():
url = "https://mangadex.org/title/6fef1f74-a0ad-4f0d-99db-d32a7cd24098/fire-punch"
lang = "en"
forcevol = False
verbose = False
verbose = True
test = Mangadex(url, lang, forcevol, verbose)
test_list = [
"1",
@ -116,6 +178,7 @@ def test_create_chapter_list():
"20",
"21",
]
assert test.create_chapter_list() == test_list
@ -123,7 +186,7 @@ def test_create_chapter_list_forcevol():
url = "https://mangadex.org/title/6fef1f74-a0ad-4f0d-99db-d32a7cd24098/fire-punch"
lang = "en"
forcevol = True
verbose = False
verbose = True
test = Mangadex(url, lang, forcevol, verbose)
test_list = [
"1:1",
@ -148,6 +211,7 @@ def test_create_chapter_list_forcevol():
"3:20",
"3:21",
]
assert test.create_chapter_list() == test_list
@ -155,7 +219,7 @@ def test_get_chapter_images():
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
lang = "en"
forcevol = False
verbose = False
verbose = True
test = Mangadex(url, lang, forcevol, verbose)
img_base_url = "https://uploads.mangadex.org"
chapter_hash = "0752bc5db298beff6b932b9151dd8437"
@ -178,3 +242,18 @@ def test_get_chapter_images():
f"{img_base_url}/data/{chapter_hash}/x14-f6ed71bbb9af2bceab51028b460813c57935c923e1872fb277beb21d54425434.jpg",
]
assert test.get_chapter_images(chapter_num, 0.5) == test_list
def test_get_chapter_images_error(monkeypatch):
fail_url = (
"https://api.mangadex.org/at-home/server/e86ec2c4-c5e4-4710-bfaa-999999999999"
)
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
lang = "en"
forcevol = False
verbose = True
test = Mangadex(url, lang, forcevol, verbose)
chapter_num = "1"
monkeypatch.setattr(requests, "get", fail_url)
assert not test.get_chapter_images(chapter_num, 0.5)

0
tests/test_file Normal file
View file

0
tests/test_file.cbz Normal file
View file

58
tests/test_full.py Normal file
View file

@ -0,0 +1,58 @@
import os
import shutil
from pathlib import Path
import mangadlp.main as MdlpMain
def test_full_mangadex():
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
lang = "en"
chapters = "1"
readlist = ""
list_chapters = False
nocbz = False
forcevol = False
download_path = "tests"
download_wait = 1
verbose = True
manga_path = Path("tests/Komi-san wa Komyushou Desu")
chapter_path = Path("tests/Komi-san wa Komyushou Desu/Ch. 1 - A Normal Person.cbz")
MdlpMain.main(
url,
lang,
chapters,
readlist,
list_chapters,
nocbz,
forcevol,
download_path,
download_wait,
verbose,
)
assert manga_path.exists()
assert chapter_path.exists()
# cleanup
shutil.rmtree(manga_path, ignore_errors=True)
def test_full_with_input():
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
lang = "en"
chapters = "1"
download_path = "tests"
manga_path = Path("tests/Komi-san wa Komyushou Desu")
chapter_path = Path("tests/Komi-san wa Komyushou Desu/Ch. 1 - A Normal Person.cbz")
command_args = f"-u {url} -l {lang} -c {chapters} --path {download_path}"
script_path = "manga-dlp.py"
os.system(f"python3 {script_path} {command_args}")
assert manga_path.exists()
assert chapter_path.exists()
# cleanup
shutil.rmtree(manga_path, ignore_errors=True)
def test_full_without_input():
script_path = "manga-dlp.py"
assert os.system(f"python3 {script_path}") != 0

View file

@ -1,7 +1,7 @@
import os
import shutil
from pathlib import Path
import runpy
import mangadlp.main as MdlpMain
from mangadlp.api.mangadex import Mangadex
@ -22,52 +22,3 @@ def test_check_api():
test = MdlpMain.check_api(url)
assert test == Mangadex
def test_full():
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
lang = "en"
chapters = "1"
readlist = ""
list_chapters = False
nocbz = False
forcevol = False
download_path = "tests"
download_wait = 1
verbose = False
manga_path = Path("tests/Komi-san wa Komyushou Desu")
chapter_path = Path("tests/Komi-san wa Komyushou Desu/Ch. 1 - A Normal Person.cbz")
MdlpMain.main(
url,
lang,
chapters,
readlist,
list_chapters,
nocbz,
forcevol,
download_path,
download_wait,
verbose,
)
assert manga_path.exists()
assert chapter_path.exists()
# cleanup
shutil.rmtree(manga_path, ignore_errors=True)
def test_full_with_input():
url = "https://mangadex.org/title/a96676e5-8ae2-425e-b549-7f15dd34a6d8/komi-san-wa-komyushou-desu"
lang = "en"
chapters = "1"
download_path = "tests"
manga_path = Path("tests/Komi-san wa Komyushou Desu")
chapter_path = Path("tests/Komi-san wa Komyushou Desu/Ch. 1 - A Normal Person.cbz")
command_args = f"-u {url} -l {lang} -c {chapters} --path {download_path}"
script_path = "manga-dlp.py"
os.system(f"python3 {script_path} {command_args}")
assert manga_path.exists()
assert chapter_path.exists()
# cleanup
shutil.rmtree(manga_path, ignore_errors=True)

View file

@ -1,17 +1,32 @@
from pathlib import Path
import shutil
from pathlib import Path
import mangadlp.utils as utils
def test_existence_true():
path = "README.md"
path = "tests/test_file"
nocbz = False
test = utils.check_existence(path, nocbz)
assert test
def test_existence_true_nocbz():
path = "tests/test_file"
nocbz = True
test = utils.check_existence(path, nocbz)
assert test
def test_existence_false():
path = "DONTEXIST.md"
path = "tests/test_file_nonexistent"
nocbz = False
test = utils.check_existence(path, nocbz)
assert not test
def test_existence_false_nocbz():
path = "tests/test_file_nonexistent"
nocbz = True
test = utils.check_existence(path, nocbz)
assert not test
@ -44,6 +59,12 @@ def test_chapter_list():
assert utils.get_chapter_list(chapters_in) == chapters_out
def test_chapter_list_forcevol():
chapters_in = "1:1-1:4,2:8,3:11,4:14-4:15,5:22"
chapters_out = ["1:1", "1:2", "1:3", "1:4", "2:8", "3:11", "4:14", "4:15", "5:22"]
assert utils.get_chapter_list(chapters_in) == chapters_out
def test_fix_name():
filename_in1 = "..hello?; @test1-*<>test2.cbz"
filename_in2 = "!hello: >test1-/test2<!.cbz"
@ -68,6 +89,17 @@ def test_get_filename_forcevol():
)
def test_get_filename_forcevol_noname():
chapter_name = ""
chapter_vol = "2"
chapter_num = "44"
forcevol = True
filename = "Vol. 2 Ch. 44"
assert (
utils.get_filename(chapter_name, chapter_vol, chapter_num, forcevol) == filename
)
def test_get_filename():
chapter_name = "The holy test Chapter"
chapter_vol = "2"
@ -88,3 +120,14 @@ def test_get_filename_oneshot():
assert (
utils.get_filename(chapter_name, chapter_vol, chapter_num, forcevol) == filename
)
def test_get_filename_noname():
chapter_name = ""
chapter_vol = "1"
chapter_num = "1"
forcevol = False
filename = "Ch. 1"
assert (
utils.get_filename(chapter_name, chapter_vol, chapter_num, forcevol) == filename
)