fix(management): 评审修正——config_json注解对齐NOT NULL+补3个约束测试
- EmployeeScoreConfig.config_json 去掉 | None,注解与 DDL(nullable=False)一致 - 删测试死 import datetime as dt - 补约束测试: metric_daily 复合PK防重/ call_daily PK防重/ config_json NOT NULL (JSON 列 Python None 序列化为 'null' 字符串,测 NOT NULL 须 sa.null()) - EmployeeScoreConfig docstring 补 JSON 列整列重赋值注记 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -3,8 +3,6 @@
|
||||
只验证:建表可用、主键/唯一约束生效、默认值正确。业务逻辑在
|
||||
first_reply_facts / employee_metrics 的测试里。
|
||||
"""
|
||||
import datetime as dt # noqa: F401
|
||||
|
||||
import pytest
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
@@ -62,6 +60,19 @@ async def test_metric_daily_defaults_zero_not_none(make_session):
|
||||
assert row.data_complete is False or row.data_complete == 0 # SQLite 0/1
|
||||
|
||||
|
||||
async def test_metric_daily_pk_duplicate_rejected(make_session):
|
||||
"""同 (day, channel, user_id, metric_version) 复合主键防重。
|
||||
|
||||
Task 6 delete+insert 幂等重算的安全网:版本内重复落表直接 IntegrityError。
|
||||
"""
|
||||
s = make_session
|
||||
s.add(EmployeeMetricDaily(day=20260901, channel="wechat", user_id="u1"))
|
||||
await s.commit()
|
||||
s.add(EmployeeMetricDaily(day=20260901, channel="wechat", user_id="u1"))
|
||||
with pytest.raises(IntegrityError):
|
||||
await s.commit()
|
||||
|
||||
|
||||
async def test_call_daily_roundtrip(make_session):
|
||||
s = make_session
|
||||
s.add(EmployeeCallDaily(day=20260901, user_id="u1", call_count=2,
|
||||
@@ -72,6 +83,15 @@ async def test_call_daily_roundtrip(make_session):
|
||||
assert row.call_seconds == 164
|
||||
|
||||
|
||||
async def test_call_daily_pk_duplicate_rejected(make_session):
|
||||
s = make_session
|
||||
s.add(EmployeeCallDaily(day=20260901, user_id="u1"))
|
||||
await s.commit()
|
||||
s.add(EmployeeCallDaily(day=20260901, user_id="u1"))
|
||||
with pytest.raises(IntegrityError):
|
||||
await s.commit()
|
||||
|
||||
|
||||
async def test_score_config_version_unique(make_session):
|
||||
s = make_session
|
||||
s.add(EmployeeScoreConfig(version="v1.0", name="一期试算",
|
||||
@@ -83,6 +103,19 @@ async def test_score_config_version_unique(make_session):
|
||||
await s.commit()
|
||||
|
||||
|
||||
async def test_score_config_config_json_not_null(make_session):
|
||||
"""config_json NOT NULL——注解与 DDL 一致(修复:去掉 | None)。
|
||||
|
||||
注意 JSON 列语义:Python None 经 JSON 序列化变成字符串 'null' 而非
|
||||
SQL NULL,不会违反约束;测 NOT NULL 须用 sa.null() 显式发 SQL NULL。
|
||||
"""
|
||||
s = make_session
|
||||
s.add(EmployeeScoreConfig(version="v1.0", name="空配置",
|
||||
config_json=sa.null()))
|
||||
with pytest.raises(IntegrityError):
|
||||
await s.commit()
|
||||
|
||||
|
||||
async def test_snapshot_batch_user_unique(make_session):
|
||||
s = make_session
|
||||
common = {"snapshot_batch_id": "b1", "period_type": "week",
|
||||
|
||||
Reference in New Issue
Block a user