Files
group_fqcd_jr/tests/unit/service/test_fund_quote_service.py
T
张胜宇 e239eb778b docs: 品牌全量口径统一为「南方基金」+ 作废文档清理
1) 客服 Agent 四份交付文档 + 构建脚手架:品牌由包装占位 XX科技 / 旧名 南方财富
   统一为南方基金(热线 400-889-8899 / 官网 nffund.com),系统名改为「智能服务系统」;
   同步追加 §0.4 修订记录行,工程记录行保留原占位字面以支撑硬编码扫描验收。
2) 开发文档:清理 28 份已作废/残留文档(14 份移出归档 + 14 份仓库副本),
   新增《文档规整方案与开发前待决事项-2026-09-17》。
3) 客服agent 四份交付文档首次纳入本分支。
2026-09-17 15:15:22 +08:00

69 lines
2.4 KiB
Python

from datetime import UTC, datetime
from decimal import Decimal
import pytest
from app.core.fund_contracts import FundQuoteQuery
from app.service.fund_quote_service import FundQuoteService
class StubAdapter:
async def fetch_names(self, codes: list[str]) -> dict[str, str]:
return {code: f"基金-{code}" for code in codes}
async def fetch_quotes(self, codes: list[str]) -> dict[str, dict[str, object]]:
return {code: {"nav": Decimal("1.23"), "daily_change": Decimal("0.82")} for code in codes}
async def fetch_history(self, code: str, target_date: object) -> dict[str, object]:
return {
"nav": Decimal("1.20"), "nav_date": target_date,
"daily_change": Decimal("0.10"), "degraded": False,
}
@pytest.mark.asyncio
async def test_service_uses_intraday_quote_during_market_hours() -> None:
service = FundQuoteService(StubAdapter())
result = await service.query(
FundQuoteQuery(fund_codes=("159511",)),
now=datetime(2026, 9, 9, 2, 0, tzinfo=UTC),
)
assert result[0].nav == Decimal("1.23")
assert result[0].is_intraday is True
assert result[0].degraded is False
@pytest.mark.asyncio
async def test_service_uses_history_outside_market_hours() -> None:
service = FundQuoteService(StubAdapter())
result = await service.query(
FundQuoteQuery(fund_codes=("159511",)),
now=datetime(2026, 9, 9, 8, 0, tzinfo=UTC),
)
assert result[0].nav == Decimal("1.20")
assert result[0].is_intraday is False
@pytest.mark.asyncio
async def test_service_filters_type_and_rejects_unknown_type() -> None:
service = FundQuoteService(StubAdapter())
result = await service.query(FundQuoteQuery(fund_type="债券型", limit=1))
assert len(result) == 1
assert result[0].fund_type == "债券型"
with pytest.raises(ValueError, match="基金类型"):
await service.query(FundQuoteQuery(fund_type="未知"))
@pytest.mark.asyncio
async def test_intraday_missing_live_quote_is_degraded() -> None:
class NoLive(StubAdapter):
async def fetch_quotes(self, codes: list[str]) -> dict[str, dict[str, object]]:
return {}
result = await FundQuoteService(NoLive()).query(
FundQuoteQuery(fund_codes=("159511",)),
now=datetime(2026, 9, 9, 2, 0, tzinfo=UTC),
)
assert result[0].degraded is True
assert result[0].quote_source == "degraded"