Files
group_fqcd_jr/tests/unit/service/test_fund_quote_cache.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

54 lines
1.8 KiB
Python

from datetime import UTC, datetime
from decimal import Decimal
import pytest
from app.core.fund_contracts import FundQuoteQuery
from app.infrastructure.fund_quote_cache import FundQuoteCache
from app.infrastructure.memory_cache import CacheReadResult
from app.service.fund_quote_service import FundQuoteService
class MemoryClient:
def __init__(self) -> None:
self.values: dict[str, str] = {}
self.ttls: dict[str, int] = {}
async def get(self, key: str) -> CacheReadResult:
return CacheReadResult(self.values.get(key))
async def set(self, key: str, value: str, ttl_seconds: int) -> bool:
self.values[key] = value
self.ttls[key] = ttl_seconds
return True
class Adapter:
calls = 0
async def fetch_names(self, codes: list[str]) -> dict[str, str]:
self.calls += 1
return {code: "测试基金" for code in codes}
async def fetch_quotes(self, codes: list[str]) -> dict[str, dict[str, object]]:
self.calls += 1
return {code: {"nav": Decimal("1.2")} for code in codes}
async def fetch_history(self, code: str, target_date: object) -> dict[str, object]:
self.calls += 1
return {"nav": Decimal("1.1"), "nav_date": target_date, "degraded": False}
@pytest.mark.asyncio
async def test_service_reads_and_writes_cache() -> None:
client = MemoryClient()
adapter = Adapter()
service = FundQuoteService(adapter, FundQuoteCache(client))
query = FundQuoteQuery(fund_codes=("159511",))
first = await service.query(query, now=datetime(2026, 9, 9, 8, tzinfo=UTC))
second = await service.query(query, now=datetime(2026, 9, 9, 8, tzinfo=UTC))
assert first[0].quote_source == "eastmoney"
assert second[0].quote_source == "cache"
assert adapter.calls == 2
assert next(iter(client.ttls.values())) == 900