Skip to content

Commit

Permalink
Improve core coverage - 1 (#1174)
Browse files Browse the repository at this point in the history
* improve coverage

split test_core.py

test creation|modification_date
  • Loading branch information
getzze authored Oct 3, 2024
1 parent 8e879cb commit 641b11a
Show file tree
Hide file tree
Showing 24 changed files with 3,243 additions and 524 deletions.
1 change: 1 addition & 0 deletions changelog.d/1174.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Tests] define "core" tests, move provider specific tests from test_core.py to providers/test_providers.py
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ addopts = "--import-mode=importlib --doctest-glob='*.rst'"
markers = [
"integration",
"converter",
"core",
]
doctest_optionflags = ["NORMALIZE_WHITESPACE", "IGNORE_EXCEPTION_DETAIL"]

Expand Down
8 changes: 6 additions & 2 deletions subliminal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ class ProviderPool:

#: Name of providers to use
providers: Sequence[str]

#: Provider configuration
provider_configs: Mapping[str, Any]

#: Initialized providers
initialized_providers: dict[str, Provider]

#: Discarded providers
discarded_providers: set[str]

Expand Down Expand Up @@ -297,6 +300,7 @@ class AsyncProviderPool(ProviderPool):
"""

#: Maximum number of threads to use.
max_workers: int

def __init__(self, max_workers: int | None = None, *args: Any, **kwargs: Any) -> None:
Expand Down Expand Up @@ -495,7 +499,7 @@ def scan_video(path: str | os.PathLike, name: str | None = None) -> Video:
return video


def scan_archive(path: str | os.PathLike, name: str | None = None) -> Video:
def scan_archive(path: str | os.PathLike, name: str | None = None) -> Video: # pragma: no cover
"""Scan an archive from a `path`.
:param str path: existing path to the archive.
Expand Down Expand Up @@ -632,7 +636,7 @@ def scan_videos(
# skip old files
try:
file_age = get_age(filepath, use_ctime=use_ctime)
except ValueError:
except ValueError: # pragma: no cover
logger.warning('Could not get age of file %r in %r', filename, dirpath)
continue
else:
Expand Down
4 changes: 2 additions & 2 deletions subliminal/matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def title_matches(video: Video, *, title: str | None = None, episode_title: str
return video.title is not None and sanitize(episode_title) == sanitize(video.title)
if isinstance(video, Movie):
return video.title is not None and sanitize(title) == sanitize(video.title)
return False
return False # pragma: no cover


def season_matches(video: Video, *, season: int | None = None, **kwargs: Any) -> bool:
Expand Down Expand Up @@ -142,7 +142,7 @@ def country_matches(video: Video, *, country: Country | None = None, partial: bo
if isinstance(video, Movie):
# count "no country" as an information
return video.country is None and country is None
return False
return False # pragma: no cover


def release_group_matches(video: Video, *, release_group: str | None = None, **kwargs: Any) -> bool:
Expand Down
7 changes: 4 additions & 3 deletions subliminal/providers/bsplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ def __init__(self, search_url: str | None = None, timeout: int = 10) -> None:

@staticmethod
def hash_video(video_path: str | os.PathLike) -> str | None:
"""Compute a hash using BSPlayer's algorithm.
"""Compute a hash using BSPlayer algorithm.
:param str video_path: path of the video.
:return: the hash.
:rtype: str.
:rtype: str
"""
little_endian_long_long = '<q' # little-endian long long
byte_size = struct.calcsize(little_endian_long_long)
Expand Down Expand Up @@ -235,8 +235,9 @@ def _api_request(self, func_name: str = 'logIn', params: str = '', tries: int =
:param str func_name: the type of request.
:param str params: xml string of parameters to send with the request.
:param int tries: the number of times to try making the request.
:return: the root XML element from the response.
:rtype: `xml.etree.ElementTree.Element`.
:rtype: `xml.etree.ElementTree.Element`
"""
headers = {
'User-Agent': 'BSPlayer/2.x (1022.12360)',
Expand Down
28 changes: 5 additions & 23 deletions subliminal/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from importlib.util import find_spec
from typing import TYPE_CHECKING, Any

from .utils import clip
from .video import Episode, Movie

if TYPE_CHECKING:
Expand Down Expand Up @@ -96,25 +97,6 @@ def __call__(self, subtitle: Subtitle, video: Video, *, hearing_impaired: bool |
equivalent_release_groups = ({'LOL', 'DIMENSION'}, {'ASAP', 'IMMERSE', 'FLEET'}, {'AVS', 'SVA'})


def clip(value: float, minimum: float | None, maximum: float | None) -> float:
"""Clip the value between a minimum and maximum.
Cheap replacement for the numpy.clip function.
:param float value: the value to clip (float or int).
:param (float | None) minimum: the minimum value (no minimum if None).
:param (float | None) maximum: the maximum value (no maximum if None).
:return: the clipped value.
:rtype: float
"""
if maximum is not None:
value = min(value, maximum)
if minimum is not None:
value = max(value, minimum)
return value


def get_equivalent_release_groups(release_group: str) -> set[str]:
"""Get all the equivalents of the given release group.
Expand Down Expand Up @@ -147,7 +129,7 @@ def get_scores(video: Video) -> dict[str, Any]:
return movie_scores

msg = 'video must be an instance of Episode or Movie' # pragma: no-cover
raise ValueError(msg)
raise ValueError(msg) # pragma: no-cover


def match_hearing_impaired(subtitle: Subtitle, *, hearing_impaired: bool | None = None) -> bool:
Expand Down Expand Up @@ -206,7 +188,7 @@ def compute_score(subtitle: Subtitle, video: Video, *, hearing_impaired: bool |
if 'series_tvdb_id' in matches:
logger.debug('Adding series_tvdb_id match equivalents')
matches |= {'series', 'year', 'country'}
elif isinstance(video, Movie):
elif isinstance(video, Movie): # pragma: no branch
if 'imdb_id' in matches:
logger.debug('Adding imdb_id match equivalents')
matches |= {'title', 'year', 'country'}
Expand All @@ -222,14 +204,14 @@ def compute_score(subtitle: Subtitle, video: Video, *, hearing_impaired: bool |

# ensure score is within valid bounds
max_score = scores['hash'] + scores['hearing_impaired']
if not (0 <= score <= max_score):
if not (0 <= score <= max_score): # pragma: no cover
logger.info('Clip score between 0 and %d: %d', max_score, score)
score = int(clip(score, 0, max_score))

return score


if WITH_SYMPY:
if WITH_SYMPY: # pragma: no cover
from sympy import Eq, Symbol, solve, symbols # type: ignore[import-untyped]

def solve_episode_equations() -> dict[Symbol, int]:
Expand Down
Loading

0 comments on commit 641b11a

Please sign in to comment.