1) 客服 Agent 四份交付文档 + 构建脚手架:品牌由包装占位 XX科技 / 旧名 南方财富 统一为南方基金(热线 400-889-8899 / 官网 nffund.com),系统名改为「智能服务系统」; 同步追加 §0.4 修订记录行,工程记录行保留原占位字面以支撑硬编码扫描验收。 2) 开发文档:清理 28 份已作废/残留文档(14 份移出归档 + 14 份仓库副本), 新增《文档规整方案与开发前待决事项-2026-09-17》。 3) 客服agent 四份交付文档首次纳入本分支。
75 lines
3.0 KiB
Python
75 lines
3.0 KiB
Python
"""会话时区回归测试:存储层必须统一 UTC。
|
|
|
|
为什么值得单独一条集成测试:这个缺陷的表现是"数据照写、接口照跑、时间悄悄错 8 小时",
|
|
只有在比较 DB 默认值与默认值之外的第二个时间源时才会暴露。历史上它造成过一次真实
|
|
故障——给 `sys_user_role.assigned_at` 用 MySQL `NOW()` 写入的时间超前 UTC 8 小时,
|
|
被 RBAC 的 `assigned_at <= now` 判为"尚未生效",接口直接 403。
|
|
|
|
同时锁住实现方式:`init_command` 必须仍在 DSN 上。曾试过用 SQLAlchemy 的 `connect`
|
|
事件写 `SET time_zone`,在 asyncmy 这套 asyncio 方言上**不报错也不生效**,
|
|
静默退回本地时区——所以这里断言的是"连接后的会话时区"这一可观测事实。
|
|
"""
|
|
|
|
import pytest
|
|
from sqlalchemy import text
|
|
|
|
from app.infrastructure.db import SessionFactory
|
|
|
|
UTC_OFFSET_SECONDS = 0
|
|
|
|
|
|
@pytest.mark.integration
|
|
async def test_mysql_session_timezone_is_utc():
|
|
async with SessionFactory() as session:
|
|
time_zone = (await session.execute(text("SELECT @@session.time_zone"))).scalar()
|
|
offset = (
|
|
await session.execute(
|
|
text("SELECT TIMESTAMPDIFF(SECOND, UTC_TIMESTAMP(6), NOW(6))")
|
|
)
|
|
).scalar()
|
|
|
|
assert time_zone == "+00:00", f"会话时区应被钉在 UTC,实际 {time_zone!r}"
|
|
assert offset == UTC_OFFSET_SECONDS, f"会话时间与 UTC 相差 {offset} 秒,应为 0"
|
|
|
|
|
|
@pytest.mark.integration
|
|
async def test_db_default_timestamp_matches_application_utc():
|
|
"""DB 的 `CURRENT_TIMESTAMP` 默认值必须与应用侧 `datetime.now(UTC)` 同源。
|
|
|
|
断言方式刻意不依赖具体时区:同一行里写入一次 DB 默认值和一次应用值,
|
|
两者相差必须为 0 秒——改动前这条断言会得到 28800 秒。
|
|
"""
|
|
from datetime import UTC, datetime
|
|
|
|
async with SessionFactory() as session:
|
|
await session.execute(
|
|
text(
|
|
"CREATE TEMPORARY TABLE tz_default_probe ("
|
|
" id INT PRIMARY KEY,"
|
|
" db_default DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),"
|
|
" app_value DATETIME(6) NOT NULL)"
|
|
)
|
|
)
|
|
app_now = datetime.now(UTC).replace(tzinfo=None)
|
|
await session.execute(
|
|
text(
|
|
"INSERT INTO tz_default_probe (id, app_value) VALUES (:id, :app_value)"
|
|
),
|
|
{"id": 1, "app_value": app_now},
|
|
)
|
|
row = (
|
|
await session.execute(
|
|
text(
|
|
"SELECT db_default, app_value,"
|
|
" TIMESTAMPDIFF(SECOND, app_value, db_default) AS gap_seconds"
|
|
" FROM tz_default_probe WHERE id = 1"
|
|
)
|
|
)
|
|
).mappings().one()
|
|
await session.execute(text("DROP TEMPORARY TABLE tz_default_probe"))
|
|
|
|
assert row["gap_seconds"] == UTC_OFFSET_SECONDS, (
|
|
f"DB 默认值 {row['db_default']} 与应用写入 {row['app_value']} 相差 "
|
|
f"{row['gap_seconds']} 秒,说明会话时区未统一到 UTC"
|
|
)
|