Files
SakuraHub/tests/test_online_search_service.py
T

78 lines
3.0 KiB
Python
Raw Normal View History

2026-11-16 20:45:22 +08:00
from __future__ import annotations
import unittest
from unittest.mock import patch
from sakura_cloud.services.online_search import _assert_public_url, search_sources
class OnlineSearchServiceTest(unittest.TestCase):
def test_novel_json_rule_search(self) -> None:
sources = [
{
"bookSourceName": "Novel JSON",
"bookSourceUrl": "https://novel.example.com",
"bookSourceType": 0,
"searchUrl": "/search?q={{key}}",
"ruleSearch": {
"bookList": "$.data[*]",
"name": "$.name",
"bookUrl": "$.url",
"author": "$.author",
},
}
]
body = '{"data":[{"name":"十宗罪","url":"/book/1","author":"蜘蛛"}]}'
with patch("sakura_cloud.services.online_search._assert_public_url"), patch(
"sakura_cloud.services.online_search._fetch_text",
return_value=body,
):
rows = search_sources(sources, "十宗罪", mode="novel")
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0].title, "十宗罪")
self.assertEqual(rows[0].detail_url, "https://novel.example.com/book/1")
def test_manga_json_fallback_search(self) -> None:
sources = [
{
"bookSourceName": "Manga JSON",
"bookSourceUrl": "https://manga.example.com",
"bookSourceType": 2,
"searchUrl": "/search?q={{key}}",
"ruleSearch": {"bookList": "$.missing[*]"},
}
]
body = '{"data":[{"comic_name":"樱花漫画","comic_url":"/comic/1","cover_url":"/cover.jpg"}]}'
with patch("sakura_cloud.services.online_search._assert_public_url"), patch(
"sakura_cloud.services.online_search._fetch_text",
return_value=body,
):
rows = search_sources(sources, "樱花", mode="manga")
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0].cover_url, "https://manga.example.com/cover.jpg")
def test_manga_html_card_fallback_search(self) -> None:
sources = [
{
"bookSourceName": "Manga HTML",
"bookSourceUrl": "https://manga.example.com",
"bookSourceType": 2,
"searchUrl": "/search?q={{key}}",
}
]
body = '<a href="/manga/1" title="魔法漫画"><img data-src="/cover.webp"></a>'
with patch("sakura_cloud.services.online_search._assert_public_url"), patch(
"sakura_cloud.services.online_search._fetch_text",
return_value=body,
):
rows = search_sources(sources, "魔法", mode="manga")
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0].detail_url, "https://manga.example.com/manga/1")
def test_private_url_is_rejected(self) -> None:
with self.assertRaises(Exception):
_assert_public_url("http://127.0.0.1:8000/search")
if __name__ == "__main__":
unittest.main()