Files
pricing_review/CostReview/app/tests/test_esc_import.py
lianjianwei2024 e0e09a893b feat(import): 决策265 解析器支持 ESCC2.0 广东省造价数据标准(明文 XML)
- sniff 加 esc 分支(头部 <Projects);parse_file 分流;parse_esc 照 parse_swt 同款 iterparse+out 容器(约 200 行)
- 覆盖:分部/清单(措施 Kind=prelim)/定额 WorkContent 全收/定额消耗/工程量表达式/项目+单位级 Resource(决策122A 价口径)/uw_summary/项目特征;重名单位工程 Number 自动去重
- s_lme_proj 99、s_lme_uw 391 = 决策201「未引用即删」正确行为,s_lme_lib 126 全量兜底(测试按此断言)
- 推翻 2026-10-01「不再支持 xml」ESCC 部分;其他散装 XML 仍拒(test_api_p0 改无名根样料)
- test_esc_import.py 3 项留库;369 passed / 3 skipped;IAB 全链路实测(样本 y程ESCC2.xml)
2026-11-04 07:16:53 +08:00

107 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 -*-
"""ESCC2.0(广东省造价数据标准 DBJ/T 15-145-2018)明文 XML 解析回归。
2026-11-03 主人定:恢复散装 xml 中 ESCC 支持(推翻 2026-10-01「不再支持 xml」的 ESCC 部分)。
样本:后台需要导入的项目/y程ESCC2.xml(珠海高新区 2024 地灾治理,7 单位工程,变更工程负量)。"""
import os
import sys
import pytest
from api_harness import post, server # noqa: F401 (夹具)
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "core"))
import import_job as ij # noqa: E402
SAMPLE = os.path.join(os.path.dirname(__file__), "..", "..", "..",
"后台需要导入的项目", "y程ESCC2.xml")
def test_parse_esc_dry():
"""干跑:容器行数与关键值与样本实测一致。"""
out = {"uw": [], "bills": [], "norms": [], "lmes": []}
kind = ij.parse_esc(SAMPLE, out)
assert kind == "ESCC2"
assert out["proj_name"] == "珠海市高新区2024年地质灾害隐患点、风险点治理工程"
assert out["total"] == pytest.approx(-238931.60, abs=0.01)
assert len(out["uw"]) == 7 # 2 单项组(4+3)单位工程
assert len(out["sections"]) == 2
assert len(out["bills"]) == 186 # 分部分项 65 + 措施 121
assert sum(1 for k in out["bill_kind"] if k == "prelim") == 121
assert len(out["norms"]) == 127 # WorkContent 组内定额全收
assert len(out["norm_res"]) == 698 # 定额消耗
assert len(out["lme_proj"]) == 126 # 项目级 Resource 价格表
assert len(out["lme_uw"]) == 512 # 单位级 Resource 全收(96+72+40 ×2 +96)
assert len(out["uw_cost"]) == 7
assert len(out["uw_summary"]) == 7 # 单位级 Summary 树(样本无项目级)
assert len(out["bill_expr"]) == 186 # 清单工程量表达式
# 负数量/负合价原样保真(变更工程特性)
b0 = out["bills"][0]
assert b0[0] == "010102001004" and b0[1] == "孤石清理"
assert b0[4] == pytest.approx(-170.0) and b0[6] == pytest.approx(-9661.10)
# 措施分部嵌套(样本 13 层 Branch/Branch)→ parent 链正确
md = [d for d in out["divides"] if d["kind"] == "prelim"]
assert len(md) == 105
by_idx = {i: d for i, d in enumerate(out["divides"])}
for d in md:
if d["parent"] is not None:
assert by_idx[d["parent"]]["kind"] == "prelim"
def test_parse_file_dispatch_esc():
"""sniff/parse_file 分流:根节点 Projects → parse_esc;其他散装 XML 仍拒绝。"""
assert ij.sniff(SAMPLE) == "esc"
out = {"uw": [], "bills": [], "norms": [], "lmes": []}
assert ij.parse_file(SAMPLE, out) == "ESCC2"
# 未知散装 XML 仍报错
import tempfile
p = os.path.join(tempfile.gettempdir(), "not_priced.xml")
with open(p, "w", encoding="utf-8") as fh:
fh.write('<?xml version="1.0"?><Root><Thing/></Root>')
try:
with pytest.raises(ValueError):
ij.parse_file(p, {"uw": [], "bills": [], "norms": [], "lmes": []})
finally:
os.unlink(p)
def test_esc_open_via_audit(server):
"""端到端:走 /api/audit 真实入库(批次库),断言库内行数与树结构。"""
p = server["src_dir"] / "esc_t.xml"
p.write_bytes(open(SAMPLE, "rb").read())
j = post(server, "/api/audit", {
"path": str(p), "name": "ESCC回归工程",
"period_from": 24319, "period_to": 24319,
"province": "广东省", "city": "珠海市", "district": "香洲区",
"bulid_unit": "测试建设单位", "file_kind": 1})
assert j.get("ok") is True, j
sid = j["session_id"]
import sqlite3
con = sqlite3.connect(str(next(server["root"].rglob("session.db"))))
con.row_factory = sqlite3.Row
try:
uw = con.execute("SELECT COUNT(*) c FROM s_unit_works WHERE session_id=?", (sid,)).fetchone()["c"]
assert uw == 7, uw
sec = con.execute("SELECT COUNT(*) c FROM s_sections WHERE session_id=?", (sid,)).fetchone()["c"]
assert sec == 2, sec
nb = con.execute("""SELECT COUNT(*) c FROM s_bills b JOIN s_unit_works u ON u.id=b.uw_id
WHERE u.session_id=?""", (sid,)).fetchone()["c"]
assert nb == 186, nb
nn = con.execute("""SELECT COUNT(*) c FROM s_norms n JOIN s_bills b ON b.id=n.bill_id
JOIN s_unit_works u ON u.id=b.uw_id WHERE u.session_id=?""", (sid,)).fetchone()["c"]
assert nn == 127, nn
# 项目级价格表 126 条入库后,决策 201 打开收尾「未引用即删」会清掉
# 只在价格表出现、无任何定额/台班组成引用的行(样本 27 条费用科目行)——与其他格式同口径
lp = con.execute("SELECT COUNT(*) c FROM s_lme_proj WHERE session_id=?", (sid,)).fetchone()["c"]
assert lp == 99, lp
lib = con.execute("SELECT COUNT(*) c FROM s_lme_lib WHERE session_id=?", (sid,)).fetchone()["c"]
assert lib == 126, lib # s_lme_lib 是全量兜底库,不删
us = con.execute("""SELECT COUNT(*) c FROM s_uw_summary s JOIN s_unit_works u ON u.id=s.uw_id
WHERE u.session_id=?""", (sid,)).fetchone()["c"]
assert us > 0, "单位汇总分析没入库"
# 负数量保真
neg = con.execute("""SELECT b.Quantity q FROM s_bills b JOIN s_unit_works u ON u.id=b.uw_id
WHERE u.session_id=? AND b.Number='010102001004'""", (sid,)).fetchone()
assert neg and neg["q"] == pytest.approx(-170.0)
finally:
con.close()