912773fc65
- 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>
67 lines
2.4 KiB
Python
67 lines
2.4 KiB
Python
"""Tests for Zhihu Article Crawler"""
|
|
|
|
import pytest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from kb_workflow.crawler.models import ArticleData, CrawlResult
|
|
|
|
|
|
class TestCrawlArticle:
|
|
"""Tests for crawl_article() method."""
|
|
|
|
def test_crawl_article_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_article("https://www.example.com/invalid")
|
|
|
|
assert result.success is False
|
|
assert result.error is not None
|
|
assert "Invalid" in result.error
|
|
|
|
def test_crawl_article_extracts_title(
|
|
self, sample_article_api_response
|
|
):
|
|
"""Test that crawl_article extracts title from API response."""
|
|
from kb_workflow.crawler.zhihu_crawler import ZhihuCrawler
|
|
|
|
crawler = ZhihuCrawler(headless=True, timeout=10)
|
|
result = crawler._parse_article_data(
|
|
sample_article_api_response,
|
|
article_id=11223344
|
|
)
|
|
|
|
assert isinstance(result, ArticleData)
|
|
assert result.title == "How to Build a Web Scraper"
|
|
|
|
def test_crawl_article_extracts_content(
|
|
self, sample_article_api_response
|
|
):
|
|
"""Test that crawl_article extracts content from API response."""
|
|
from kb_workflow.crawler.zhihu_crawler import ZhihuCrawler
|
|
|
|
crawler = ZhihuCrawler(headless=True, timeout=10)
|
|
result = crawler._parse_article_data(
|
|
sample_article_api_response,
|
|
article_id=11223344
|
|
)
|
|
|
|
assert result.content == "<p>This article explains how to build a web scraper...</p>"
|
|
assert result.excerpt == "This article explains how to build a web scraper..."
|
|
|
|
def test_crawl_article_contains_metadata(self, sample_article_api_response):
|
|
"""Test that parsed article contains metadata."""
|
|
from kb_workflow.crawler.zhihu_crawler import ZhihuCrawler
|
|
|
|
crawler = ZhihuCrawler(headless=True, timeout=10)
|
|
result = crawler._parse_article_data(
|
|
sample_article_api_response,
|
|
article_id=11223344
|
|
)
|
|
|
|
assert result.metadata.voteup_count == 500
|
|
assert result.metadata.comment_count == 25
|
|
assert result.metadata.author is not None
|
|
assert result.metadata.author.name == "Jane Smith"
|