From bee20e574656edaaf98f30799faf8daabeba7fc6 Mon Sep 17 00:00:00 2001
From: log
Date: Thu, 09 Jul 2026 18:44:32 +0800
Subject: [PATCH] fix: reuse akshare stock list for baostock history
---
tests/test_data_update_v2.py | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 111 insertions(+), 0 deletions(-)
diff --git a/tests/test_data_update_v2.py b/tests/test_data_update_v2.py
index 02e103c..fe36f87 100644
--- a/tests/test_data_update_v2.py
+++ b/tests/test_data_update_v2.py
@@ -36,6 +36,8 @@
class FakeBaoStock:
def __init__(self):
self.logged_out = False
+ self.stock_basic_calls = 0
+ self.history_codes = []
def login(self):
return type("LoginResult", (), {"error_code": "0", "error_msg": "success"})()
@@ -44,6 +46,7 @@
self.logged_out = True
def query_stock_basic(self):
+ self.stock_basic_calls += 1
return FakeBaoStockResult(
pd.DataFrame(
[
@@ -62,6 +65,7 @@
def query_history_k_data_plus(
self, code, fields, start_date, end_date, frequency, adjustflag
):
+ self.history_codes.append(code)
return FakeBaoStockResult(
pd.DataFrame(
[
@@ -84,6 +88,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(
[
@@ -118,6 +174,60 @@
def test_fetch_uses_baostock_when_akshare_returns_no_rows(self):
fake_bs = FakeBaoStock()
+ akshare_stock_list = pd.DataFrame(
+ [{"代码": "600519", "名称": "贵州茅台"}]
+ )
+
+ with patch.dict(sys.modules, {"baostock": fake_bs}):
+ with patch.object(
+ data_update_v2,
+ "fetch_stock_list_from_akshare",
+ return_value=akshare_stock_list,
+ ):
+ with patch.object(
+ data_update_v2,
+ "fetch_real_stock_data_from_akshare_only",
+ return_value=None,
+ ):
+ result = data_update_v2.fetch_real_stock_data_from_akshare(
+ n_stocks=1, days=10
+ )
+
+ self.assertIsNotNone(result)
+ self.assertEqual(result["stock_code"].unique().tolist(), ["600519"])
+ self.assertEqual(fake_bs.history_codes, ["sh.600519"])
+ self.assertEqual(fake_bs.stock_basic_calls, 0)
+ self.assertIn("ma5", result.columns)
+ self.assertTrue(fake_bs.logged_out)
+
+ def test_fetch_baostock_can_use_akshare_stock_list(self):
+ fake_bs = FakeBaoStock()
+ stock_list = pd.DataFrame(
+ [
+ {"代码": "000001", "名称": "平安银行"},
+ {"代码": "600519", "名称": "贵州茅台"},
+ ]
+ )
+
+ with patch.dict(sys.modules, {"baostock": fake_bs}):
+ result = data_update_v2.fetch_real_stock_data_from_baostock(
+ n_stocks=2, days=10, stock_list=stock_list
+ )
+
+ self.assertIsNotNone(result)
+ self.assertEqual(fake_bs.history_codes, ["sz.000001", "sh.600519"])
+ self.assertEqual(fake_bs.stock_basic_calls, 0)
+ self.assertEqual(
+ result[["stock_code", "stock_name"]].drop_duplicates().to_dict("records"),
+ [
+ {"stock_code": "000001", "stock_name": "平安银行"},
+ {"stock_code": "600519", "stock_name": "贵州茅台"},
+ ],
+ )
+ self.assertTrue(fake_bs.logged_out)
+
+ def test_fetch_baostock_falls_back_to_baostock_list_without_akshare_list(self):
+ fake_bs = FakeBaoStock()
with patch.dict(sys.modules, {"baostock": fake_bs}):
with patch.object(
@@ -131,6 +241,7 @@
self.assertIsNotNone(result)
self.assertEqual(result["stock_code"].unique().tolist(), ["600519"])
+ self.assertEqual(fake_bs.stock_basic_calls, 1)
self.assertIn("ma5", result.columns)
self.assertTrue(fake_bs.logged_out)
--
Gitblit v1.8.0