feat: 添加 Dahuagov Spider(大化县政府网爬虫)+ 测试

This commit is contained in:
2026-05-09 13:47:48 +08:00
parent b1bbf64287
commit 5e0582d499
2 changed files with 134 additions and 0 deletions
@@ -0,0 +1,62 @@
import pytest
from unittest.mock import AsyncMock, patch, MagicMock
from app.crawler.dahuagov_spider import DahuagovSpider
from app.crawler.base import PipelineConfig
@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