From 048ea1585b4e1b89ef1f547844869ef9a74accd8 Mon Sep 17 00:00:00 2001
From: log
Date: Thu, 09 Jul 2026 18:36:34 +0800
Subject: [PATCH] fix: abort akshare history after repeated failures

---
 tests/test_data_update_v2.py |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/tests/test_data_update_v2.py b/tests/test_data_update_v2.py
index 02e103c..4e02a65 100644
--- a/tests/test_data_update_v2.py
+++ b/tests/test_data_update_v2.py
@@ -84,6 +84,58 @@
 
 
 class DataUpdateV2BaoStockTests(unittest.TestCase):
+    def test_fetch_akshare_stock_list_prefers_exchange_code_name_api(self):
+        class FakeAkShare:
+            def stock_info_a_code_name(self):
+                return pd.DataFrame(
+                    [
+                        {"code": "000001", "name": "平安银行"},
+                        {"code": "600519", "name": "贵州茅台"},
+                    ]
+                )
+
+            def stock_zh_a_spot_em(self):
+                raise AssertionError("stock_zh_a_spot_em should not be called")
+
+        result = data_update_v2.fetch_akshare_stock_list(FakeAkShare())
+
+        self.assertEqual(
+            result.to_dict("records"),
+            [
+                {"代码": "000001", "名称": "平安银行"},
+                {"代码": "600519", "名称": "贵州茅台"},
+            ],
+        )
+
+    def test_akshare_history_failures_abort_before_full_stock_pool(self):
+        class FakeAkShare:
+            def __init__(self):
+                self.history_calls = 0
+
+            def stock_info_a_code_name(self):
+                return pd.DataFrame(
+                    [
+                        {"code": f"{index:06d}", "name": f"测试{index}"}
+                        for index in range(1, 21)
+                    ]
+                )
+
+            def stock_zh_a_hist(self, **kwargs):
+                self.history_calls += 1
+                raise ConnectionError(
+                    "Remote end closed connection without response"
+                )
+
+        fake_ak = FakeAkShare()
+
+        with patch.dict(sys.modules, {"akshare": fake_ak}):
+            result = data_update_v2.fetch_real_stock_data_from_akshare_only(
+                n_stocks=20, days=10, max_consecutive_failures=3
+            )
+
+        self.assertIsNone(result)
+        self.assertEqual(fake_ak.history_calls, 3)
+
     def test_convert_baostock_history_matches_existing_schema(self):
         raw = pd.DataFrame(
             [

--
Gitblit v1.8.0