7455d7e426
130 issues auto-fixed (import ordering, UP045/UP006 type annotations), 33 issues manually fixed (E712/E501/E402/E722 + N818 rename + per-file wechat ignore for N8xx naming conventions). All 33 tests pass.
65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from app.crawler.base import PipelineConfig
|
|
from app.crawler.dahuagov_spider import DahuagovSpider
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_dahuagov_spider_attributes():
|
|
spider = DahuagovSpider()
|
|
assert spider.name == "dahuagov"
|
|
assert spider.source_code == "dahuagov"
|
|
assert spider.source_name == "大化县政府网采购公告"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_dahuagov_spider_pipeline_config():
|
|
spider = DahuagovSpider()
|
|
config = spider.get_pipeline_config()
|
|
assert isinstance(config, PipelineConfig)
|
|
assert config.filter_enabled is False
|
|
assert config.notify_mode == "all"
|
|
assert config.mark_sent is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_dahuagov_spider_crawl():
|
|
spider = DahuagovSpider()
|
|
|
|
html = """
|
|
<html><body>
|
|
<ul class="more-list">
|
|
<li><span>2026-05-08</span>
|
|
<a href="./detail/1.html" title="公告1">公告1</a></li>
|
|
<li><span>2026-05-07</span>
|
|
<a href="./detail/2.html" title="公告2">公告2</a></li>
|
|
</ul>
|
|
</body></html>
|
|
"""
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = html
|
|
|
|
with patch("httpx.AsyncClient.get", AsyncMock(return_value=mock_response)):
|
|
result = await spider.crawl()
|
|
assert result.total_count == 2
|
|
assert len(result.announcements) == 2
|
|
assert result.success is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_dahuagov_spider_crawl_empty():
|
|
spider = DahuagovSpider()
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = "<html><body></body></html>"
|
|
|
|
with patch("httpx.AsyncClient.get", AsyncMock(return_value=mock_response)):
|
|
result = await spider.crawl()
|
|
assert result.total_count == 0
|
|
assert result.success is True
|