| | |
| | | self.assertIsNone(data_update_v2.parse_n_stocks("全部")) |
| | | self.assertEqual(data_update_v2.parse_n_stocks("6000"), 6000) |
| | | |
| | | def test_parse_args_supports_bse_only(self): |
| | | args = data_update_v2.parse_args(["--bse-only", "--days", "30"]) |
| | | |
| | | self.assertTrue(args.bse_only) |
| | | self.assertEqual(args.days, 30) |
| | | |
| | | def test_bse_codes_use_tencent_instead_of_baostock(self): |
| | | self.assertIsNone(data_update_v2.to_baostock_code("920099")) |
| | | self.assertIsNone(data_update_v2.to_baostock_code("830001")) |
| | |
| | | self.assertEqual(data_update_v2.to_tencent_code("830001"), "bj830001") |
| | | self.assertEqual(data_update_v2.to_baostock_code("600519"), "sh.600519") |
| | | self.assertEqual(data_update_v2.to_baostock_code("000001"), "sz.000001") |
| | | |
| | | def test_load_bse_code_mapping_contains_bettery_old_code(self): |
| | | mapping = data_update_v2.load_bse_code_mapping() |
| | | |
| | | self.assertEqual(mapping["920185"], "835185") |
| | | self.assertEqual(len(mapping), 248) |
| | | |
| | | def test_tencent_bse_symbol_candidates_include_legacy_code(self): |
| | | symbols = data_update_v2.tencent_bse_symbol_candidates( |
| | | "920185", {"920185": "835185"} |
| | | ) |
| | | |
| | | self.assertEqual(symbols, ["bj920185", "bj835185"]) |
| | | |
| | | def test_convert_tencent_history_matches_existing_schema(self): |
| | | raw = pd.DataFrame( |
| | |
| | | self.assertEqual(result.loc[0, "amount"], 2050000.0) |
| | | self.assertAlmostEqual(result.loc[1, "change_pct"], (21.0 / 20.5 - 1) * 100) |
| | | self.assertTrue(pd.isna(result.loc[0, "turnover"])) |
| | | |
| | | def test_merge_replacement_data_replaces_only_downloaded_stocks(self): |
| | | existing = pd.DataFrame( |
| | | [ |
| | | {"stock_code": "600519", "date": pd.Timestamp("2026-07-08"), "close": 1.0}, |
| | | {"stock_code": "920185", "date": pd.Timestamp("2026-07-08"), "close": 2.0}, |
| | | {"stock_code": "920187", "date": pd.Timestamp("2026-07-08"), "close": 3.0}, |
| | | ] |
| | | ) |
| | | replacement = pd.DataFrame( |
| | | [ |
| | | {"stock_code": "920185", "date": pd.Timestamp("2026-07-09"), "close": 4.0}, |
| | | ] |
| | | ) |
| | | |
| | | result = data_update_v2.merge_replacement_data(existing, replacement) |
| | | |
| | | self.assertEqual( |
| | | set(result["stock_code"]), {"600519", "920185", "920187"} |
| | | ) |
| | | self.assertEqual( |
| | | result[result["stock_code"] == "920185"]["close"].tolist(), [4.0] |
| | | ) |
| | | |
| | | def test_tencent_bse_fetch_uses_bj_market_prefix(self): |
| | | class FakeAkShare: |
| | |
| | | self.assertEqual(fake_ak.symbols, ["bj920099"]) |
| | | self.assertEqual(result["stock_code"].unique().tolist(), ["920099"]) |
| | | |
| | | def test_tencent_bse_fetch_falls_back_to_legacy_code_on_missing_day(self): |
| | | class FakeAkShare: |
| | | def __init__(self): |
| | | self.symbols = [] |
| | | |
| | | def stock_zh_a_hist_tx(self, **kwargs): |
| | | symbol = kwargs["symbol"] |
| | | self.symbols.append(symbol) |
| | | if symbol == "bj920185": |
| | | raise KeyError("day") |
| | | return pd.DataFrame( |
| | | [ |
| | | { |
| | | "date": "2026-07-08", |
| | | "open": 20.0, |
| | | "close": 20.5, |
| | | "high": 20.8, |
| | | "low": 19.8, |
| | | "amount": 1000.0, |
| | | } |
| | | ] |
| | | ) |
| | | |
| | | fake_ak = FakeAkShare() |
| | | stock_list = pd.DataFrame([{"代码": "920185", "名称": "贝特瑞"}]) |
| | | |
| | | with patch.dict(sys.modules, {"akshare": fake_ak}): |
| | | with patch.object( |
| | | data_update_v2, |
| | | "load_bse_code_mapping", |
| | | return_value={"920185": "835185"}, |
| | | ): |
| | | result = data_update_v2.fetch_real_stock_data_from_tencent_bse( |
| | | stock_list=stock_list, |
| | | days=365, |
| | | max_retries=2, |
| | | retry_delay=0, |
| | | request_delay=0, |
| | | ) |
| | | |
| | | self.assertIsNotNone(result) |
| | | self.assertEqual(fake_ak.symbols, ["bj920185", "bj835185"]) |
| | | self.assertEqual(result.loc[0, "stock_code"], "920185") |
| | | self.assertEqual(result.loc[0, "stock_code_api"], "bj835185") |
| | | |
| | | def test_tencent_bse_fetch_splits_date_range_at_code_switch(self): |
| | | class FakeAkShare: |
| | | def __init__(self): |
| | | self.calls = [] |
| | | |
| | | def stock_zh_a_hist_tx(self, **kwargs): |
| | | self.calls.append(kwargs) |
| | | date = "2026-07-08" if kwargs["symbol"] == "bj920185" else "2025-10-08" |
| | | return pd.DataFrame( |
| | | [ |
| | | { |
| | | "date": date, |
| | | "open": 20.0, |
| | | "close": 20.5, |
| | | "high": 20.8, |
| | | "low": 19.8, |
| | | "amount": 1000.0, |
| | | } |
| | | ] |
| | | ) |
| | | |
| | | fake_ak = FakeAkShare() |
| | | stock_list = pd.DataFrame([{"代码": "920185", "名称": "贝特瑞"}]) |
| | | |
| | | with patch.dict(sys.modules, {"akshare": fake_ak}): |
| | | with patch.object( |
| | | data_update_v2, |
| | | "load_bse_code_mapping", |
| | | return_value={"920185": "835185"}, |
| | | ): |
| | | result = data_update_v2.fetch_real_stock_data_from_tencent_bse( |
| | | stock_list=stock_list, |
| | | days=365, |
| | | request_delay=0, |
| | | ) |
| | | |
| | | self.assertEqual(len(result), 2) |
| | | self.assertEqual(fake_ak.calls[0]["symbol"], "bj920185") |
| | | self.assertEqual(fake_ak.calls[0]["start_date"], "20251009") |
| | | self.assertEqual(fake_ak.calls[1]["symbol"], "bj835185") |
| | | self.assertEqual(fake_ak.calls[1]["end_date"], "20251008") |
| | | |
| | | def test_fetch_stock_list_from_akshare_all_keeps_full_list(self): |
| | | stock_list = pd.DataFrame( |
| | | [{"代码": f"{index:06d}", "名称": f"测试{index}"} for index in range(10)] |