This repository has been archived on 2026-08-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
kb-skills/tests/test_crawler/test_answer_crawler.py
T
starmen1265 912773fc65 test(08-01): add failing tests for ZhihuCrawler
- Add test_answer_crawler.py with tests for crawl_answer()
- Add test_article_crawler.py with tests for crawl_article()
- Add test_metadata_extraction.py with tests for author/metadata parsing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-17 21:08:13 +08:00

94 lines
3.6 KiB
Python

"""Tests for Zhihu Answer Crawler"""
import pytest
from unittest.mock import MagicMock, patch, PropertyMock
from kb_workflow.crawler.models import AnswerData, CrawlResult
class TestCrawlAnswer:
"""Tests for crawl_answer() method."""
def test_crawl_answer_returns_crawl_result(self, mock_browser, sample_answer_url):
"""Test that crawl_answer returns a CrawlResult object."""
from kb_workflow.crawler.zhihu_crawler import ZhihuCrawler
with patch.object(ZhihuCrawler, '_get_cookie', return_value='test_cookie'):
with patch('kb_workflow.crawler.zhihu_crawler.PlaywrightBrowser') as mock_pb:
mock_pb_instance = MagicMock()
mock_pb.return_value = mock_pb_instance
mock_pb_instance.page = MagicMock()
mock_pb_instance.page.route = MagicMock()
crawler = ZhihuCrawler(headless=True, timeout=10)
result = crawler.crawl_answer(sample_answer_url)
assert isinstance(result, CrawlResult)
def test_crawl_answer_invalid_url_returns_failure(self):
"""Test that invalid URL returns CrawlResult with success=False."""
from kb_workflow.crawler.zhihu_crawler import ZhihuCrawler
crawler = ZhihuCrawler(headless=True, timeout=10)
result = crawler.crawl_answer("https://www.example.com/invalid")
assert result.success is False
assert result.error is not None
assert "Invalid" in result.error
def test_crawl_answer_extracts_question_title(
self, sample_answer_url, sample_answer_api_response
):
"""Test that crawl_answer extracts question title from API response."""
from kb_workflow.crawler.zhihu_crawler import ZhihuCrawler, ZhihuApiInterceptor
# Create interceptor with captured data
interceptor = ZhihuApiInterceptor()
interceptor.captured_data["answer"] = sample_answer_api_response
crawler = ZhihuCrawler(headless=True, timeout=10)
# Test the parse method directly
result_data = crawler._parse_answer_data(
sample_answer_api_response,
answer_id=123456789,
question_id=987654321
)
assert result_data.question_title == "What is the best way to learn programming?"
class TestAnswerDataExtraction:
"""Tests for answer data extraction."""
def test_parse_answer_data_contains_content(self, sample_answer_api_response):
"""Test that parsed answer contains content."""
from kb_workflow.crawler.zhihu_crawler import ZhihuCrawler
crawler = ZhihuCrawler(headless=True, timeout=10)
result = crawler._parse_answer_data(
sample_answer_api_response,
answer_id=123456789,
question_id=987654321
)
assert isinstance(result, AnswerData)
assert result.content == "<p>This is a sample answer content in HTML format.</p>"
assert result.excerpt == "This is a sample answer content..."
def test_parse_answer_data_contains_metadata(self, sample_answer_api_response):
"""Test that parsed answer contains metadata."""
from kb_workflow.crawler.zhihu_crawler import ZhihuCrawler
crawler = ZhihuCrawler(headless=True, timeout=10)
result = crawler._parse_answer_data(
sample_answer_api_response,
answer_id=123456789,
question_id=987654321
)
assert result.metadata.voteup_count == 1000
assert result.metadata.comment_count == 50
assert result.metadata.author is not None
assert result.metadata.author.name == "John Doe"