8000 fix: handle unavailable news cache by dajiaohuang · Pull Request #8 · aahl/mcp-aktools · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion mcp_aktools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,13 @@ def stock_news(
symbol: str = Field(description="股票代码/加密货币符号"),
limit: int = Field(15, description="返回数量(int)", strict=False),
):
news_data = ak_cache(stock_news_em, symbol=symbol, ttl=3600)
if news_data is None:
return f"News unavailable for {symbol}"

news = list(dict.fromkeys([
v["新闻内容"]
for v in ak_cache(stock_news_em, symbol=symbol, ttl=3600).to_dict(orient="records")
for v in news_data.to_dict(orient="records")
if isinstance(v, dict)
]))
if news:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_stock_news.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest
from unittest.mock import patch

import mcp_aktools


class StockNewsFailureTest(unittest.TestCase):
@patch("mcp_aktools.ak_cache", return_value=None)
def test_reports_unavailable_when_source_fetch_fails(self, _mock_cache):
self.assertEqual(
mcp_aktools.stock_news("SYNTHETIC"),
"News unavailable for SYNTHETIC",
)


if __name__ == "__main__":
unittest.main()
0