unis-oa/backend/scripts/check_months_style.py

156 lines
7.2 KiB
Python
Raw Permalink 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_months_style.py
来由:那一格最初只有一种长相——12 个月的盘子摊在表单里,一整行没了。现在让它挑姿势
(下拉多选 / 弹出框多选 / 平铺点选),用的是设计器里已有的那份 ``pickerStyle``,
于是最容易坏的不是控件,是**保存那一路把它当残骸收掉**:
* ``api/design.py`` 的 ``clean_option_source`` 见「这一类没有候选来源」就连 ``pickerStyle``
一起抹(多选月份不在 ``source_kinds_of`` 的清单里)——配的人挑了弹出框,存一次默默掉回下拉;
* 反过来,换成文本框之后那一格还留着 ``pickerStyle``,看着是文本框,读出来却带着上一类的
展现配置,下一个人无从知道它为什么在那儿。
钉这些:三档都认、默认是下拉(平铺要吃掉一整行,不能当默认)、认不出的写法收掉并在
保存那一刻说一句、明细列同理、换成别的类型跟着收、值与念法照旧、交出去那一句照展示念且
有上限(顿号那一串会把对方那种定长列撑爆)。全程不碰库。
"""
from __future__ import annotations
import sys
from app.api.design import clean_option_source
from app.service import formruntime
FAILS: list[str] = []
#: 钉了几句自己数,别靠人改一句就回来改一次括号里的数字
TOTAL = 0
def check(label: str, got: object, want: object) -> None:
global TOTAL
TOTAL += 1
if got != want:
FAILS.append(f"{label}:想要 {want!r},拿到 {got!r}")
def fields_of(*items: dict) -> dict:
return {"fields": list(items)}
def cleaned(field: dict) -> dict:
return clean_option_source(fields_of(field))["fields"][0]
# ------------------------------------------------------------------ 认档与默认
check("默认那一档", formruntime.months_style_of({}), "dropdown")
check("弹出框认得", formruntime.months_style_of({"pickerStyle": "dialog"}), "dialog")
check("平铺认得", formruntime.months_style_of({"pickerStyle": "inline"}), "inline")
check("下拉认得", formruntime.months_style_of({"pickerStyle": "dropdown"}), "dropdown")
# 「自动」是那两格下拉组件的说法:多选月份只有一档默认,摆「自动」等于让人猜
check("auto 回落默认", formruntime.months_style_of({"pickerStyle": "auto"}), "dropdown")
check("瞎写的回落默认", formruntime.months_style_of({"pickerStyle": "popup"}), "dropdown")
check("脏空格照样认", formruntime.months_style_of({"pickerStyle": " dialog "}), "dialog")
# ------------------------------------------------------------------ 保存那一路
check(
"配了弹出框存得住",
cleaned({"key": "m", "label": "停用月份", "type": "months", "pickerStyle": "dialog"}).get("pickerStyle"),
"dialog",
)
check(
"配了平铺存得住",
cleaned({"key": "m", "label": "停用月份", "type": "months", "pickerStyle": "inline"}).get("pickerStyle"),
"inline",
)
check(
"认不出的写法收掉",
"pickerStyle" in cleaned({"key": "m", "label": "停用月份", "type": "months", "pickerStyle": "popup"}),
False,
)
check(
"多选月份不许挂候选来源",
"optionSource" in cleaned({"key": "m", "label": "停用月份", "type": "months", "optionSource": "dict"}),
False,
)
check(
"换成文本框之后那一档跟着收",
"pickerStyle" in cleaned({"key": "m", "label": "停用月份", "type": "text", "pickerStyle": "dialog"}),
False,
)
check(
"选择类的挑选方式照旧存得住(这次改动没把它们带坏)",
cleaned({"key": "s", "label": "类别", "type": "select", "pickerStyle": "inline", "options": [{"value": "a"}]}).get(
"pickerStyle"
),
"inline",
)
# ------------------------------------------------------------------ 保存那一刻的说法
bogus = {"key": "m", "label": "停用月份", "type": "months", "pickerStyle": "popup"}
problems = formruntime.validate_schema([bogus])
check("主表那一格配歪了会说", any("挑选方式不认识" in line for line in problems), True)
check(
"说的时候带上是哪一格",
any("「停用月份」" in line for line in problems if "挑选方式不认识" in line),
True,
)
check(
"配对了不多说一句",
formruntime.validate_schema([{"key": "m", "label": "停用月份", "type": "months", "pickerStyle": "dialog"}]),
[],
)
column_bogus = {
"key": "rows",
"label": "明细",
"type": "table",
"columns": [{"key": "m", "label": "停用月份", "type": "months", "pickerStyle": "popup"}],
}
column_problems = formruntime.validate_schema([column_bogus])
check("明细那一列同样会说", any("的列「停用月份」的挑选方式不认识" in line for line in column_problems), True)
# ------------------------------------------------------------------ 值与念法照旧
check("并段那句没被带坏", formruntime.months_text(["2026-11", "2026-10"]), "2026年10-11月")
check(
"跨年两头写全",
formruntime.months_text(["2026-12", "2027-01", "2027-02", "2027-05"]),
"2026年12月-2027年2月、2027年5月",
)
check("脏值丢掉、去重、按时间排", formruntime.month_list(["2026-11", "2026-11", "2026-13", "x"]), ["2026-11"])
value_problems = formruntime.value_problems(
[{"key": "m", "label": "停用月份", "type": "months", "required": True}], {"m": "editable"}, {"m": ["2026-10", "十月"]}
)
check("提交时那一项写法不对会挡", any("2026-01 这样写法的月份" in line for line in value_problems), True)
# ---------------------------------------------------------- 交出去那一句(照展示念、有上限)
check("单月那一趟交出去的也是人话", formruntime.months_wire_text(["2026-10"]), "2026年10月")
check("连着几个月并成一段再交",
formruntime.months_wire_text(["2026-07", "2026-08", "2026-09", "2026-10", "2026-11"]), "2026年7-11月")
# 顿号那一串是 117 个字符,对方那种二十来字节的列装不下;并段之后一年全挑上也才十个字
check("整年交出去只有十个字符",
len(formruntime.months_wire_text([f"2026-{m:02d}" for m in range(1, 13)])), 10)
check("认得出这一串是月份", formruntime.is_month_list(["2026-10", "2026-11"]), True)
check("掺一个不是月份的就不是", formruntime.is_month_list(["2026-10", "x"]), False)
check("空数组不是", formruntime.is_month_list([]), False)
check("裸的一串月份不当数组看(「月份」那一格照旧交 2026-10)", formruntime.is_month_list("2026-10"), False)
scattered = formruntime.months_wire_text(
[f"{year}-{m:02d}" for year in range(2026, 2032) for m in (1, 3, 5)])
check("断成十几段也有上限", len(scattered) <= formruntime.MONTHS_WIRE_MAX, True)
check("截的是整段,不留半截月份", scattered.endswith("月"), True)
def main() -> int:
for line in FAILS:
print(f"[FAIL] {line}")
if FAILS:
print(f"共 {len(FAILS)} 句不对")
return 1
print(f"通过:多选月份的挑选方式认档、存得住、配歪了当场说、交出去那一句照展示念({TOTAL} 句)")
return 0
if __name__ == "__main__":
sys.exit(main())