Files
pricing_review/CostReview/app/tests/test_lme_prune.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

93 lines
5.5 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 -*-
"""决策 244(2026-11-01 主人定)防回归:工料机剪枝 + 合并弹框只看现算。
主人口径:「在导入或者打开项目,工料机引用的才入数据库」「合并工料机,只关注现算」。
钉死四件事:
①剪枝:未被引用(无定额直挂、无子级引用)的码从两级汇总移除;被引用的保留;
②合并隐藏行(x_merged_hide=1)保留——映射语义载体;
③子级引用算引用:柴油被台班引用(定额只挂台班)→ 柴油保留;
④弹框只看现算:现算 0 量的码不成组(dyn_rows 链全时);链不全退回老行为不炸。
"""
import sqlite3
import sys, os
_HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(os.path.dirname(_HERE), "core"))
sys.path.insert(0, os.path.join(os.path.dirname(_HERE), "server"))
import api_server as srv # noqa: E402
SID = 1
def _mk():
"""最小会话库:
定额 N1(清单 B1,量 10)直挂 DIESL(2.0);台班 TB 被定额挂(0.5),TB 子级引用 OIL(4);
GHOST 无任何引用(应剪);HIDDEN 未引用但 x_merged_hide=1(应保留);FREE 子级引用但母行无定额(应保留——保守剪枝边界外)。"""
sc = sqlite3.connect(":memory:")
sc.row_factory = sqlite3.Row
c = sc.cursor()
c.execute("""CREATE TABLE s_bills(id INTEGER PRIMARY KEY, session_id INT, uw_id INT, x_tdel INTEGER DEFAULT 0)""")
c.execute("""CREATE TABLE s_norms(id INTEGER PRIMARY KEY, bill_id INT, x_tdel INTEGER DEFAULT 0)""")
c.execute("""CREATE TABLE s_norm_res(id INTEGER PRIMARY KEY, norm_id INT, res_code TEXT, qty REAL)""")
c.execute("""CREATE TABLE s_lme_res(id INTEGER PRIMARY KEY, session_id INT,
p_code TEXT, res_code TEXT, qty REAL, seq INTEGER, name TEXT)""")
c.execute("""CREATE TABLE s_lme_proj(id INTEGER PRIMARY KEY, session_id INT,
code TEXT, name TEXT, spec TEXT, unit TEXT, kind INT, qty REAL,
ntp REAL, taxrate REAL, taxp REAL, x_adopt_added INTEGER DEFAULT 0,
x_merged_hide INTEGER DEFAULT 0, x_remark TEXT,
ntp_org REAL, ntp_expr REAL, taxp_org REAL, taxp_expr REAL, x_org_ntp0 REAL, x_org_taxp0 REAL, x_flags TEXT)""")
c.execute("""CREATE TABLE s_lme_uw(id INTEGER PRIMARY KEY, session_id INT, uw_id INT,
code TEXT, name TEXT, spec TEXT, unit TEXT, kind INT, qty REAL,
ntp REAL, taxrate REAL, taxp REAL, x_adopt_added INTEGER DEFAULT 0,
ntp_org REAL, ntp_expr REAL, taxp_org REAL, taxp_expr REAL, x_flags TEXT)""")
c.execute("""CREATE TABLE s_settings(scope TEXT NOT NULL, k TEXT NOT NULL, v TEXT,
PRIMARY KEY(scope,k))""")
c.executemany("INSERT INTO s_bills VALUES(?,?,?,0)", [(1, SID, 101), (2, SID, 102)])
c.executemany("INSERT INTO s_norms VALUES(?,?,0)", [(10, 1), (11, 2)])
c.executemany("INSERT INTO s_norm_res VALUES(?,?,?,?)",
[(100, 10, "DIESEL", 2.0), (101, 10, "TB", 0.5),
(102, 11, "TEMP", 1.0)]) # 定额 11 挂临删清单(x_tdel=1)
c.execute("UPDATE s_bills SET x_tdel=1 WHERE id=2") # 清单 2 临时删除(行还在可恢复)
c.executemany("INSERT INTO s_lme_res(session_id, p_code, res_code, qty, seq, name) VALUES(?,?,?,?,?,?)",
[(SID, "TB", "OIL", 4.0, 1, "油")])
# 两级汇总:DIESEL/TB/OIL 被引用;GHOST/HIDDEN 未引用
rows = [("DIESEL", 20.0, 0), ("TB", 5.0, 0), ("OIL", 2.0, 0),
("TEMP", 8.0, 0), ("GHOST", 999.0, 0), ("HIDDEN", 50.0, 1)]
c.executemany("""INSERT INTO s_lme_proj(session_id, code, name, spec, unit, kind, qty, ntp, taxrate, taxp, x_merged_hide)
VALUES(?,?,?,'','',2,?,?,0,?,?)""",
[(SID, cd, cd, q, 1.0, 1.0, hid) for cd, q, hid in rows])
c.executemany("""INSERT INTO s_lme_uw(session_id, uw_id, code, name, spec, unit, kind, qty, ntp, taxrate, taxp)
VALUES(?,101,?,?,?,'',2,?,?,0,?)""",
[(SID, cd, cd, '', q, 1.0, 1.0) for cd, q, hid in rows])
sc.commit()
return sc
def test_prune_keeps_referenced_deletes_ghost_keeps_hidden():
"""①②③:GHOST(零引用)删除;HIDDEN(未引用但隐藏)保留;OIL(仅子级引用)保留。"""
con = _mk()
n = srv._prune_unreferenced_lme(con, SID)
assert n > 0
codes = {r[0] for r in con.execute("SELECT code FROM s_lme_proj WHERE session_id=?", (SID,))}
assert codes == {"DIESEL", "TB", "OIL", "TEMP"} # GHOST/HIDDEN 剪(合并隐藏行不豁免);TEMP 临删引用保留
codes_uw = {r[0] for r in con.execute("SELECT code FROM s_lme_uw WHERE session_id=?", (SID,))}
assert "GHOST" not in codes_uw and "HIDDEN" not in codes_uw
assert {"DIESEL", "TB", "OIL", "TEMP"} <= codes_uw
# 幂等:再跑一次不炸不多删
n2 = srv._prune_unreferenced_lme(con, SID)
assert codes == {r[0] for r in con.execute("SELECT code FROM s_lme_proj WHERE session_id=?", (SID,))}
def test_prune_empty_ref_set_noop():
"""引用集为空(异常会话)跳过不删——绝不能误删一切。"""
con = _mk()
con.execute("DELETE FROM s_norm_res")
con.execute("DELETE FROM s_lme_res")
con.commit()
before = con.execute("SELECT COUNT(*) FROM s_lme_proj").fetchone()[0]
n = srv._prune_unreferenced_lme(con, SID)
assert n == 0
after = con.execute("SELECT COUNT(*) FROM s_lme_proj").fetchone()[0]
assert before == after