47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
|
|
from datetime import date
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
import tushare as ts # pyright: ignore[reportMissingTypeStubs]
|
||
|
|
|
||
|
|
from zhixing_server.modules.market_data.domain.models import SyncWindow
|
||
|
|
from zhixing_server.modules.market_data.infrastructure.tushare import TushareAdapter
|
||
|
|
|
||
|
|
|
||
|
|
def test_from_token_reuses_api_client_for_pro_bar(
|
||
|
|
monkeypatch: pytest.MonkeyPatch,
|
||
|
|
) -> None:
|
||
|
|
created_client = object()
|
||
|
|
calls: list[dict[str, object]] = []
|
||
|
|
|
||
|
|
def fake_pro_api(token: str) -> object:
|
||
|
|
assert token
|
||
|
|
return created_client
|
||
|
|
|
||
|
|
def fake_pro_bar(**kwargs: object) -> list[dict[str, object]]:
|
||
|
|
calls.append(kwargs)
|
||
|
|
return [
|
||
|
|
{
|
||
|
|
"ts_code": "000001.SZ",
|
||
|
|
"trade_date": "20240102",
|
||
|
|
"close": "10",
|
||
|
|
}
|
||
|
|
]
|
||
|
|
|
||
|
|
monkeypatch.setattr(ts, "pro_api", fake_pro_api)
|
||
|
|
monkeypatch.setattr(ts, "pro_bar", fake_pro_bar)
|
||
|
|
|
||
|
|
adapter = TushareAdapter.from_token(
|
||
|
|
"test-token",
|
||
|
|
request_interval_seconds=0,
|
||
|
|
)
|
||
|
|
|
||
|
|
bars = adapter.fetch_bars(
|
||
|
|
"000001.SZ",
|
||
|
|
SyncWindow(start=date(2024, 1, 2), end=date(2024, 1, 2)),
|
||
|
|
)
|
||
|
|
|
||
|
|
assert bars[0].ts_code == "000001.SZ"
|
||
|
|
assert calls
|
||
|
|
assert calls[0]["api"] is created_client
|
||
|
|
assert calls[0]["adj"] == "qfq"
|