fix: 防止重复推送通知 — 添加已发送检查 + GXGP 标记已发送
- Pipeline 新增 _exclude_sent() 方法,通知前查询数据库跳过 is_sent=True 的公告 - GXGP Spider mark_sent 改为 True,配合去重逻辑防止每次爬取重复通知
This commit is contained in:
@@ -45,13 +45,16 @@ async def test_pipeline_filtered_mode():
|
||||
pipeline = PostCrawlPipeline(db_session=mock_db, notification_service=mock_notify)
|
||||
|
||||
with patch.object(pipeline, "_save_to_db", AsyncMock(return_value=2)):
|
||||
with patch.object(pipeline, "_send_notifications", AsyncMock(return_value=1)):
|
||||
pipe_result = await pipeline.process(
|
||||
result.announcements, config
|
||||
)
|
||||
assert pipe_result.stored == 2
|
||||
assert pipe_result.filtered == 1
|
||||
assert pipe_result.notified == 1
|
||||
with patch.object(pipeline, "_exclude_sent", AsyncMock(
|
||||
side_effect=lambda anns: [a for a in anns if "大化" in a["title"]]
|
||||
)):
|
||||
with patch.object(pipeline, "_send_notifications", AsyncMock(return_value=1)):
|
||||
pipe_result = await pipeline.process(
|
||||
result.announcements, config
|
||||
)
|
||||
assert pipe_result.stored == 2
|
||||
assert pipe_result.filtered == 1
|
||||
assert pipe_result.notified == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -84,12 +87,15 @@ async def test_pipeline_all_mode():
|
||||
pipeline = PostCrawlPipeline(db_session=mock_db, notification_service=mock_notify)
|
||||
|
||||
with patch.object(pipeline, "_save_to_db", AsyncMock(return_value=2)):
|
||||
with patch.object(pipeline, "_send_notifications", AsyncMock(return_value=2)):
|
||||
with patch.object(pipeline, "_mark_sent", AsyncMock(return_value=2)):
|
||||
pipe_result = await pipeline.process(
|
||||
result.announcements, config
|
||||
)
|
||||
assert pipe_result.stored == 2
|
||||
assert pipe_result.filtered == 0
|
||||
assert pipe_result.notified == 2
|
||||
pipeline._mark_sent.assert_awaited_once()
|
||||
with patch.object(pipeline, "_exclude_sent", AsyncMock(
|
||||
side_effect=lambda anns: anns
|
||||
)):
|
||||
with patch.object(pipeline, "_send_notifications", AsyncMock(return_value=2)):
|
||||
with patch.object(pipeline, "_mark_sent", AsyncMock(return_value=2)):
|
||||
pipe_result = await pipeline.process(
|
||||
result.announcements, config
|
||||
)
|
||||
assert pipe_result.stored == 2
|
||||
assert pipe_result.filtered == 0
|
||||
assert pipe_result.notified == 2
|
||||
pipeline._mark_sent.assert_awaited_once()
|
||||
|
||||
Reference in New Issue
Block a user