101 lines
3.6 KiB
Python
101 lines
3.6 KiB
Python
|
|
import os
|
||
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
|
||
|
|
from renamer import CliAction, _is_media_file, process_path
|
||
|
|
|
||
|
|
|
||
|
|
class TestIsMediaFile(unittest.TestCase):
|
||
|
|
def test_extension_is_case_insensitive(self):
|
||
|
|
self.assertTrue(_is_media_file("Show.S04E06.MKV"))
|
||
|
|
self.assertTrue(_is_media_file("Show.S04E06.mkv"))
|
||
|
|
self.assertTrue(_is_media_file("Show.S04E06.AVI"))
|
||
|
|
|
||
|
|
def test_unsupported_extension(self):
|
||
|
|
self.assertFalse(_is_media_file("Show.S04E06.txt"))
|
||
|
|
|
||
|
|
|
||
|
|
class TestRenameCaseInsensitiveExtension(unittest.TestCase):
|
||
|
|
def test_uppercase_extension_is_processed(self):
|
||
|
|
with tempfile.TemporaryDirectory() as root:
|
||
|
|
original = "Show S04E06.MKV"
|
||
|
|
open(os.path.join(root, original), "w").close()
|
||
|
|
|
||
|
|
process_path(CliAction.rename, os.path.join(root, original))
|
||
|
|
|
||
|
|
self.assertEqual(os.listdir(root), ["Show.S04E06.MKV"])
|
||
|
|
|
||
|
|
|
||
|
|
class TestSeasonNumbering(unittest.TestCase):
|
||
|
|
def test_scoped_to_directory_not_whole_tree(self):
|
||
|
|
with tempfile.TemporaryDirectory() as root:
|
||
|
|
season_dir = os.path.join(root, "Season 1")
|
||
|
|
extras_dir = os.path.join(root, "Extras")
|
||
|
|
os.mkdir(season_dir)
|
||
|
|
os.mkdir(extras_dir)
|
||
|
|
for name in (
|
||
|
|
"Show.(01.serija.iz.10).mkv",
|
||
|
|
"Show.(02.serija.iz.10).mkv",
|
||
|
|
):
|
||
|
|
open(os.path.join(season_dir, name), "w").close()
|
||
|
|
open(os.path.join(extras_dir, "Blooper.reel.mkv"), "w").close()
|
||
|
|
|
||
|
|
process_path(
|
||
|
|
CliAction.rename, root, overrides={"name": "Show"}, season=1,
|
||
|
|
)
|
||
|
|
|
||
|
|
self.assertEqual(
|
||
|
|
sorted(os.listdir(season_dir)),
|
||
|
|
["Show.S01E01.mkv", "Show.S01E02.mkv"],
|
||
|
|
)
|
||
|
|
# Extras is a separate directory: it gets its own numbering,
|
||
|
|
# starting fresh at E01, instead of continuing the season's count.
|
||
|
|
self.assertEqual(os.listdir(extras_dir), ["Show.S01E01.mkv"])
|
||
|
|
|
||
|
|
def test_single_file_target_defaults_to_episode_one(self):
|
||
|
|
with tempfile.TemporaryDirectory() as root:
|
||
|
|
original = "Show.(01.serija.iz.10).mkv"
|
||
|
|
open(os.path.join(root, original), "w").close()
|
||
|
|
|
||
|
|
process_path(
|
||
|
|
CliAction.rename, os.path.join(root, original),
|
||
|
|
overrides={"name": "Show"}, season=1,
|
||
|
|
)
|
||
|
|
|
||
|
|
self.assertEqual(os.listdir(root), ["Show.S01E01.mkv"])
|
||
|
|
|
||
|
|
|
||
|
|
class TestSetOverride(unittest.TestCase):
|
||
|
|
def test_name_override_drops_unrecognized_leftovers(self):
|
||
|
|
with tempfile.TemporaryDirectory() as root:
|
||
|
|
# nothing else in the title is recognized (no year/quality/etc),
|
||
|
|
# so it would otherwise land entirely under "unknown"
|
||
|
|
original = "Show.(01.serija.iz.10).mkv"
|
||
|
|
open(os.path.join(root, original), "w").close()
|
||
|
|
|
||
|
|
process_path(
|
||
|
|
CliAction.rename, os.path.join(root, original),
|
||
|
|
overrides={"name": "Show"}, season=1,
|
||
|
|
)
|
||
|
|
|
||
|
|
self.assertEqual(os.listdir(root), ["Show.S01E01.mkv"])
|
||
|
|
|
||
|
|
def test_name_override_preserves_unknown_when_name_was_recognized(self):
|
||
|
|
with tempfile.TemporaryDirectory() as root:
|
||
|
|
original = "The.Walking.Dead.S04E06.1080p.WEB-DL.Rus.Eng.HDCLUB.mkv"
|
||
|
|
open(os.path.join(root, original), "w").close()
|
||
|
|
|
||
|
|
process_path(
|
||
|
|
CliAction.rename, os.path.join(root, original),
|
||
|
|
overrides={"name": "TWD"},
|
||
|
|
)
|
||
|
|
|
||
|
|
self.assertEqual(
|
||
|
|
os.listdir(root),
|
||
|
|
["TWD.S04E06.1080p.WEB-DL.Rus.Eng.HDCLUB.mkv"],
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|