247 lines
14 KiB
Python
Executable File
247 lines
14 KiB
Python
Executable File
"""周期、元信息、常规项目 / 分类 / 标签 / 执行记录 / 设置。"""
|
||
from __future__ import annotations
|
||
|
||
from datetime import date, timedelta
|
||
|
||
from app import constants
|
||
|
||
|
||
# ------------------------------------------------------------------ 周期
|
||
def test_周期列表会自动补出今天所在的周(h):
|
||
data = h.periods()
|
||
assert data["weeks"] and data["today_id"]
|
||
today = [w for w in data["weeks"] if w["id"] == data["today_id"]][0]
|
||
y, m, w = constants.week_of_date(__import__("datetime").date.today())
|
||
assert (today["year"], today["month"], today["week"]) == (y, m, w)
|
||
sorts = [x["sort_key"] for x in data["weeks"]]
|
||
assert sorts == sorted(sorts) # 顶栏按时间正序
|
||
assert all(x["first_day"] <= x["last_day"] for x in data["weeks"])
|
||
month_keys = {(x["year"], x["month"]) for x in data["months"]}
|
||
assert (y, m) in month_keys
|
||
assert sum(x["week_count"] for x in data["months"]) == len(data["weeks"])
|
||
|
||
|
||
def test_跨月周按周四所在月归属():
|
||
"""口径锁死:一周 = 自然周(周一~周日),归属月看这一周的周四落在哪个月。
|
||
|
||
2026-09-28 是周一,所在的周是 09/28–10/04,周四是 10/1 → 整周归 10月第1周,
|
||
所以 9/28、9/29 写的记录统计在 10 月。这是口径不是 bug(界面已把日期范围与
|
||
跨月事实标在顶栏、录入页和周期管理上)。改这条规则要连带迁移历史 period_id。
|
||
"""
|
||
assert constants.week_of_date(date(2026, 9, 27)) == (2026, 9, 4) # 9 月到这天为止
|
||
assert constants.week_of_date(date(2026, 9, 28)) == (2026, 10, 1) # ← 争议边界
|
||
assert constants.week_of_date(date(2026, 9, 30)) == (2026, 10, 1)
|
||
assert constants.week_of_date(date(2026, 10, 1)) == (2026, 10, 1)
|
||
assert constants.week_of_date(date(2026, 10, 4)) == (2026, 10, 1)
|
||
assert constants.week_of_date(date(2026, 10, 5)) == (2026, 10, 2)
|
||
# 9 月只有 4 个自然周,10 月有 5 个:一个月不可能正好 4 周
|
||
assert constants.period_first_day(2026, 9, 1) == "2026-08-31"
|
||
assert constants.period_last_day(2026, 9, 4) == "2026-09-27"
|
||
assert constants.period_first_day(2026, 10, 1) == "2026-09-28"
|
||
assert constants.period_last_day(2026, 10, 5) == "2026-11-01"
|
||
|
||
# period_first_day / week_of_date 互为逆运算
|
||
for y, m, w in [(2026, 1, 1), (2026, 9, 4), (2026, 10, 1), (2026, 10, 5), (2027, 2, 4)]:
|
||
first = date.fromisoformat(constants.period_first_day(y, m, w))
|
||
assert constants.week_of_date(first) == (y, m, w)
|
||
assert constants.period_last_day(y, m, w) == (first + timedelta(days=6)).isoformat()
|
||
|
||
# 全年按天扫一遍:每天都有唯一归属、不重不漏,每月周数是 4 或 5
|
||
per_month: dict[int, set] = {}
|
||
day = date(2026, 1, 1)
|
||
while day.year == 2026:
|
||
y, m, w = constants.week_of_date(day)
|
||
assert y == 2026
|
||
per_month.setdefault(m, set()).add(w)
|
||
day += timedelta(days=1)
|
||
assert len(per_month) == 12
|
||
for m, weeks in per_month.items():
|
||
assert weeks == set(range(1, len(weeks) + 1)), f"2026-{m:02d} 周数不连续:{sorted(weeks)}"
|
||
assert len(weeks) in (4, 5), f"2026-{m:02d} 有 {len(weeks)} 周"
|
||
|
||
|
||
def test_新建周期会按标签推年份并跨年(h):
|
||
base = h.periods()["today_id"]
|
||
latest = [w for w in h.periods()["weeks"] if w["id"] == base][0]
|
||
r = h.admin.post("/api/periods", json={"label": "12月第1周"})
|
||
assert r.status_code == 200
|
||
p = r.json()["period"]
|
||
# 12 月不早于当前月份 → 仍然是今年
|
||
assert p["year"] == latest["year"] and (p["month"], p["week"]) == (12, 1)
|
||
assert p["sort_key"] == p["year"] * 10000 + 1201
|
||
# 同一个周期不会重复建
|
||
again = h.admin.post("/api/periods", json={"label": "12月第1周"}).json()
|
||
assert again["created"] is False and again["period"]["id"] == p["id"]
|
||
# 再补 1 月:月份比最新周期小,说明跨年,年份 +1
|
||
p2 = h.admin.post("/api/periods", json={"label": "1月第1周"}).json()["period"]
|
||
assert p2["year"] == latest["year"] + 1 and p2["sort_key"] > p["sort_key"]
|
||
# 标签里写了年份就按写的算
|
||
p3 = h.admin.post("/api/periods", json={"label": f"{latest['year'] - 1}年3月第2周"}).json()["period"]
|
||
assert p3["year"] == latest["year"] - 1 and p3["week"] == 2
|
||
|
||
|
||
def test_新建周期的入参校验(h):
|
||
assert h.admin.post("/api/periods", json={}).status_code == 400
|
||
assert h.admin.post("/api/periods", json={"label": "随便写点什么"}).status_code == 400
|
||
p = h.period(2027, 5, 3)
|
||
assert (p["year"], p["month"], p["week"], p["sort_key"]) == (2027, 5, 3, 20270503)
|
||
|
||
|
||
def test_有记录的周期不能删(h):
|
||
pid = h.today_period_id
|
||
p = h.project("要被记录的周期项目")
|
||
h.update(p["id"], pid, content="本周推进")
|
||
r = h.admin.delete(f"/api/periods/{pid}")
|
||
assert r.status_code == 400 and "执行记录" in r.json()["detail"]
|
||
empty = h.period(2030, 1, 1)
|
||
assert h.admin.delete(f"/api/periods/{empty['id']}").json() == {"ok": True}
|
||
assert h.admin.delete("/api/periods/424242").status_code == 404
|
||
|
||
|
||
def test_元信息给状态字典和AI状态(h):
|
||
meta = h.admin.get("/api/meta").json()
|
||
assert [x["code"] for x in meta["statuses"]] == [c for c, _, _ in constants.STATUSES]
|
||
assert "priorities" not in meta # 优先级已随 Project.priority 下线
|
||
assert meta["ai"]["enabled"] is False # 没配 Key:走本地规则
|
||
assert meta["ai"]["has_key"] is False
|
||
|
||
|
||
# ------------------------------------------------------------------ 分类 / 标签
|
||
def test_分类增删改(h):
|
||
c = h.category("研发平台", color="#123456")
|
||
assert c["color"] == "#123456"
|
||
assert h.admin.post("/api/categories", json={"name": "研发平台"}).status_code == 400
|
||
up = h.admin.patch(f"/api/categories/{c['id']}", json={"name": "研发中台", "color": "#000"})
|
||
assert up.json()["name"] == "研发中台"
|
||
assert h.admin.delete(f"/api/categories/{c['id']}").status_code == 200
|
||
assert h.admin.patch(f"/api/categories/{c['id']}", json={"name": "x"}).status_code == 404
|
||
assert h.admin.delete("/api/categories/424242").status_code == 404
|
||
|
||
|
||
def test_同名标签多项目共用一条记录(h):
|
||
a = h.project("甲项目", tags=["重点"])
|
||
b = h.project("乙项目", tags=["重点", "新客户"])
|
||
assert a["tags"] and a["tags"][0]["id"] == b["tags"][0]["id"] # 同名标签不重复建
|
||
assert len(b["tags"]) == 2
|
||
|
||
|
||
# ------------------------------------------------------------------ 项目
|
||
def test_项目字段与人员标签的写入(h):
|
||
cat = h.category("交付项目")
|
||
p = h.project("智慧园区", category_id=cat["id"], owners=["张三", "李四"],
|
||
members=["王五", "赵六"], tags=["新客户", "重点"], is_key=True,
|
||
progress=35)
|
||
assert p["owner"] == "张三" and p["owners"] == ["张三", "李四"] # 第一位是主负责人
|
||
assert p["members"] == ["王五", "赵六"]
|
||
assert {t["name"] for t in p["tags"]} == {"新客户", "重点"}
|
||
assert p["category"]["name"] == "交付项目" and p["is_key"] and p["progress"] == 35
|
||
# 标签随项目写入自动创建,没有独立的建标签入口
|
||
# 换负责人名单是整体替换
|
||
up = h.admin.patch(f"/api/projects/{p['id']}", json={"owners": ["李四"], "tags": []})
|
||
assert up.json()["owner"] == "李四" and up.json()["owners"] == ["李四"] and up.json()["tags"] == []
|
||
|
||
|
||
def test_项目的重名与空名保护(h):
|
||
h.project("同名项目")
|
||
assert h.admin.post("/api/projects", json={"name": "同名项目"}).status_code == 400
|
||
p = h.project("另一个项目")
|
||
assert h.admin.patch(f"/api/projects/{p['id']}", json={"name": "同名项目"}).status_code == 400
|
||
assert h.admin.patch(f"/api/projects/{p['id']}", json={"name": " "}).status_code == 400
|
||
assert h.admin.patch("/api/projects/424242", json={"name": "不存在"}).status_code == 404
|
||
assert h.admin.get("/api/projects/424242").status_code == 404
|
||
assert h.admin.delete("/api/projects/424242").status_code == 404
|
||
|
||
|
||
def test_项目列表的筛选与本周期状态(h):
|
||
pid = h.today_period_id
|
||
cat = h.category("筛选专用")
|
||
a = h.project("甲项目", category_id=cat["id"], owners=["陈明"], is_key=True)
|
||
b = h.project("乙项目", owners=["刘芳"])
|
||
h.update(b["id"], pid, content="乙本周有记录")
|
||
rows = {x["name"]: x for x in h.admin.get("/api/projects").json()}
|
||
assert rows["甲项目"]["updated_this_period"] is False and rows["甲项目"]["idle_weeks"] is None
|
||
assert rows["乙项目"]["updated_this_period"] is True and rows["乙项目"]["update_count"] == 1
|
||
assert rows["甲项目"]["task_count"] == 0
|
||
assert {x["id"] for x in h.admin.get("/api/projects", params={"is_key": True}).json()} == {a["id"]}
|
||
assert {x["id"] for x in h.admin.get("/api/projects", params={"keyword": "甲"}).json()} == {a["id"]}
|
||
assert {x["id"] for x in h.admin.get("/api/projects", params={"category_id": cat["id"]}).json()} == {a["id"]}
|
||
assert {x["id"] for x in h.admin.get("/api/projects", params={"owner": "陈明"}).json()} == {a["id"]}
|
||
# 归档后默认看不到,archived=true 才出现
|
||
h.admin.patch(f"/api/projects/{a['id']}", json={"archived": True})
|
||
assert a["id"] not in {x["id"] for x in h.admin.get("/api/projects").json()}
|
||
assert a["id"] in {x["id"] for x in h.admin.get("/api/projects", params={"archived": "true"}).json()}
|
||
assert a["id"] in {x["id"] for x in h.admin.get("/api/projects", params={"archived": "all"}).json()}
|
||
assert b["id"] in {x["id"] for x in h.admin.get("/api/projects", params={"owner": "刘芳"}).json()}
|
||
|
||
|
||
def test_按周与按月都能取到本周期数据(h):
|
||
week = h.period(2028, 3, 2)
|
||
p = h.project("跨月记录")
|
||
h.update(p["id"], week["id"], content="2028年3月第2周")
|
||
# 项目清单始终给全量,周期范围只决定「本周期是否更新 / 隔了几周没更新」
|
||
rows = h.admin.get("/api/projects", params={"period_id": week["id"]}).json()
|
||
assert [x["name"] for x in rows] == ["跨月记录"] and rows[0]["updated_this_period"] is True
|
||
rows = h.admin.get("/api/projects", params={"year": 2028, "month": 3}).json()
|
||
assert rows and rows[0]["updated_this_period"] is True
|
||
assert rows[0]["idle_weeks"] == 0
|
||
# 该年月还没有任何周期 → 清单照旧返回,只是都算「本周期没更新」
|
||
rows = h.admin.get("/api/projects", params={"year": 2027, "month": 1}).json()
|
||
assert [x["name"] for x in rows] == ["跨月记录"] and rows[0]["updated_this_period"] is False
|
||
assert h.admin.get("/api/updates", params={"period_id": week["id"]}).json()[0]["content"] == "2028年3月第2周"
|
||
assert h.admin.get("/api/updates", params={"year": 2028, "month": 3}).json() != []
|
||
assert h.admin.get("/api/updates", params={"year": 2026, "month": 1}).json() == []
|
||
|
||
|
||
def test_执行记录的增改删(h):
|
||
pid = h.today_period_id
|
||
p = h.project("记录演练")
|
||
t0 = h.update(p["id"], pid, task_name="接口联调", content="联调启动")
|
||
assert t0["task_id"] # 写执行记录自动建子任务
|
||
t1 = h.update(p["id"], pid, task_name="接口联调", content="再记一笔")
|
||
assert t1["task_id"] == t0["task_id"] # 同名子任务复用,不重复建
|
||
u = h.update(p["id"], pid, task_id=t0["task_id"], content="联调过半", progress=60,
|
||
status="at_risk", risk="等待接口文档", next_step="约对方对齐", owner="张三")
|
||
assert u["content"] == "联调过半" and u["status"] == "at_risk" and u["task_id"] == t0["task_id"]
|
||
assert u["period_label"] or u["period_id"] == pid
|
||
up = h.admin.patch(f"/api/updates/{u['id']}", json={"progress": 80, "status": "in_progress"})
|
||
assert up.json()["progress"] == 80 and up.json()["status"] == "in_progress"
|
||
assert up.json()["content"] == "联调过半" # 没提交的字段保持原值
|
||
assert h.admin.patch(f"/api/updates/{u['id']}", json={"period_id": None}).status_code == 400
|
||
assert h.admin.patch("/api/updates/424242", json={"progress": 1}).status_code == 404
|
||
detail = h.admin.get(f"/api/projects/{p['id']}").json()
|
||
assert detail["tasks"] and detail["updates"]
|
||
assert h.admin.delete(f"/api/updates/{u['id']}").status_code == 200
|
||
for extra in (t0, t1): # 前两步为建子任务写的记录也清掉
|
||
assert h.admin.delete(f"/api/updates/{extra['id']}").status_code == 200
|
||
assert h.admin.get("/api/updates", params={"project_id": p["id"]}).json() == []
|
||
assert h.admin.delete(f"/api/updates/{u['id']}").status_code == 404
|
||
|
||
|
||
def test_删除项目会带走它的记录(h):
|
||
pid = h.today_period_id
|
||
p = h.project("用完就删")
|
||
h.update(p["id"], pid, content="临时记录")
|
||
assert h.admin.delete(f"/api/projects/{p['id']}").status_code == 200
|
||
assert h.admin.get("/api/updates", params={"project_id": p["id"]}).json() == []
|
||
|
||
|
||
def test_设置只认AI相关的键(h):
|
||
r = h.admin.put("/api/settings", json={"values": {"ai_model": "glm-4", "ai_api_key": "sk-x",
|
||
"ai_temperature": "0.5", "偷改的键": "1"}})
|
||
assert r.json() == {"ok": True}
|
||
s = h.admin.get("/api/settings").json()
|
||
assert s["ai_model"] == "glm-4" and s["ai_api_key"] == "sk-x" and s["ai_temperature"] == "0.5"
|
||
assert "偷改的键" not in s
|
||
assert h.admin.get("/api/meta").json()["ai"]["has_key"] is True # 配了 Key 就显示已配置
|
||
|
||
|
||
def test_分类PATCH是部分更新(h):
|
||
"""README 既定:PATCH 只改传了的字段。曾经复用创建模型导致 PATCH {} 直接 422。"""
|
||
c = h.category("交付项目", color="#111111")
|
||
up = h.admin.patch(f"/api/categories/{c['id']}", json={"color": "#222222"}).json()
|
||
assert up["color"] == "#222222" and up["name"] == c["name"] # 没传 name 不动
|
||
up = h.admin.patch(f"/api/categories/{c['id']}", json={"name": "改名后"}).json()
|
||
assert up["name"] == "改名后" and up["color"] == "#222222"
|
||
assert h.admin.patch(f"/api/categories/{c['id']}", json={"name": " "}).status_code == 400
|
||
assert h.admin.patch(f"/api/categories/{c['id']}", json={}).status_code == 200
|