unis-oa/backend/scripts/check_table_formula_editabl...

105 lines
4.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
"""明细列勾了「公式带入后允许修改」之后,后端这一侧跟着改了什么。
PYTHONPATH=. python3 scripts/check_table_formula_editable.py
用户那句原话:**"表单中明细表中引用公式带入的值,为什么不能编辑?我需要可编辑。"**
本行公式的数**只有前端会算**(``compute_formulas`` 只管 ``type=formula`` 那一格),所以后端
从来不重算明细列——这一趟动到后端的只有一件事:**别把人自己敲的那份凭据半路丢掉**。
前端改一格会在行上写一份 ``_manual``(列标识的数组,与关联带出那一份 ``_link`` 同一层),
下一轮重算靠它绕开那一格;它若被 :func:`normalise_rows` 当野键丢掉,下一环的人重新打开
这张单,昨天改的数就悄悄变回公式算的那个——改动了却留不住,比不让改更气人。
顺带钉住四条没动的:勾了这一档不算把列锁上(必填照卡)、公式列的默认值照旧不复活、
值本身照旧过格式那几关、这一档不是配置毛病(保存那一刻不多说一句)。
全程不碰库。
"""
from __future__ import annotations
from pathlib import Path
from app.service import formruntime
FAILS: list[str] = []
FRONTEND = Path(__file__).resolve().parents[2] / "frontend" / "src"
def check(label: str, got: object, want: object) -> None:
if got != want:
FAILS.append(f"{label}:想要 {want!r},拿到 {got!r}")
def table(columns: list[dict], **rest: object) -> dict:
return {"key": "items", "label": "费用明细", "type": "table", "columns": columns, **rest}
FORMULA = {"key": "pay", "label": "收款金额", "type": "money", "formula": "qty * price"}
EDITABLE = {**FORMULA, "formulaEditable": True}
PLAIN = {"key": "qty", "label": "数量", "type": "number"}
PRICE = {"key": "price", "label": "单价", "type": "number"}
# --------------------------------------------------------------- 那一份凭据
check("行标记与前端认的是同一个", formruntime.ROW_MANUAL_KEY, "_manual")
check(
"前端那份写的也是同一个键",
f"export const ROW_MANUAL_KEY = '{formruntime.ROW_MANUAL_KEY}'" in (FRONTEND / "utils" / "formRuntime.ts").read_text(encoding="utf-8"),
True,
)
rows = formruntime.normalise_rows(
[
{"qty": 2, "price": 30, "pay": 888, "_manual": ["pay"], "_link": "J1", "_wild": "丢掉"},
{"qty": "", "price": "", "pay": "", "_manual": ["pay"]},
],
table([PLAIN, PRICE, FORMULA]),
)
check("只留认识的列与两份留痕", sorted(rows[0].keys()), ["_link", "_manual", "pay", "price", "qty"])
check("人敲的那个数原样落库", rows[0]["pay"], 888)
check("凭据跟着走完全程", rows[0]["_manual"], ["pay"])
check("只带着凭据的空行不凭空造一行", len(rows), 1)
# ------------------------------------------------------- 这一档不算把列锁上
check("勾了这一档的公式列仍归人填", formruntime.column_not_for_people(EDITABLE), False)
check(
"必填照卡:那一格有框了,空着就该说",
formruntime._row_problems(table([dict(EDITABLE, required=True)]), [{"pay": ""}]),
["「费用明细」第 1 行的「收款金额」还没填写"],
)
check(
"值本身照判格式:手工敲歪了同样过不了那道闸",
formruntime._row_problems(table([{**EDITABLE, "max": 100}]), [{"pay": 999}]),
["「费用明细」第 1 行的「收款金额」大于配好的最大值 100"],
)
# ------------------------------------------------------- 默认值不跟着复活
check("公式列的默认值一律不落(勾没勾这一档都一样)", formruntime.column_default({**FORMULA, "defaultValue": 9}), None)
check("勾了这一档也一样", formruntime.column_default({**EDITABLE, "defaultValue": 9}), None)
# ----------------------------------------------------------- 设计期不报错
check(
"这一档不是配置毛病:保存那一刻不多说一句",
formruntime.validate_schema([table([PLAIN, PRICE, dict(EDITABLE, required=True)])]),
[],
)
def main() -> int:
for line in FAILS:
print(f"[FAIL] {line}")
if FAILS:
print(f"共 {len(FAILS)} 句不对")
return 1
print("通过:行上那份凭据一路不丢、这一档不锁列也不放宽校验、默认值没复活、设计期不报错(12 句)")
return 0
if __name__ == "__main__":
import sys
sys.exit(main())