|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Regression tests for optional pipeline service degradation logs.""" |
| 3 | + |
| 4 | +import logging |
| 5 | +from types import SimpleNamespace |
| 6 | +from unittest.mock import MagicMock, patch |
| 7 | + |
| 8 | +from src.core.pipeline import StockAnalysisPipeline |
| 9 | + |
| 10 | + |
| 11 | +def _make_config() -> SimpleNamespace: |
| 12 | + return SimpleNamespace( |
| 13 | + max_workers=2, |
| 14 | + save_context_snapshot=False, |
| 15 | + bocha_api_keys=[], |
| 16 | + tavily_api_keys=[], |
| 17 | + brave_api_keys=[], |
| 18 | + serpapi_keys=[], |
| 19 | + minimax_api_keys=[], |
| 20 | + searxng_base_urls=[], |
| 21 | + searxng_public_instances_enabled=False, |
| 22 | + news_max_age_days=7, |
| 23 | + news_strategy_profile="short", |
| 24 | + enable_realtime_quote=False, |
| 25 | + realtime_source_priority=[], |
| 26 | + enable_chip_distribution=False, |
| 27 | + social_sentiment_api_key="", |
| 28 | + social_sentiment_api_url="https://example.invalid/social", |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def _build_pipeline(config: SimpleNamespace) -> StockAnalysisPipeline: |
| 33 | + with patch("src.core.pipeline.get_db", return_value=MagicMock()), \ |
| 34 | + patch("src.core.pipeline.DataFetcherManager", return_value=MagicMock()), \ |
| 35 | + patch("src.core.pipeline.StockTrendAnalyzer", return_value=MagicMock()), \ |
| 36 | + patch("src.core.pipeline.GeminiAnalyzer", return_value=MagicMock()), \ |
| 37 | + patch("src.core.pipeline.NotificationService", return_value=MagicMock()): |
| 38 | + return StockAnalysisPipeline(config=config) |
| 39 | + |
| 40 | + |
| 41 | +def test_search_service_init_failure_logs_traceback_and_failure_state(caplog): |
| 42 | + config = _make_config() |
| 43 | + social_service = MagicMock() |
| 44 | + social_service.is_available = False |
| 45 | + |
| 46 | + with patch("src.core.pipeline.SearchService", side_effect=RuntimeError("search init boom")), \ |
| 47 | + patch("src.core.pipeline.SocialSentimentService", return_value=social_service), \ |
| 48 | + caplog.at_level(logging.WARNING, logger="src.core.pipeline"): |
| 49 | + pipeline = _build_pipeline(config) |
| 50 | + |
| 51 | + assert pipeline.search_service is None |
| 52 | + |
| 53 | + init_failure_records = [ |
| 54 | + record for record in caplog.records if "搜索服务初始化失败,将以无搜索模式运行" in record.message |
| 55 | + ] |
| 56 | + assert len(init_failure_records) == 1 |
| 57 | + assert init_failure_records[0].exc_info is not None |
| 58 | + assert "搜索服务未启用(初始化失败或依赖缺失)" in caplog.text |
| 59 | + assert "搜索服务未启用(未配置搜索能力)" not in caplog.text |
| 60 | + |
| 61 | + |
| 62 | +def test_social_sentiment_init_failure_logs_traceback(caplog): |
| 63 | + config = _make_config() |
| 64 | + search_service = MagicMock() |
| 65 | + search_service.is_available = False |
| 66 | + |
| 67 | + with patch("src.core.pipeline.SearchService", return_value=search_service), \ |
| 68 | + patch("src.core.pipeline.SocialSentimentService", side_effect=RuntimeError("social init boom")), \ |
| 69 | + caplog.at_level(logging.WARNING, logger="src.core.pipeline"): |
| 70 | + pipeline = _build_pipeline(config) |
| 71 | + |
| 72 | + assert pipeline.social_sentiment_service is None |
| 73 | + |
| 74 | + init_failure_records = [ |
| 75 | + record for record in caplog.records if "社交舆情服务初始化失败,将跳过舆情分析" in record.message |
| 76 | + ] |
| 77 | + assert len(init_failure_records) == 1 |
| 78 | + assert init_failure_records[0].exc_info is not None |
| 79 | + |
| 80 | + |
| 81 | +def test_emit_progress_logs_context_when_callback_fails(caplog): |
| 82 | + pipeline = StockAnalysisPipeline.__new__(StockAnalysisPipeline) |
| 83 | + pipeline.query_id = "query-123" |
| 84 | + |
| 85 | + def _fail_callback(progress, message): |
| 86 | + raise RuntimeError(f"cannot send {progress}:{message}") |
| 87 | + |
| 88 | + pipeline.progress_callback = _fail_callback |
| 89 | + |
| 90 | + with caplog.at_level(logging.WARNING, logger="src.core.pipeline"): |
| 91 | + pipeline._emit_progress(55, "fetching news") |
| 92 | + |
| 93 | + records = [record for record in caplog.records if "progress callback failed" in record.message] |
| 94 | + assert len(records) == 1 |
| 95 | + record = records[0] |
| 96 | + assert "progress=55" in record.message |
| 97 | + assert "message='fetching news'" in record.message |
| 98 | + assert "query_id=query-123" in record.message |
| 99 | + assert record.progress == 55 |
| 100 | + assert record.progress_message == "fetching news" |
| 101 | + assert record.query_id == "query-123" |
0 commit comments