Files
pricing_review/CostReview/app/tests/test_yhy_import.py
lianjianwei2024 78e58a40c7 feat(export): 决策263-266 单一骨架导出通路/数据全量入库/空骨架规则标注/导入按内容判族
- 决策 263:定额单价专题(三同合并全字段统一、原价三级链、LME Summary 26 属性收口、x_flags 采集、e2e 回归)
- 决策 264 补记五:原格式导出只保留骨架回灌一条通路;删除 export_cos.export_zip 旧引擎;新增 skeleton_engine(内容判族/唯一结构位/结构旁注/路径指纹);单位级/项目级非数据层 x_attr/x_side 采集与回灌;导出件成员名改 GBK 原始字节(_GbkZipInfo);后台「导出设置」导出空骨架(标注)
- 决策 265:导入按内容判族(zip 内含 ConstructProject→SWT 族,否则 COS 族;裸 XML 同),扩展名不参与拦截;api_scan 取消 EXT_OK 过滤
- 决策 266:结构位统一「自增区域/自增块/自增行」,取消「固定」;min_count(区域1/块行0),导出条数=max;区域数据0条保底1空壳;LME 汇总两形态(带组成闭合块/普通自闭合行)按行硬分派
- 前序未提交:HTML 导出整体移除(export_html.py 删除)、Excel 导出加序号列并移除说明页、.yhy 导入(import_yhy.py)
- 测试:367 passed / 3 skipped;新增 test_topic_price/test_yhy_import/test_import_suffix 等
2026-11-03 23:39:46 +08:00

88 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""决策 261 补三(2026-11-03 主人定):.yhy 导入(id 整体平移法)回归——
导出→导入→逐表对拍:行数一致、数据列内容一致(id/引用列按 offset 平移)、id 不撞库、同名拒绝。"""
import io
import json
import os
import sqlite3
import sys
import zipfile
from api_harness import post, server # noqa: F401 (夹具)
from test_manual_export import _create_manual
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "core"))
import import_yhy # noqa: E402
_TABS = ["s_sections", "s_unit_works", "s_divides", "s_bills", "s_lme_lib",
"s_lme_proj", "s_lme_res", "s_lme_uw", "s_lmes", "s_proj_summary",
"s_sundry", "s_uw_summary"]
_REF_COLS = {t: (set(refs) - {"session_id"}) for t, refs in import_yhy._TABLES}
def test_yhy_roundtrip(server):
sid1 = _create_manual(server)
c = server["client"]
# ① 导出 .yhy
r = c.get("/api/session/export", params={"session_id": sid1, "fmt": "yhy"})
assert r.status_code == 200
ybytes = r.content
assert zipfile.ZipFile(io.BytesIO(ybytes)).read("manifest.json")
# ② 写入待导入目录并导入(换名)
ypath = server["src_dir"] / "roundtrip_t.yhy"
ypath.write_bytes(ybytes)
j = post(server, "/api/session/yhy/import", {"path": str(ypath), "name": "回导测试工程"})
assert j.get("ok") is True, j
sid2 = j["session_id"]
assert sid2 != sid1 and j["name"] == "回导测试工程"
# ③ 同名拒绝(决策 242)
r2 = c.post("/api/session/yhy/import", json={"path": str(ypath), "name": "回导测试工程"})
j2 = r2.json()
assert (not j2.get("ok")) and ("同名" in j2.get("msg", "")), j2
# ④ 逐表对拍:导出件行 vs 库内新会话行
z = zipfile.ZipFile(io.BytesIO(ybytes))
con = sqlite3.connect(str(next(server["root"].rglob("session.db"))))
con.row_factory = sqlite3.Row
try:
for t in _TABS:
src = json.loads(z.read("rows/%s.json" % t))
if not src:
continue
off = None
newrows = [dict(x) for x in con.execute(
"SELECT * FROM %s WHERE session_id=?" % t, (sid2,))]
assert len(newrows) == len(src), "%s 行数不一致 %d != %d" % (t, len(newrows), len(src))
# offset=新 id −旧 id,须全表恒定且不与旧会话撞
old_ids = sorted(x["id"] for x in src)
new_ids = sorted(x["id"] for x in newrows)
off = new_ids[0] - old_ids[0]
assert off > 0 or old_ids[0] == 1, t
for x, y in zip(sorted(src, key=lambda r: r["id"]), sorted(newrows, key=lambda r: r["id"])):
assert y["id"] == x["id"] + off, "%s id 平移不恒定" % t
# 数据列(非 id/session_id/引用列)逐列一致;引用列平移正确性由下面⑤子表抽验覆盖
for col, v in x.items():
if col in ("id", "session_id") or col in _REF_COLS.get(t, ()):
continue
assert y.get(col) == v, "%s.%s 不一致:%r != %r" % (t, col, y.get(col), v)
# ⑤ 子表引用链抽验:源有定额时,s_norms.bill_id 必须落在 sid2 的清单 id 集内
src_norms = json.loads(z.read("rows/s_norms.json"))
bids = {x["id"] for x in con.execute("SELECT id FROM s_bills WHERE session_id=?", (sid2,))}
if src_norms:
nids = [x[0] for x in con.execute(
"SELECT n.id FROM s_norms n WHERE n.bill_id IN (SELECT id FROM s_bills WHERE session_id=?)",
(sid2,))]
assert len(nids) == len(src_norms), "定额没平移全 %d != %d" % (len(nids), len(src_norms))
assert bids, "清单没平移过来"
finally:
con.close()
def test_yhy_import_bad_file(server):
"""非 yhy 包 / 坏 zip → 明确报错不落库。"""
p = server["src_dir"] / "bad.yhy"
p.write_bytes(b"not a zip")
r = c = server["client"].post("/api/session/yhy/import",
json={"path": str(p), "name": "坏包测试"})
j = r.json()
assert not j.get("ok"), j