Merge remote-tracking branch 'origin/qyqy_develop' into qyqy_develop

This commit is contained in:
2026-09-12 14:18:06 +08:00
7 changed files with 490 additions and 62 deletions
@@ -36,6 +36,92 @@ def test_hq_exchange_history_parser_contract(monkeypatch: pytest.MonkeyPatch) ->
}]
def test_hq_exchange_history_retries_invalid_json(monkeypatch: pytest.MonkeyPatch) -> None:
import hq
calls = 0
class Response:
def raise_for_status(self) -> None:
return None
def json(self) -> dict[str, object]:
nonlocal calls
calls += 1
if calls == 1:
raise ValueError("temporary invalid response")
return {"data": {"klines": [
"2026-09-09,1.20,1.23,1.24,1.19,100000,1234567.89",
]}}
monkeypatch.setattr(hq.httpx, "get", lambda *args, **kwargs: Response())
monkeypatch.setattr(hq, "EXCHANGE_HISTORY_MIN_INTERVAL_SECONDS", 0)
monkeypatch.setattr(hq.time, "sleep", lambda _seconds: None)
rows = hq.get_southern_fund_exchange_history("159511", "2026-09-09", "2026-09-09")
assert calls == 2
assert rows[0]["turnover_amount"] == "1234567.89"
def test_tencent_history_parser_uses_explicit_amount_field(
monkeypatch: pytest.MonkeyPatch,
) -> None:
import hq
class Response:
text = (
'kline_day2026={"code":0,"data":{"sz159511":{"day":['
'["2026-09-09","2.37","2.38","2.40","2.36","82036.00",{},'
'"4.10","233.83","0.000","0.000"]]}}}'
)
def raise_for_status(self) -> None:
return None
monkeypatch.setattr(hq.httpx, "get", lambda *args, **kwargs: Response())
monkeypatch.setattr(hq, "EXCHANGE_HISTORY_MIN_INTERVAL_SECONDS", 0)
rows = hq.get_southern_fund_exchange_history_tencent(
"159511", "2026-09-09", "2026-09-09"
)
assert rows[0]["close_price"] == "2.38"
assert rows[0]["turnover_amount"] == "2338300.00"
def test_nav_history_falls_back_to_tencent_when_eastmoney_nav_is_unavailable(
monkeypatch: pytest.MonkeyPatch,
) -> None:
import hq
monkeypatch.setattr(
hq,
"_fetch_nav_records",
lambda *args, **kwargs: (_ for _ in ()).throw(httpx.ConnectError("down")),
)
monkeypatch.setattr(
hq,
"get_southern_fund_exchange_history_tencent",
lambda *args, **kwargs: [{
"fund_code": "159511",
"trade_date": "2026-09-09",
"close_price": "2.380000",
"turnover_amount": "2338300.00",
}],
)
rows = hq.get_southern_fund_nav_history("159511", "2026-09-09", "2026-09-09")
assert rows == [{
"fund_code": "159511",
"trade_date": "2026-09-09",
"nav": "2.380000",
"source": "tencent_hq_history",
"turnover_amount": "2338300.00",
}]
@pytest.mark.asyncio
async def test_adapter_parses_names_quotes_and_history() -> None:
def handler(request: httpx.Request) -> httpx.Response: