mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-06-05 23:49:54 +08:00
fix: update shared recognize cache flow
This commit is contained in:
@@ -78,6 +78,9 @@ class TestMediaRecognizeShare(unittest.TestCase):
|
||||
), patch(
|
||||
"app.chain.MediaRecognizeShareHelper.report",
|
||||
return_value=False,
|
||||
), patch.object(
|
||||
self.chain,
|
||||
"_update_local_recognize_cache",
|
||||
):
|
||||
result = self.chain.recognize_media(meta=meta, cache=False)
|
||||
|
||||
@@ -117,17 +120,69 @@ class TestMediaRecognizeShare(unittest.TestCase):
|
||||
), patch(
|
||||
"app.chain.MediaRecognizeShareHelper.async_report",
|
||||
AsyncMock(return_value=False),
|
||||
):
|
||||
), patch.object(
|
||||
self.chain,
|
||||
"_async_update_local_recognize_cache",
|
||||
AsyncMock(),
|
||||
) as backfill_mock:
|
||||
result = await self.chain.async_recognize_media(meta=meta, cache=False)
|
||||
return result, query_mock
|
||||
return result, query_mock, backfill_mock
|
||||
|
||||
result, query_mock = asyncio.run(runner())
|
||||
result, query_mock, backfill_mock = asyncio.run(runner())
|
||||
|
||||
self.assertIs(result, shared_media)
|
||||
self.assertEqual(async_run_module.await_count, 2)
|
||||
query_mock.assert_awaited_once_with(meta=meta, mtype=None)
|
||||
backfill_mock.assert_awaited_once()
|
||||
self.assertIsNone(meta.begin_season)
|
||||
|
||||
def test_backfill_local_cache_after_shared_recognize_success(self):
|
||||
"""
|
||||
共享识别后二次本地识别成功时,应回填原始名称对应的本地识别缓存。
|
||||
"""
|
||||
meta = self._build_meta("测试缓存回填", MediaType.MOVIE)
|
||||
shared_media = MediaInfo(
|
||||
title="测试缓存回填",
|
||||
year="2024",
|
||||
tmdb_id=700,
|
||||
type=MediaType.MOVIE,
|
||||
source="themoviedb",
|
||||
tmdb_info={"id": 700, "media_type": MediaType.MOVIE, "title": "测试缓存回填"},
|
||||
)
|
||||
|
||||
with patch.object(
|
||||
self.chain,
|
||||
"run_module",
|
||||
side_effect=[None, shared_media],
|
||||
), patch(
|
||||
"app.chain.MediaRecognizeShareHelper.query",
|
||||
return_value={"type": "movie", "tmdbid": 700},
|
||||
), patch(
|
||||
"app.chain.MediaRecognizeShareHelper.to_recognize_params",
|
||||
return_value={
|
||||
"mtype": MediaType.MOVIE,
|
||||
"tmdbid": 700,
|
||||
"doubanid": None,
|
||||
"bangumiid": None,
|
||||
"season": None,
|
||||
},
|
||||
), patch(
|
||||
"app.chain.MediaRecognizeShareHelper.report",
|
||||
return_value=False,
|
||||
), patch.object(
|
||||
self.chain,
|
||||
"_update_local_recognize_cache",
|
||||
) as backfill_mock:
|
||||
result = self.chain.recognize_media(meta=meta, cache=False)
|
||||
|
||||
self.assertIs(result, shared_media)
|
||||
backfill_mock.assert_called_once()
|
||||
backfill_meta, backfill_media = backfill_mock.call_args.args
|
||||
self.assertIsNot(backfill_meta, meta)
|
||||
self.assertEqual(backfill_meta.name, meta.name)
|
||||
self.assertEqual(backfill_meta.type, meta.type)
|
||||
self.assertIs(backfill_media, shared_media)
|
||||
|
||||
def test_query_and_report_prefer_original_name_keyword(self):
|
||||
"""
|
||||
查询和上报共享识别时应优先使用未应用识别词的识别名称
|
||||
@@ -151,6 +206,57 @@ class TestMediaRecognizeShare(unittest.TestCase):
|
||||
self.assertEqual(query_params["keyword"], "未应用识别词的名称")
|
||||
self.assertEqual(report_payload["keyword"], "未应用识别词的名称")
|
||||
|
||||
def test_skip_report_when_local_recognize_hits_cache(self):
|
||||
"""
|
||||
本地识别命中缓存时不应上报共享识别
|
||||
"""
|
||||
meta = self._build_meta("缓存电影", MediaType.MOVIE)
|
||||
mediainfo = MediaInfo(title="缓存电影", year="2024", tmdb_id=500, type=MediaType.MOVIE)
|
||||
mediainfo.recognize_cache_hit = True
|
||||
|
||||
with patch.object(self.chain, "run_module", return_value=mediainfo) as run_module, patch(
|
||||
"app.chain.MediaRecognizeShareHelper.report",
|
||||
return_value=True,
|
||||
) as report_mock, patch(
|
||||
"app.chain.MediaRecognizeShareHelper.query"
|
||||
) as query_mock:
|
||||
result = self.chain.recognize_media(meta=meta)
|
||||
|
||||
self.assertIs(result, mediainfo)
|
||||
run_module.assert_called_once()
|
||||
report_mock.assert_not_called()
|
||||
query_mock.assert_not_called()
|
||||
|
||||
def test_async_skip_report_when_local_recognize_hits_cache(self):
|
||||
"""
|
||||
异步本地识别命中缓存时不应上报共享识别
|
||||
"""
|
||||
meta = self._build_meta("缓存剧集", MediaType.TV)
|
||||
mediainfo = MediaInfo(title="缓存剧集", year="2025", tmdb_id=600, type=MediaType.TV)
|
||||
mediainfo.recognize_cache_hit = True
|
||||
|
||||
async def runner():
|
||||
with patch.object(
|
||||
self.chain,
|
||||
"async_run_module",
|
||||
AsyncMock(return_value=mediainfo),
|
||||
) as async_run_module, patch(
|
||||
"app.chain.MediaRecognizeShareHelper.async_report",
|
||||
AsyncMock(return_value=True),
|
||||
) as report_mock, patch(
|
||||
"app.chain.MediaRecognizeShareHelper.async_query",
|
||||
AsyncMock(),
|
||||
) as query_mock:
|
||||
result = await self.chain.async_recognize_media(meta=meta)
|
||||
return result, async_run_module, report_mock, query_mock
|
||||
|
||||
result, async_run_module, report_mock, query_mock = asyncio.run(runner())
|
||||
|
||||
self.assertIs(result, mediainfo)
|
||||
async_run_module.assert_awaited_once()
|
||||
report_mock.assert_not_awaited()
|
||||
query_mock.assert_not_awaited()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user