feat: add czzy category and search flow
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import unittest
|
||||
from importlib.machinery import SourceFileLoader
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
@@ -13,6 +14,10 @@ class TestCZZYSpider(unittest.TestCase):
|
||||
self.spider = Spider()
|
||||
self.spider.init()
|
||||
|
||||
def test_default_host_prefers_czzy89(self):
|
||||
self.assertEqual(self.spider.hosts[0], "https://www.czzy89.com")
|
||||
self.assertEqual(self.spider.current_host, "https://www.czzy89.com")
|
||||
|
||||
def test_home_content_exposes_expected_categories(self):
|
||||
content = self.spider.homeContent(False)
|
||||
class_ids = [item["type_id"] for item in content["class"]]
|
||||
@@ -43,6 +48,61 @@ class TestCZZYSpider(unittest.TestCase):
|
||||
],
|
||||
)
|
||||
|
||||
@patch.object(Spider, "fetch")
|
||||
def test_request_html_falls_back_to_second_host(self, mock_fetch):
|
||||
class FakeResponse:
|
||||
def __init__(self, text, status_code=200):
|
||||
self.text = text
|
||||
self.status_code = status_code
|
||||
self.encoding = "utf-8"
|
||||
|
||||
mock_fetch.side_effect = [
|
||||
Exception("first host down"),
|
||||
FakeResponse("<html><body>ok</body></html>"),
|
||||
]
|
||||
|
||||
html, host = self.spider._request_html("/movie_bt/page/1", expect_xpath="//body")
|
||||
self.assertIn("ok", html)
|
||||
self.assertEqual(host, "https://www.cz01.org")
|
||||
self.assertEqual(self.spider.current_host, "https://www.cz01.org")
|
||||
|
||||
@patch.object(Spider, "_request_html")
|
||||
def test_category_content_builds_media_list(self, mock_request_html):
|
||||
mock_request_html.return_value = (
|
||||
"""
|
||||
<ul>
|
||||
<li>
|
||||
<a href="/movie/abc.html"><img src="/cover.jpg" alt="分类影片" /></a>
|
||||
<span class="hdinfo">HD</span>
|
||||
</li>
|
||||
</ul>
|
||||
""",
|
||||
"https://www.czzy89.com",
|
||||
)
|
||||
|
||||
result = self.spider.categoryContent("movie", "2", False, {})
|
||||
self.assertEqual(result["page"], 2)
|
||||
self.assertEqual(result["list"][0]["vod_name"], "分类影片")
|
||||
self.assertEqual(result["list"][0]["vod_pic"], "https://www.czzy89.com/cover.jpg")
|
||||
|
||||
@patch.object(Spider, "_request_html")
|
||||
def test_search_content_reuses_media_card_parser(self, mock_request_html):
|
||||
mock_request_html.return_value = (
|
||||
"""
|
||||
<div>
|
||||
<li>
|
||||
<a href="/movie/search-hit.html" title="搜索影片"></a>
|
||||
<img data-src="https://img.example/search.jpg" />
|
||||
</li>
|
||||
</div>
|
||||
""",
|
||||
"https://www.czzy89.com",
|
||||
)
|
||||
|
||||
result = self.spider.searchContent("繁花", False, "1")
|
||||
self.assertEqual(result["list"][0]["vod_id"], "/movie/search-hit.html")
|
||||
self.assertEqual(result["list"][0]["vod_name"], "搜索影片")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user