Files
ashare-data/tests/test_financial_quarters.py
T
曾志威 dbc8107caa P0/P1/P2 一次性落地:补齐文档、修复 main 挂载/笔误、引入 logging+tests
P0:
- 清理 src/fetchers/sector.py 第 225 行起的旧版残留代码
- 修复 src/fetchers/market_daily.py 中 fetch_history -> _fetch_history 笔误
- 在 src/main.py 挂载 --market-daily 子命令

P1:
- 修正 src/db.py docstring(market_breadth -> market_daily 等)
- requirements.txt 补 baostock;pyproject.toml 同步 + 新增 [dev] extras
- README 增加 config.yaml 安全提示,将 git 历史清理升级为高风险 P0 由用户决策

P2:
- 引入 src/log.py 统一 logging(控制台 + logs/ashare.log 按日滚动 7 天)
- 建立 tests/ 框架,4 个测试文件 / 19 个 pytest 用例全部通过
- pyproject.toml 新增 [tool.pytest.ini_options]
- config.example.yaml 补全 workers 字段与多源说明
- README 更新功能概览/参数说明/表结构/项目结构/设计说明全章节
- TODO.md 全面重写,按 P0/P2 整理剩余条目并附变更日志
- .gitignore 新增 .claude/
2026-05-15 13:26:21 +08:00

51 lines
1.4 KiB
Python

"""验证 financial._recent_quarters 的季度滚动逻辑"""
from datetime import datetime
from unittest.mock import patch
from src.fetchers import financial
def _fake_now(year: int, month: int, day: int = 15):
"""生成一个固定时间,用于 patch datetime.now()"""
fixed = datetime(year, month, day)
class FakeDatetime(datetime):
@classmethod
def now(cls, tz=None): # noqa: ARG003
return fixed
return FakeDatetime
def test_count_matches():
with patch.object(financial, "datetime", _fake_now(2026, 5, 15)):
out = financial._recent_quarters(8)
assert len(out) == 8
def test_first_is_current_quarter():
# 2026-05-15 属于 Q2
with patch.object(financial, "datetime", _fake_now(2026, 5, 15)):
out = financial._recent_quarters(4)
assert out[0] == (2026, 2)
def test_crosses_year_boundary():
# 2026 Q1 → 2025 Q4 → 2025 Q3 → 2025 Q2
with patch.object(financial, "datetime", _fake_now(2026, 2, 10)):
out = financial._recent_quarters(4)
assert out == [(2026, 1), (2025, 4), (2025, 3), (2025, 2)]
def test_q1_january():
with patch.object(financial, "datetime", _fake_now(2026, 1, 1)):
out = financial._recent_quarters(2)
assert out == [(2026, 1), (2025, 4)]
def test_q4_december():
with patch.object(financial, "datetime", _fake_now(2025, 12, 31)):
out = financial._recent_quarters(2)
assert out == [(2025, 4), (2025, 3)]