Browse Source

Correct sorting test

pull/1935/head
Safihre 4 years ago
parent
commit
82fa42d182
  1. 58
      sabnzbd/sorting.py
  2. 33
      tests/test_sorting.py

58
sabnzbd/sorting.py

@ -41,7 +41,6 @@ import sabnzbd.cfg as cfg
from sabnzbd.constants import EXCLUDED_GUESSIT_PROPERTIES from sabnzbd.constants import EXCLUDED_GUESSIT_PROPERTIES
from sabnzbd.nzbstuff import NzbObject, scan_password from sabnzbd.nzbstuff import NzbObject, scan_password
# Do not rename .vob files as they are usually DVD's # Do not rename .vob files as they are usually DVD's
EXCLUDED_FILE_EXTS = (".vob", ".bin") EXCLUDED_FILE_EXTS = (".vob", ".bin")
@ -92,6 +91,10 @@ class BaseSorter:
"""Implemented by child classes""" """Implemented by child classes"""
pass pass
def get_values(self):
"""Implemented by child classes"""
pass
def get_final_path(self) -> str: def get_final_path(self) -> str:
if self.matched: if self.matched:
# Construct the final path # Construct the final path
@ -335,10 +338,6 @@ class Sorter:
return self.sorter.get_final_path() if self.sort_file else complete_dir return self.sorter.get_final_path() if self.sort_file else complete_dir
def rename(self, newfiles: List[str], workdir_complete: str) -> Tuple[str, bool]:
"""Rename files of the job"""
return self.sorter.rename(newfiles, workdir_complete)
class SeriesSorter(BaseSorter): class SeriesSorter(BaseSorter):
"""Methods for Series Sorting""" """Methods for Series Sorting"""
@ -357,19 +356,12 @@ class SeriesSorter(BaseSorter):
def match(self): def match(self):
"""Try to guess series info if config and category sort out or force is set""" """Try to guess series info if config and category sort out or force is set"""
if self.force or (cfg.enable_tv_sorting() and cfg.tv_sort_string()): if self.force or (cfg.enable_tv_sorting() and cfg.tv_sort_string() and self.cat.lower() in self.cats):
if ( self.guess = guess_what(self.original_job_name, sort_type="episode")
self.force if self.guess.get("type") == "episode" and "date" not in self.guess:
or (not self.cats) logging.debug("Using tv sorter for %s", self.original_job_name)
or (self.cat and self.cat.lower() in self.cats) self.matched = True
or (not self.cat and "None" in self.cats) self.type = "tv"
):
if not self.guess:
self.guess = guess_what(self.original_job_name, sort_type="episode")
if self.guess.get("type") == "episode" and not "date" in self.guess:
logging.debug("Using tv sorter for %s", self.original_job_name)
self.matched = True
self.type = "tv"
def get_values(self): def get_values(self):
"""Collect all values needed for path replacement""" """Collect all values needed for path replacement"""
@ -427,14 +419,12 @@ class MovieSorter(BaseSorter):
def match(self): def match(self):
"""Try to guess movie info if config and category sort out or force is set""" """Try to guess movie info if config and category sort out or force is set"""
if self.force or (cfg.enable_movie_sorting() and self.sort_string): if self.force or (cfg.enable_movie_sorting() and self.sort_string and self.cat.lower() in self.cats):
if self.force or (self.cat and self.cat.lower() in self.cats) or (not self.cat and "None" in self.cats): self.guess = guess_what(self.original_job_name, sort_type="movie")
if not self.guess: if self.guess.get("type") == "movie":
self.guess = guess_what(self.original_job_name, sort_type="movie") logging.debug("Using movie sorter for %s", self.original_job_name)
if self.guess.get("type") == "movie": self.matched = True
logging.debug("Using movie sorter for %s", self.original_job_name) self.type = "movie"
self.matched = True
self.type = "movie"
def get_values(self): def get_values(self):
"""Collect all values needed for path replacement""" """Collect all values needed for path replacement"""
@ -509,14 +499,12 @@ class DateSorter(BaseSorter):
def match(self): def match(self):
"""Checks the category for a match, if so set self.matched to true""" """Checks the category for a match, if so set self.matched to true"""
if self.force or (cfg.enable_date_sorting() and self.sort_string): if self.force or (cfg.enable_date_sorting() and self.sort_string and self.cat.lower() in self.cats):
if self.force or (self.cat and self.cat.lower() in self.cats) or (not self.cat and "None" in self.cats): self.guess = guess_what(self.original_job_name, sort_type="episode")
if not self.guess: if self.guess.get("type") == "episode" and "date" in self.guess:
self.guess = guess_what(self.original_job_name, sort_type="episode") logging.debug("Using date sorter for %s", self.original_job_name)
if self.guess.get("type") == "episode" and "date" in self.guess: self.matched = True
logging.debug("Using date sorter for %s", self.original_job_name) self.type = "date"
self.matched = True
self.type = "date"
def get_date(self): def get_date(self):
"""Get month and day""" """Get month and day"""
@ -607,7 +595,7 @@ def guess_what(name: str, sort_type: Optional[str] = None) -> MatchesDict:
guess["title"] = guess.get("title", "")[len(digit_fix) :] guess["title"] = guess.get("title", "")[len(digit_fix) :]
# Force season to 1 for seasonless episodes with no date # Force season to 1 for seasonless episodes with no date
if guess.get("type") == "episode" and not "date" in guess: if guess.get("type") == "episode" and "date" not in guess:
guess.setdefault("season", 1) guess.setdefault("season", 1)
# Try to avoid setting the type to movie on arbitrary jobs (e.g. 'Setup.exe') just because guessit defaults to that # Try to avoid setting the type to movie on arbitrary jobs (e.g. 'Setup.exe') just because guessit defaults to that

33
tests/test_sorting.py

@ -681,15 +681,32 @@ class TestSortingSorters:
) )
def test_sorter_generic(self, job_name, result_sort_file, result_class): def test_sorter_generic(self, job_name, result_sort_file, result_class):
"""Check if the generic sorter makes the right choices""" """Check if the generic sorter makes the right choices"""
generic = sorting.Sorter(None, None)
generic.detect(job_name, SAB_CACHE_DIR)
assert generic.sort_file is result_sort_file @set_config(
if result_sort_file: {
assert generic.sorter "tv_sort_string": "test", # TV
assert generic.sorter.__class__ is result_class "tv_categories": "test_cat",
else: "enable_tv_sorting": 1,
assert not generic.sorter "movie_sort_string": "test", # Movie
"movie_categories": "test_cat",
"enable_movie_sorting": 1,
"date_sort_string": "test", # Date
"date_categories": "test_cat",
"enable_date_sorting": 1,
}
)
def _func():
generic = sorting.Sorter(None, "test_cat")
generic.detect(job_name, SAB_CACHE_DIR)
assert generic.sort_file is result_sort_file
if result_sort_file:
assert generic.sorter
assert generic.sorter.__class__ is result_class
else:
assert not generic.sorter
_func()
@pytest.mark.parametrize( @pytest.mark.parametrize(
"name, result", "name, result",

Loading…
Cancel
Save