mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-08-14 18:24:42 +08:00
* wip(v3): 移植监控与整理韧性修复到 v3 基线 包含:监控看门狗隔离/挂载探测、整理队列持久化、文件系统子进程代理、 写入原子化。迁移重挂到 v3 链 8a4c7e1d2f90 -> 7f5c1d2e3a4b -> e3d9f4b7c806。 tmdb 相关测试尚未通过,待定位。 * fix(v3): 修正移植引入的 16 项测试失败 - poller.py:合并时我方保留的行仍用旧变量名 merged_snapshot,而 v3 已统一 改名为 current_snapshot,导致 NameError 被外层 except 吞掉、快照从未保存 - smb.py:采纳 f-string 拆分写法,恢复 Python 3.11 可解析 - dispatcher 测试:历史查重由 _should_skip_by_history 统一承担,mock 点随之调整 - tmdb 缓存测试:补充 v3 新增的 media_source/media_id 字段 - tmdb 重试测试:为 fake 补充 match_multi/async_match_multi 尚余 3 项与 v3 识别流程的连接失败处理有关,待单独判断。 * fix(v3): 测试适配 v3 的 media_source/media_id 重构 v3 将媒体标识从 tmdbid 统一重构为 media_source + media_id,recognize_media 的 tmdbid 参数已被 **kwargs 静默吞掉——传了也不生效,流程会误降级到名称搜索。 tmdb 重试用例改用新参数后恢复正确路径。 同时修正 fake 的 match_multi 语义:真实实现(tmdbapi.match_multi)吞掉所有 异常并返回 None,连接失败与「未找到」在该路径上本就不可区分,fake 需保持一致。 至此移植引入的 19 项失败全部清零。 --------- Co-authored-by: Aqr-K <Aqr-K@users.noreply.github.com>
73 lines
2.6 KiB
Python
73 lines
2.6 KiB
Python
"""
|
|
TMDB request 层业务失败响应不缓存测试(§6.4)。
|
|
|
|
TMDB 对 404 等业务失败返回合法 JSON(success=false),原实现会随快照缓存
|
|
12 小时;瞬时的服务端错误也会被同样固化,期间同 key 请求直接命中失败快照。
|
|
业务失败响应必须跳过缓存,允许下次请求重新确认。
|
|
"""
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from app.core.cache import cached
|
|
from app.modules.themoviedb.tmdbv3api.tmdb import TMDb
|
|
|
|
from tests.test_tmdb_response_cache import _FakeResponse
|
|
|
|
_NOT_FOUND_PAYLOAD = {
|
|
"success": False,
|
|
"status_code": 34,
|
|
"status_message": "The resource you requested could not be found.",
|
|
}
|
|
_HEADERS = {"Content-Type": "application/json"}
|
|
|
|
|
|
class CachedSkipIfTest(unittest.TestCase):
|
|
|
|
def test_skip_if_prevents_caching_matching_values(self):
|
|
"""skip_if 命中的返回值不入缓存,后续调用重新执行函数。"""
|
|
calls = {"bad": 0, "good": 0}
|
|
|
|
@cached(region="test_skip_if", ttl=60,
|
|
skip_if=lambda value: value.get("bad"))
|
|
def fetch(kind: str) -> dict:
|
|
calls[kind] += 1
|
|
return {"bad": kind == "bad"}
|
|
|
|
fetch("bad")
|
|
fetch("bad")
|
|
self.assertEqual(calls["bad"], 2)
|
|
fetch("good")
|
|
fetch("good")
|
|
self.assertEqual(calls["good"], 1)
|
|
|
|
|
|
class TmdbFailureSnapshotCacheTest(unittest.TestCase):
|
|
|
|
@staticmethod
|
|
def _make_tmdb() -> TMDb:
|
|
tmdb = TMDb()
|
|
tmdb.api_key = "test-key"
|
|
return tmdb
|
|
|
|
def test_business_failure_response_is_not_cached(self):
|
|
"""404 业务失败 JSON 不入缓存,同参数再次请求会重新访问 TMDB。"""
|
|
tmdb = self._make_tmdb()
|
|
fake = _FakeResponse(_NOT_FOUND_PAYLOAD, _HEADERS, status_code=404)
|
|
with patch.object(TMDb, "_request_once", return_value=fake) as req:
|
|
tmdb.request("GET", "https://api.tmdb.test/failure-not-cached", None, None)
|
|
tmdb.request("GET", "https://api.tmdb.test/failure-not-cached", None, None)
|
|
self.assertEqual(req.call_count, 2)
|
|
|
|
def test_success_response_is_still_cached(self):
|
|
"""成功响应保持缓存,同参数第二次请求命中快照。"""
|
|
tmdb = self._make_tmdb()
|
|
fake = _FakeResponse({"id": 98865, "title": "Test"}, _HEADERS)
|
|
with patch.object(TMDb, "_request_once", return_value=fake) as req:
|
|
tmdb.request("GET", "https://api.tmdb.test/success-cached", None, None)
|
|
tmdb.request("GET", "https://api.tmdb.test/success-cached", None, None)
|
|
self.assertEqual(req.call_count, 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|