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_metadata_extraction.py
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

110 lines
3.5 KiB
Python

"""Tests for metadata extraction from Zhihu API responses"""
import pytest
from kb_workflow.crawler.models import AuthorInfo, ContentMetadata
class TestAuthorExtraction:
"""Tests for author information extraction."""
def test_parse_author_with_full_data(self):
"""Test parsing author with all fields present."""
from kb_workflow.crawler.zhihu_crawler import ZhihuCrawler
crawler = ZhihuCrawler(headless=True, timeout=10)
author_data = {
"name": "Test User",
"url_token": "test-user-123",
"headline": "Test headline"
}
result = crawler._parse_author(author_data)
assert isinstance(result, AuthorInfo)
assert result.name == "Test User"
assert result.url_token == "test-user-123"
assert result.headline == "Test headline"
def test_parse_author_with_minimal_data(self):
"""Test parsing author with only required fields."""
from kb_workflow.crawler.zhihu_crawler import ZhihuCrawler
crawler = ZhihuCrawler(headless=True, timeout=10)
author_data = {"name": "Minimal User"}
result = crawler._parse_author(author_data)
assert result.name == "Minimal User"
assert result.url_token is None
assert result.headline is None
def test_parse_author_with_empty_data(self):
"""Test parsing author with empty/None data."""
from kb_workflow.crawler.zhihu_crawler import ZhihuCrawler
crawler = ZhihuCrawler(headless=True, timeout=10)
result = crawler._parse_author(None)
assert result is None
result = crawler._parse_author({})
assert result.name == ""
class TestMetadataExtraction:
"""Tests for content metadata extraction."""
def test_parse_metadata_with_full_data(self):
"""Test parsing metadata with all fields present."""
from kb_workflow.crawler.zhihu_crawler import ZhihuCrawler
crawler = ZhihuCrawler(headless=True, timeout=10)
data = {
"author": {"name": "Test Author"},
"voteup_count": 100,
"comment_count": 20,
"created_time": 1700000000,
"updated_time": 1700100000
}
result = crawler._parse_metadata(data)
assert isinstance(result, ContentMetadata)
assert result.voteup_count == 100
assert result.comment_count == 20
assert result.created_time == 1700000000
assert result.updated_time == 1700100000
assert result.author is not None
assert result.author.name == "Test Author"
def test_parse_metadata_with_defaults(self):
"""Test parsing metadata with missing optional fields."""
from kb_workflow.crawler.zhihu_crawler import ZhihuCrawler
crawler = ZhihuCrawler(headless=True, timeout=10)
data = {}
result = crawler._parse_metadata(data)
assert result.voteup_count == 0
assert result.comment_count == 0
assert result.created_time is None
assert result.updated_time is None
assert result.author is None
def test_parse_metadata_with_zero_values(self):
"""Test parsing metadata when counts are zero."""
from kb_workflow.crawler.zhihu_crawler import ZhihuCrawler
crawler = ZhihuCrawler(headless=True, timeout=10)
data = {
"voteup_count": 0,
"comment_count": 0
}
result = crawler._parse_metadata(data)
assert result.voteup_count == 0
assert result.comment_count == 0