2022-07-11 16:10:31 -07:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-07-18 09:00:56 -07:00
|
|
|
import copy
|
2023-12-17 15:27:02 -08:00
|
|
|
import datetime
|
2022-07-18 09:00:56 -07:00
|
|
|
import io
|
|
|
|
import shutil
|
2022-07-11 16:10:31 -07:00
|
|
|
import unittest.mock
|
2023-12-17 15:27:02 -08:00
|
|
|
from argparse import Namespace
|
2022-08-10 16:46:00 -07:00
|
|
|
from collections.abc import Generator
|
|
|
|
from typing import Any
|
2022-07-11 16:10:31 -07:00
|
|
|
|
|
|
|
import pytest
|
2023-07-28 23:25:12 -07:00
|
|
|
import settngs
|
2022-07-18 09:00:56 -07:00
|
|
|
from PIL import Image
|
Convert ComicIssue into GenericMetadata
I could not find a good reason for ComicIssue to exist other than that
it had more attributes than GenericMetadata, so it has been replaced.
New attributes for GenericMetadata:
series_id: a string uniquely identifying the series to tag_origin
series_aliases: alternate series names that are not the canonical name
title_aliases: alternate issue titles that are not the canonical name
alternate_images: a list of urls to alternate cover images
Updated attributes for GenericMetadata:
genre -> genres: str -> list[str]
comments -> description: str -> str
story_arc -> story_arcs: str -> list[str]
series_group -> series_groups: str -> list[str]
character -> characters: str -> list[str]
team -> teams: str -> list[str]
location -> locations: str -> list[str]
tag_origin -> tag_origin: str -> TagOrigin (tuple[str, str])
ComicSeries has been relocated to the ComicAPI package, currently has no
usage within ComicAPI.
CreditMetadata has been renamed to Credit and has replaced Credit from
ComicTalker.
fetch_series has been added to ComicTalker, this is currently only used
in the GUI when a series is selected and does not already contain the
needed fields, this function should always be cached.
A new split function has been added to ComicAPI, all uses of split on
single characters have been updated to use this
cleanup_html and the corresponding setting are now only used in
ComicTagger proper, for display we want any html directly from the
upstream. When applying the metadata we then strip the description of
any html.
A new conversion has been added to the MetadataFormatter:
j: joins any lists into a string with ', '. Note this is a valid
operation on strings as well, it will add ', ' in between every
character.
parse_settings now assigns the given ComicTaggerPaths object to the
result ensuring that the correct path is always used.
2023-08-02 09:00:04 -07:00
|
|
|
from pyrate_limiter import Limiter, RequestRate
|
2022-07-11 16:10:31 -07:00
|
|
|
|
2022-07-13 19:56:34 -07:00
|
|
|
import comicapi.comicarchive
|
2022-07-11 16:10:31 -07:00
|
|
|
import comicapi.genericmetadata
|
2023-12-17 15:27:02 -08:00
|
|
|
import comictaggerlib.cli
|
2023-01-31 20:21:39 -08:00
|
|
|
import comictaggerlib.ctsettings
|
2023-02-09 19:33:10 -08:00
|
|
|
import comictalker
|
2022-10-05 17:14:03 -07:00
|
|
|
import comictalker.comiccacher
|
|
|
|
import comictalker.talkers.comicvine
|
2022-07-11 16:10:31 -07:00
|
|
|
from comicapi import utils
|
2022-07-13 19:56:34 -07:00
|
|
|
from testing import comicvine, filenames
|
2022-07-11 16:10:31 -07:00
|
|
|
from testing.comicdata import all_seed_imprints, seed_imprints
|
|
|
|
|
2024-06-09 13:09:26 -07:00
|
|
|
try:
|
|
|
|
import niquests as requests
|
|
|
|
except ImportError:
|
|
|
|
import requests
|
|
|
|
|
2022-07-11 16:10:31 -07:00
|
|
|
|
2022-07-13 19:56:34 -07:00
|
|
|
@pytest.fixture
|
|
|
|
def cbz():
|
|
|
|
yield comicapi.comicarchive.ComicArchive(filenames.cbz_path)
|
|
|
|
|
|
|
|
|
2022-07-18 09:00:56 -07:00
|
|
|
@pytest.fixture
|
|
|
|
def tmp_comic(tmp_path):
|
|
|
|
shutil.copy(filenames.cbz_path, tmp_path)
|
|
|
|
yield comicapi.comicarchive.ComicArchive(tmp_path / filenames.cbz_path.name)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def cbz_double_cover(tmp_path, tmp_comic):
|
|
|
|
cover = Image.open(io.BytesIO(tmp_comic.get_page(0)))
|
|
|
|
|
|
|
|
other_page = Image.open(io.BytesIO(tmp_comic.get_page(tmp_comic.get_number_of_pages() - 1)))
|
|
|
|
|
|
|
|
double_cover = Image.new("RGB", (cover.width * 2, cover.height))
|
|
|
|
double_cover.paste(other_page, (0, 0))
|
|
|
|
double_cover.paste(cover, (cover.width, 0))
|
|
|
|
|
|
|
|
tmp_comic.archiver.write_file("double_cover.jpg", double_cover.tobytes("jpeg", "RGB"))
|
|
|
|
yield tmp_comic
|
|
|
|
|
|
|
|
|
2022-07-11 16:10:31 -07:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def no_requests(monkeypatch) -> None:
|
|
|
|
"""Remove requests.sessions.Session.request for all tests."""
|
2024-06-09 13:09:26 -07:00
|
|
|
try:
|
|
|
|
monkeypatch.delattr("niquests.sessions.Session.request")
|
|
|
|
except Exception:
|
|
|
|
...
|
|
|
|
try:
|
|
|
|
monkeypatch.delattr("requests.sessions.Session.request")
|
|
|
|
except Exception:
|
|
|
|
...
|
2022-07-11 16:10:31 -07:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2023-01-31 20:21:39 -08:00
|
|
|
def comicvine_api(monkeypatch, cbz, comic_cache, mock_version, config) -> comictalker.talkers.comicvine.ComicVineTalker:
|
2022-07-11 16:10:31 -07:00
|
|
|
# Any arguments may be passed and mock_get() will always return our
|
|
|
|
# mocked object, which only has the .json() method or None for invalid urls.
|
|
|
|
|
2022-07-18 09:00:56 -07:00
|
|
|
def make_list(cv_result):
|
|
|
|
cv_list = copy.deepcopy(cv_result)
|
|
|
|
if isinstance(cv_list["results"], dict):
|
|
|
|
cv_list["results"] = [cv_list["results"]]
|
|
|
|
return cv_list
|
|
|
|
|
2022-07-11 16:10:31 -07:00
|
|
|
def mock_get(*args, **kwargs):
|
|
|
|
if args:
|
|
|
|
if args[0].startswith("https://comicvine.gamespot.com/api/volume/4050-23437"):
|
2022-07-18 09:00:56 -07:00
|
|
|
cv_result = copy.deepcopy(comicvine.cv_volume_result)
|
|
|
|
comicvine.filter_field_list(cv_result["results"], kwargs)
|
|
|
|
return comicvine.MockResponse(cv_result)
|
|
|
|
if args[0].startswith("https://comicvine.gamespot.com/api/issue/4000-140529"):
|
2022-07-11 16:10:31 -07:00
|
|
|
return comicvine.MockResponse(comicvine.cv_issue_result)
|
2022-07-13 19:56:34 -07:00
|
|
|
if (
|
|
|
|
args[0].startswith("https://comicvine.gamespot.com/api/issues/")
|
|
|
|
and "params" in kwargs
|
|
|
|
and "filter" in kwargs["params"]
|
|
|
|
and "23437" in kwargs["params"]["filter"]
|
|
|
|
):
|
2022-07-18 09:00:56 -07:00
|
|
|
cv_list = make_list(comicvine.cv_issue_result)
|
|
|
|
for cv in cv_list["results"]:
|
|
|
|
comicvine.filter_field_list(cv, kwargs)
|
|
|
|
return comicvine.MockResponse(cv_list)
|
|
|
|
if (
|
|
|
|
args[0].startswith("https://comicvine.gamespot.com/api/search")
|
|
|
|
and "params" in kwargs
|
|
|
|
and "resources" in kwargs["params"]
|
|
|
|
and "volume" == kwargs["params"]["resources"]
|
|
|
|
):
|
|
|
|
cv_list = make_list(comicvine.cv_volume_result)
|
|
|
|
for cv in cv_list["results"]:
|
|
|
|
comicvine.filter_field_list(cv, kwargs)
|
2022-07-13 19:56:34 -07:00
|
|
|
return comicvine.MockResponse(cv_list)
|
2022-07-18 09:00:56 -07:00
|
|
|
if (
|
|
|
|
args[0]
|
|
|
|
== "https://comicvine.gamespot.com/a/uploads/scale_large/0/574/585444-109004_20080707014047_large.jpg"
|
|
|
|
):
|
|
|
|
return comicvine.MockResponse({}, cbz.get_page(0))
|
|
|
|
if (
|
|
|
|
args[0]
|
|
|
|
== "https://comicvine.gamespot.com/a/uploads/scale_avatar/0/574/585444-109004_20080707014047_large.jpg"
|
|
|
|
):
|
|
|
|
thumb = Image.open(io.BytesIO(cbz.get_page(0)))
|
|
|
|
thumb.resize((105, 160), Image.Resampling.LANCZOS)
|
|
|
|
return comicvine.MockResponse({}, thumb.tobytes("jpeg", "RGB"))
|
2022-07-13 19:56:34 -07:00
|
|
|
return comicvine.MockResponse(comicvine.cv_not_found)
|
2022-07-11 16:10:31 -07:00
|
|
|
|
|
|
|
m_get = unittest.mock.Mock(side_effect=mock_get)
|
|
|
|
|
|
|
|
# apply the monkeypatch for requests.get to mock_get
|
|
|
|
monkeypatch.setattr(requests, "get", m_get)
|
Convert ComicIssue into GenericMetadata
I could not find a good reason for ComicIssue to exist other than that
it had more attributes than GenericMetadata, so it has been replaced.
New attributes for GenericMetadata:
series_id: a string uniquely identifying the series to tag_origin
series_aliases: alternate series names that are not the canonical name
title_aliases: alternate issue titles that are not the canonical name
alternate_images: a list of urls to alternate cover images
Updated attributes for GenericMetadata:
genre -> genres: str -> list[str]
comments -> description: str -> str
story_arc -> story_arcs: str -> list[str]
series_group -> series_groups: str -> list[str]
character -> characters: str -> list[str]
team -> teams: str -> list[str]
location -> locations: str -> list[str]
tag_origin -> tag_origin: str -> TagOrigin (tuple[str, str])
ComicSeries has been relocated to the ComicAPI package, currently has no
usage within ComicAPI.
CreditMetadata has been renamed to Credit and has replaced Credit from
ComicTalker.
fetch_series has been added to ComicTalker, this is currently only used
in the GUI when a series is selected and does not already contain the
needed fields, this function should always be cached.
A new split function has been added to ComicAPI, all uses of split on
single characters have been updated to use this
cleanup_html and the corresponding setting are now only used in
ComicTagger proper, for display we want any html directly from the
upstream. When applying the metadata we then strip the description of
any html.
A new conversion has been added to the MetadataFormatter:
j: joins any lists into a string with ', '. Note this is a valid
operation on strings as well, it will add ', ' in between every
character.
parse_settings now assigns the given ComicTaggerPaths object to the
result ensuring that the correct path is always used.
2023-08-02 09:00:04 -07:00
|
|
|
monkeypatch.setattr(comictalker.talkers.comicvine, "custom_limiter", Limiter(RequestRate(100, 1)))
|
|
|
|
monkeypatch.setattr(comictalker.talkers.comicvine, "default_limiter", Limiter(RequestRate(100, 1)))
|
2022-10-14 18:02:10 -07:00
|
|
|
|
2022-11-25 15:26:26 -08:00
|
|
|
cv = comictalker.talkers.comicvine.ComicVineTalker(
|
2022-11-25 15:47:05 -08:00
|
|
|
version=mock_version[0],
|
2023-11-19 23:14:40 -08:00
|
|
|
cache_folder=config[0].Runtime_Options__config.user_cache_dir,
|
2022-11-25 15:26:26 -08:00
|
|
|
)
|
2023-07-28 23:25:12 -07:00
|
|
|
manager = settngs.Manager()
|
|
|
|
manager.add_persistent_group("comicvine", cv.register_settings)
|
|
|
|
cfg, _ = manager.defaults()
|
|
|
|
cfg["comicvine"]["comicvine_key"] = "testing"
|
|
|
|
cv.parse_settings(cfg["comicvine"])
|
2022-10-14 18:02:10 -07:00
|
|
|
return cv
|
2022-07-11 16:10:31 -07:00
|
|
|
|
|
|
|
|
2023-12-17 15:27:02 -08:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_now(monkeypatch):
|
|
|
|
class mydatetime:
|
|
|
|
time = datetime.datetime(2022, 4, 16, 15, 52, 26)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def now(cls):
|
|
|
|
return cls.time
|
|
|
|
|
2024-04-27 16:43:51 -07:00
|
|
|
monkeypatch.setattr(comictaggerlib.md, "datetime", mydatetime)
|
2023-12-17 15:27:02 -08:00
|
|
|
|
|
|
|
|
2022-07-12 07:43:33 -07:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_version(monkeypatch):
|
2023-12-17 15:27:02 -08:00
|
|
|
version = "1.3.2a5"
|
|
|
|
version_tuple = (1, 3, 2)
|
2022-07-12 07:43:33 -07:00
|
|
|
|
|
|
|
monkeypatch.setattr(comictaggerlib.ctversion, "version", version)
|
|
|
|
monkeypatch.setattr(comictaggerlib.ctversion, "__version__", version)
|
|
|
|
monkeypatch.setattr(comictaggerlib.ctversion, "version_tuple", version_tuple)
|
|
|
|
monkeypatch.setattr(comictaggerlib.ctversion, "__version_tuple__", version_tuple)
|
2023-06-09 16:20:00 -07:00
|
|
|
yield version, version_tuple
|
2022-07-12 07:43:33 -07:00
|
|
|
|
|
|
|
|
2022-07-11 16:10:31 -07:00
|
|
|
@pytest.fixture
|
|
|
|
def md():
|
2022-07-18 09:00:56 -07:00
|
|
|
yield comicapi.genericmetadata.md_test.copy()
|
2022-07-11 16:10:31 -07:00
|
|
|
|
|
|
|
|
Convert ComicIssue into GenericMetadata
I could not find a good reason for ComicIssue to exist other than that
it had more attributes than GenericMetadata, so it has been replaced.
New attributes for GenericMetadata:
series_id: a string uniquely identifying the series to tag_origin
series_aliases: alternate series names that are not the canonical name
title_aliases: alternate issue titles that are not the canonical name
alternate_images: a list of urls to alternate cover images
Updated attributes for GenericMetadata:
genre -> genres: str -> list[str]
comments -> description: str -> str
story_arc -> story_arcs: str -> list[str]
series_group -> series_groups: str -> list[str]
character -> characters: str -> list[str]
team -> teams: str -> list[str]
location -> locations: str -> list[str]
tag_origin -> tag_origin: str -> TagOrigin (tuple[str, str])
ComicSeries has been relocated to the ComicAPI package, currently has no
usage within ComicAPI.
CreditMetadata has been renamed to Credit and has replaced Credit from
ComicTalker.
fetch_series has been added to ComicTalker, this is currently only used
in the GUI when a series is selected and does not already contain the
needed fields, this function should always be cached.
A new split function has been added to ComicAPI, all uses of split on
single characters have been updated to use this
cleanup_html and the corresponding setting are now only used in
ComicTagger proper, for display we want any html directly from the
upstream. When applying the metadata we then strip the description of
any html.
A new conversion has been added to the MetadataFormatter:
j: joins any lists into a string with ', '. Note this is a valid
operation on strings as well, it will add ', ' in between every
character.
parse_settings now assigns the given ComicTaggerPaths object to the
result ensuring that the correct path is always used.
2023-08-02 09:00:04 -07:00
|
|
|
@pytest.fixture
|
|
|
|
def md_saved():
|
2024-06-20 16:47:10 -07:00
|
|
|
yield comicapi.genericmetadata.md_test.replace(data_origin=None, issue_id=None, series_id=None)
|
Convert ComicIssue into GenericMetadata
I could not find a good reason for ComicIssue to exist other than that
it had more attributes than GenericMetadata, so it has been replaced.
New attributes for GenericMetadata:
series_id: a string uniquely identifying the series to tag_origin
series_aliases: alternate series names that are not the canonical name
title_aliases: alternate issue titles that are not the canonical name
alternate_images: a list of urls to alternate cover images
Updated attributes for GenericMetadata:
genre -> genres: str -> list[str]
comments -> description: str -> str
story_arc -> story_arcs: str -> list[str]
series_group -> series_groups: str -> list[str]
character -> characters: str -> list[str]
team -> teams: str -> list[str]
location -> locations: str -> list[str]
tag_origin -> tag_origin: str -> TagOrigin (tuple[str, str])
ComicSeries has been relocated to the ComicAPI package, currently has no
usage within ComicAPI.
CreditMetadata has been renamed to Credit and has replaced Credit from
ComicTalker.
fetch_series has been added to ComicTalker, this is currently only used
in the GUI when a series is selected and does not already contain the
needed fields, this function should always be cached.
A new split function has been added to ComicAPI, all uses of split on
single characters have been updated to use this
cleanup_html and the corresponding setting are now only used in
ComicTagger proper, for display we want any html directly from the
upstream. When applying the metadata we then strip the description of
any html.
A new conversion has been added to the MetadataFormatter:
j: joins any lists into a string with ', '. Note this is a valid
operation on strings as well, it will add ', ' in between every
character.
parse_settings now assigns the given ComicTaggerPaths object to the
result ensuring that the correct path is always used.
2023-08-02 09:00:04 -07:00
|
|
|
|
|
|
|
|
2022-07-11 16:10:31 -07:00
|
|
|
# manually seeds publishers
|
|
|
|
@pytest.fixture
|
|
|
|
def seed_publishers(monkeypatch):
|
|
|
|
publisher_seed = {}
|
|
|
|
for publisher, imprint in seed_imprints.items():
|
|
|
|
publisher_seed[publisher] = imprint
|
|
|
|
monkeypatch.setattr(utils, "publishers", publisher_seed)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def seed_all_publishers(monkeypatch):
|
|
|
|
publisher_seed = {}
|
|
|
|
for publisher, imprint in all_seed_imprints.items():
|
|
|
|
publisher_seed[publisher] = imprint
|
|
|
|
monkeypatch.setattr(utils, "publishers", publisher_seed)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2023-06-09 16:20:00 -07:00
|
|
|
def config(tmp_path):
|
|
|
|
from comictaggerlib.main import App
|
|
|
|
|
|
|
|
app = App()
|
2024-08-18 19:16:55 -07:00
|
|
|
app.register_settings(False)
|
2023-06-09 16:20:00 -07:00
|
|
|
|
|
|
|
defaults = app.parse_settings(comictaggerlib.ctsettings.ComicTaggerPaths(tmp_path / "config"), "")
|
2023-11-19 23:14:40 -08:00
|
|
|
defaults[0].Runtime_Options__config.user_config_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
defaults[0].Runtime_Options__config.user_cache_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
defaults[0].Runtime_Options__config.user_log_dir.mkdir(parents=True, exist_ok=True)
|
2024-02-10 21:00:24 -08:00
|
|
|
defaults[0].Runtime_Options__config.user_plugin_dir.mkdir(parents=True, exist_ok=True)
|
2022-12-06 00:20:01 -08:00
|
|
|
yield defaults
|
2022-07-11 16:10:31 -07:00
|
|
|
|
|
|
|
|
2023-12-17 15:27:02 -08:00
|
|
|
@pytest.fixture
|
|
|
|
def plugin_config(tmp_path):
|
|
|
|
from comictaggerlib.main import App
|
|
|
|
|
|
|
|
ns = Namespace(config=comictaggerlib.ctsettings.ComicTaggerPaths(tmp_path / "config"))
|
2024-09-17 15:32:01 -07:00
|
|
|
ns.config.user_config_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
ns.config.user_cache_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
ns.config.user_log_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
ns.config.user_plugin_dir.mkdir(parents=True, exist_ok=True)
|
2023-12-17 15:27:02 -08:00
|
|
|
app = App()
|
|
|
|
app.load_plugins(ns)
|
2024-08-18 19:16:55 -07:00
|
|
|
app.register_settings(False)
|
2023-12-17 15:27:02 -08:00
|
|
|
|
|
|
|
defaults = app.parse_settings(ns.config, "")
|
|
|
|
yield (defaults, app.talkers)
|
|
|
|
|
|
|
|
|
2022-07-11 16:10:31 -07:00
|
|
|
@pytest.fixture
|
2023-01-31 20:21:39 -08:00
|
|
|
def comic_cache(config, mock_version) -> Generator[comictalker.comiccacher.ComicCacher, Any, None]:
|
2023-11-19 23:14:40 -08:00
|
|
|
yield comictalker.comiccacher.ComicCacher(config[0].Runtime_Options__config.user_cache_dir, mock_version[0])
|