73 lines
2.6 KiB
Python
73 lines
2.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""platform_skeleton 离线单测:升级包内置骨架的加载/校验/摘要(无网络逻辑)。"""
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "app"))
|
|
|
|
from core import platform_skeleton as ps # noqa: E402
|
|
|
|
|
|
def _good_data():
|
|
return {
|
|
"suffix": {"name": "cos4", "group": "广东省"},
|
|
"version": "v1",
|
|
"dirs": [
|
|
{"id": 1, "parent_id": None, "name": "建设项目", "sort": 0, "anchor": "ConstructionProject", "columns": []},
|
|
{"id": 2, "parent_id": 1, "name": "工程信息(起)", "sort": 1, "anchor": "ConstructionInfo",
|
|
"columns": [{"id": 9, "name": "费用精度", "is_value": False, "sort": 0, "parent_id": None, "value": "Option"}]},
|
|
{"id": 3, "parent_id": 1, "name": "工程信息(闭)", "sort": 2, "anchor": "/ConstructionInfo", "columns": []},
|
|
],
|
|
"files": [],
|
|
}
|
|
|
|
|
|
def _write_bundle(tmp_path, data, suffix="cos4"):
|
|
(tmp_path / "skeletons").mkdir(exist_ok=True)
|
|
(tmp_path / "skeletons" / f"{suffix}.json").write_text(
|
|
json.dumps({"data": data}, ensure_ascii=False), encoding="utf8")
|
|
|
|
|
|
def test_load_ok_and_summary(tmp_path):
|
|
_write_bundle(tmp_path, _good_data())
|
|
s = ps.summary(ps.load("cos4", skeleton_dir=str(tmp_path / "skeletons")))
|
|
assert s["dirs"] == 3 and s["anchored"] == 3 and s["close_points"] == 1
|
|
assert s["rows"] == 1 and s["filled_cells"] == 1 and s["roots"] == ["建设项目"]
|
|
|
|
|
|
def test_validate_rejects_broken(tmp_path):
|
|
bad = _good_data()
|
|
bad["dirs"][1]["parent_id"] = 999 # 悬空父级
|
|
_write_bundle(tmp_path, bad)
|
|
try:
|
|
ps.load("cos4", skeleton_dir=str(tmp_path / "skeletons"))
|
|
raise AssertionError("应拒绝悬空 parent_id")
|
|
except ps.SkeletonError:
|
|
pass
|
|
|
|
|
|
def test_load_missing_file_raises(tmp_path):
|
|
try:
|
|
ps.load("cos4", skeleton_dir=str(tmp_path)) # 无 skeletons 目录
|
|
raise AssertionError("应抛 SkeletonError")
|
|
except ps.SkeletonError as e:
|
|
assert "升级包" in str(e)
|
|
|
|
|
|
def test_load_broken_json_raises(tmp_path):
|
|
d = tmp_path / "skeletons"
|
|
d.mkdir(exist_ok=True)
|
|
(d / "cos4.json").write_bytes(b"\x88corrupt")
|
|
try:
|
|
ps.load("cos4", skeleton_dir=str(d))
|
|
raise AssertionError("应抛 SkeletonError")
|
|
except ps.SkeletonError as e:
|
|
assert "损坏" in str(e)
|
|
|
|
|
|
def test_skeleton_path_sanitizes_suffix(tmp_path):
|
|
p = ps.skeleton_path("../evil", skeleton_dir=str(tmp_path))
|
|
base = os.path.basename(p)
|
|
assert "/" not in base and "\\" not in base and base not in (".", "..") # 无路径穿越
|