test: update downloader path mapping expectations

This commit is contained in:
jxxghp
2026-06-15 14:04:58 +08:00
parent 6a635ac720
commit 47f6389424
+78 -73
View File
@@ -1,6 +1,5 @@
import sys import sys
import types import types
import unittest
from enum import Enum from enum import Enum
from pathlib import Path from pathlib import Path
from types import SimpleNamespace from types import SimpleNamespace
@@ -263,64 +262,15 @@ DownloaderBase = _load_downloader_base()
TransmissionModule, TransmissionTorrentStatus = _load_transmission_module() TransmissionModule, TransmissionTorrentStatus = _load_transmission_module()
class DownloaderPathMappingTest(unittest.TestCase): def _build_base(path_mapping):
def _build_base(self, path_mapping):
downloader = DownloaderBase.__new__(DownloaderBase) downloader = DownloaderBase.__new__(DownloaderBase)
downloader.get_config = MagicMock( downloader.get_config = MagicMock(
return_value=SimpleNamespace(path_mapping=path_mapping) return_value=SimpleNamespace(path_mapping=path_mapping)
) )
return downloader return downloader
def test_normalize_path_maps_moviepilot_path_to_downloader_path(self):
downloader = self._build_base(
[("/media", "/mnt/raid5/home_lt999lt")]
)
result = downloader.normalize_path( def _build_transmission_module(server):
Path("/media/video/downloads/movie"), "tr"
)
self.assertEqual(result, "/mnt/raid5/home_lt999lt/video/downloads/movie")
def test_normalize_return_path_maps_downloader_path_back_to_moviepilot_path(self):
downloader = self._build_base(
[("/media", "/mnt/raid5/home_lt999lt")]
)
result = downloader.normalize_return_path(
Path("/mnt/raid5/home_lt999lt/video/downloads/TV/Show.mkv"), "tr"
)
self.assertEqual(result, "/media/video/downloads/TV/Show.mkv")
def test_path_mapping_matches_complete_path_segment_only(self):
downloader = self._build_base([("/media", "/mnt/media")])
result = downloader.normalize_return_path(
Path("/mnt/media2/Show.mkv"), "tr"
)
self.assertEqual(result, "/mnt/media2/Show.mkv")
def test_blank_path_mapping_entry_is_ignored(self):
downloader = self._build_base(
[("", "/downloads"), ("/media2", ""), ("/media", "/mnt/media")]
)
result = downloader.normalize_return_path(Path("/mnt/media/Show.mkv"), "tr")
self.assertEqual(result, "/media/Show.mkv")
def test_normalize_path_strips_storage_prefix_after_mapping(self):
downloader = self._build_base([("local:/media", "/downloads")])
result = downloader.normalize_path(Path("local:/media/movie"), "qb")
self.assertEqual(result, "/downloads/movie")
class TransmissionPathMappingTest(unittest.TestCase):
def _build_module(self, server):
module = TransmissionModule.__new__(TransmissionModule) module = TransmissionModule.__new__(TransmissionModule)
module.get_instances = MagicMock(return_value={"tr": server}) module.get_instances = MagicMock(return_value={"tr": server})
module.get_instance = MagicMock(return_value=server) module.get_instance = MagicMock(return_value=server)
@@ -331,7 +281,58 @@ class TransmissionPathMappingTest(unittest.TestCase):
) )
return module return module
def test_completed_torrents_return_moviepilot_accessible_path(self):
def test_normalize_path_maps_moviepilot_path_to_downloader_path():
"""MoviePilot 访问路径应转换为下载器容器内路径。"""
downloader = _build_base([("/media", "/mnt/raid5/home_lt999lt")])
result = downloader.normalize_path(Path("/media/video/downloads/movie"), "tr")
assert result == "/mnt/raid5/home_lt999lt/video/downloads/movie"
def test_normalize_return_path_maps_downloader_path_back_to_moviepilot_path():
"""下载器容器内路径应转换回 MoviePilot 可访问路径。"""
downloader = _build_base([("/media", "/mnt/raid5/home_lt999lt")])
result = downloader.normalize_return_path(
Path("/mnt/raid5/home_lt999lt/video/downloads/TV/Show.mkv"), "tr"
)
assert result == "/media/video/downloads/TV/Show.mkv"
def test_path_mapping_matches_complete_path_segment_only():
"""路径映射只应命中完整路径段,避免误伤相似前缀。"""
downloader = _build_base([("/media", "/mnt/media")])
result = downloader.normalize_return_path(Path("/mnt/media2/Show.mkv"), "tr")
assert result == "/mnt/media2/Show.mkv"
def test_blank_path_mapping_entry_is_ignored():
"""空路径映射项应被忽略,继续使用后续有效配置。"""
downloader = _build_base(
[("", "/downloads"), ("/media2", ""), ("/media", "/mnt/media")]
)
result = downloader.normalize_return_path(Path("/mnt/media/Show.mkv"), "tr")
assert result == "/media/Show.mkv"
def test_normalize_path_strips_storage_prefix_after_mapping():
"""带存储类型前缀的路径映射后应返回下载器原生路径。"""
downloader = _build_base([("local:/media", "/downloads")])
result = downloader.normalize_path(Path("local:/media/movie"), "qb")
assert result == "/downloads/movie"
def test_completed_torrents_return_moviepilot_accessible_path():
"""Transmission 已完成任务返回的路径字段均应为 MoviePilot 可访问路径。"""
server = MagicMock() server = MagicMock()
server.get_completed_torrents.return_value = [ server.get_completed_torrents.return_value = [
SimpleNamespace( SimpleNamespace(
@@ -343,17 +344,17 @@ class TransmissionPathMappingTest(unittest.TestCase):
status="seeding", status="seeding",
) )
] ]
module = self._build_module(server) module = _build_transmission_module(server)
torrents = module.list_torrents(status=TransmissionTorrentStatus.TRANSFER) torrents = module.list_torrents(status=TransmissionTorrentStatus.TRANSFER)
self.assertEqual(torrents[0].path, Path("/media/video/downloads/TV/Show.S01E01.mkv")) assert torrents[0].path == Path("/media/video/downloads/TV/Show.S01E01.mkv")
module.normalize_return_path.assert_called_once_with( assert torrents[0].save_path == "/media/video/downloads/TV"
Path("/mnt/raid5/home_lt999lt/video/downloads/TV/Show.S01E01.mkv"), assert torrents[0].content_path == "/media/video/downloads/TV/Show.S01E01.mkv"
"tr",
)
def test_hash_lookup_return_moviepilot_accessible_path(self):
def test_hash_lookup_return_moviepilot_accessible_path():
"""Transmission 按 Hash 查询时返回的路径字段均应完成路径映射。"""
server = MagicMock() server = MagicMock()
server.get_torrents.return_value = ( server.get_torrents.return_value = (
[ [
@@ -369,17 +370,17 @@ class TransmissionPathMappingTest(unittest.TestCase):
], ],
False, False,
) )
module = self._build_module(server) module = _build_transmission_module(server)
torrents = module.list_torrents(hashs=["hash-tr"], downloader="tr") torrents = module.list_torrents(hashs=["hash-tr"], downloader="tr")
self.assertEqual(torrents[0].path, Path("/media/video/downloads/movie/Movie")) assert torrents[0].path == Path("/media/video/downloads/movie/Movie")
module.normalize_return_path.assert_called_once_with( assert torrents[0].save_path == "/media/video/downloads/movie"
Path("/mnt/raid5/home_lt999lt/video/downloads/movie/Movie"), assert torrents[0].content_path == "/media/video/downloads/movie/Movie"
"tr",
)
def test_all_torrents_include_completed_and_downloading_states(self):
def test_all_torrents_include_completed_and_downloading_states():
"""Transmission 默认列表应同时包含已完成和下载中的任务状态。"""
server = MagicMock() server = MagicMock()
server.get_torrents.return_value = ( server.get_torrents.return_value = (
[ [
@@ -407,15 +408,19 @@ class TransmissionPathMappingTest(unittest.TestCase):
], ],
False, False,
) )
module = self._build_module(server) module = _build_transmission_module(server)
torrents = module.list_torrents() torrents = module.list_torrents()
self.assertEqual(["completed", "downloading"], [torrent.state for torrent in torrents]) assert ["completed", "downloading"] == [torrent.state for torrent in torrents]
self.assertEqual(["hash-completed", "hash-downloading"], [torrent.hash for torrent in torrents]) assert ["hash-completed", "hash-downloading"] == [
torrent.hash for torrent in torrents
]
server.get_torrents.assert_called_once_with(tags="moviepilot-tag") server.get_torrents.assert_called_once_with(tags="moviepilot-tag")
def test_include_all_tags_removes_builtin_tag_filter(self):
def test_include_all_tags_removes_builtin_tag_filter():
"""查询全部标签任务时不应附加 MoviePilot 内置标签过滤。"""
server = MagicMock() server = MagicMock()
server.get_torrents.return_value = ( server.get_torrents.return_value = (
[ [
@@ -431,9 +436,9 @@ class TransmissionPathMappingTest(unittest.TestCase):
], ],
False, False,
) )
module = self._build_module(server) module = _build_transmission_module(server)
torrents = module.list_torrents(include_all_tags=True) torrents = module.list_torrents(include_all_tags=True)
self.assertEqual(["hash-external"], [torrent.hash for torrent in torrents]) assert ["hash-external"] == [torrent.hash for torrent in torrents]
server.get_torrents.assert_called_once_with(tags=None) server.get_torrents.assert_called_once_with(tags=None)