mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-11-12 00:30:28 +08:00
Run `ruff check app tests scripts --fix` with the configured rule set (E4/E7/E9/F/I): import sorting (I001), f-strings without placeholders (F541), lambda/variable/unused-import cleanups that ruff marks safe. Nine files whose only ruff debt sits next to pre-existing pylint import-resolution errors (lazy exports, sys.path script imports) or whose "unused" imports are reached through attribute access by tests are left untouched so the changed-file pylint gate and the benchmark tests stay green; their diagnostics remain in the baseline. app/startup/lifecycle/__init__.py and app/main.py import business modules after cache composition, import-path and stdio setup on purpose (the source comments document the contract), so declare E402 per-file-ignores for them instead of adding per-line noqa. Ruff ratchet watermark drops from 498 to 100; sync the baseline fixture and the optimization checklist figure. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
72 lines
2.6 KiB
Python
72 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.modules.themoviedb.tmdbv3api.tmdb import TMDb
|
|
from app.runtime.cache import cached
|
|
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()
|