Skip to content

Commit 8d18f4c

Browse files
committed
fix(test): 修复 test_query_daily_groups_by_local_date 日期敏感 Bug 导致 Python 3.14 CI 失败;
将硬编码 UTC 时间戳改为基于 datetime.now() 的动态计算,消除随时间推移 数据滑出查询窗口的隐患,同时修正 strftime 格式字符串缺失 %S 的问题。 🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist) Co-Authored-By: Aurelius Huang<threefish.ai@gmail.com>
1 parent 71fe9b8 commit 8d18f4c

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

‎tests/test_token_logger.py‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,17 @@ async def test_query_daily_days_boundary_exact(logger):
410410
@pytest.mark.asyncio
411411
async def test_query_daily_groups_by_local_date(logger):
412412
"""UTC 时间 16:30 (= UTC+8 次日 00:30) 应归入本地次日."""
413-
# 模拟:北京时间 2026-04-04 00:30 → UTC 2026-04-03 16:30
414-
utc_ts = "2026-04-03T16:30:00.000Z"
413+
# 动态计算:取「今天本地日期」的前一天作为基准,
414+
# 构造该日本地 00:30(= UTC 前日 16:30)的时间戳,
415+
# 确保 days=7 窗口必然包含此数据。
416+
now_local = datetime.now(_SHANGHAI)
417+
target_local_date = now_local.date() - timedelta(days=1) # 昨天
418+
target_local_dt = datetime(
419+
target_local_date.year, target_local_date.month,
420+
target_local_date.day, 0, 30, 0, tzinfo=_SHANGHAI,
421+
)
422+
utc_ts = target_local_dt.astimezone(UTC).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
423+
415424
await logger._db.execute(
416425
"""INSERT INTO usage_log (ts, vendor, model_requested, model_served,
417426
input_tokens, output_tokens)
@@ -424,8 +433,8 @@ async def test_query_daily_groups_by_local_date(logger):
424433
rows = await logger.query_daily(days=7)
425434

426435
assert len(rows) == 1
427-
# 在 UTC+8 下,UTC 16:30 是次日 00:30,应显示为 2026-04-04
428-
assert rows[0]["date"] == "2026-04-04"
436+
# 在 UTC+8 下,UTC 16:30 是次日 00:30,应显示为 target_local_date
437+
assert rows[0]["date"] == target_local_date.strftime("%Y-%m-%d")
429438

430439

431440
@pytest.mark.asyncio

0 commit comments

Comments
 (0)