Files
pricing_review/CostReview/app/tests/test_adopt_side273.py

161 lines
8.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 -*-
"""决策 273 e2e 回归:采纳外部定额的单价行组——行与列按本项目同区模板复制,
Total 按 Name 匹配外部(一致=外部该行 Total,不一致留空);导出件同口径。"""
import io
import json
import os
import sys
import zipfile
import pytest
_HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(os.path.dirname(_HERE), "core"))
sys.path.insert(0, _HERE)
from api_harness import META, PERIOD_202607, get, make_cos4, post, server # noqa: F401,E402
from test_api_p1 import raw_post # noqa: E402
TPL_XML = ('<UnitPriceCalculationOfItem Name="人工费" QtyFormula="_RGF" Rate="100.00"'
' Total="100.00" Code="RGF" Remark="人工费"/>'
'<UnitPriceCalculationOfItem Name="材料费" QtyFormula="_CLF" Rate="100.00"'
' Total="200.00" Code="CLF" Remark="材料费"/>'
'<UnitPriceCalculationOfItem Name="子目单价" QtyFormula="1..3" Rate="100.00"'
' Total="350.00" Code="DJ" Remark=""/>')
EXT_XML = ('<UnitPriceCalculationOfItem Name="人工费" QtyFormula="_RGF" Rate="100.00"'
' Total="999.00" Code="RGF" Remark="人工费"/>'
'<UnitPriceCalculationOfItem Name="子目单价" QtyFormula="1..2" Rate="100.00"'
' Total="777.00" Code="DJ" Remark=""/>')
# 本项目模板行组(3 行)与外部行组(2 行:人工费 Total 不同值、无材料费 → 验留空)
TPL_SIDE = [
{"tag": "UnitPriceCalculationOfItem", "attrs": {"Name": "人工费", "QtyFormula": "_RGF",
"Rate": "100.00", "Total": "100.00", "Code": "RGF", "Remark": "人工费"}},
{"tag": "UnitPriceCalculationOfItem", "attrs": {"Name": "材料费", "QtyFormula": "_CLF",
"Rate": "100.00", "Total": "200.00", "Code": "CLF", "Remark": "材料费"}},
{"tag": "UnitPriceCalculationOfItem", "attrs": {"Name": "子目单价", "QtyFormula": "1..3",
"Rate": "100.00", "Total": "350.00", "Code": "DJ", "Remark": ""}},
]
EXT_SIDE = [
{"tag": "UnitPriceCalculationOfItem", "attrs": {"Name": "人工费", "QtyFormula": "_RGF",
"Rate": "100.00", "Total": "999.00", "Code": "RGF", "Remark": "人工费"}},
{"tag": "UnitPriceCalculationOfItem", "attrs": {"Name": "子目单价", "QtyFormula": "1..2",
"Rate": "100.00", "Total": "777.00", "Code": "DJ", "Remark": ""}},
]
def _import(server, fname, name, norm_extra=""):
"""造件+upload+audit 开会话(幂等:先删同名批次)。返回 (sid, db, sessdb)。"""
from api_harness import make_cos4
fix = server["root"] / "fixsrc"
fix.mkdir(exist_ok=True)
make_cos4(str(fix / fname), name,
[("030507008001", name + "清单项", "", "台", 3, 500)],
[("DTPL001", "模板定额", "", "m3", "10", "350")],
norm_extra=norm_extra)
import sqlite3 as _s
_con = _s.connect(server["db"])
try:
bids = [x[0] for x in _con.execute("SELECT id FROM batches WHERE name=?", (name,)).fetchall()]
finally:
_con.close()
for b in bids:
post(server, "/api/admin/batch/%d/delete" % b, {})
up = raw_post(server, "/api/admin/upload", fname, (fix / fname).read_bytes())
assert up["ok"] is True, up
r = post(server, "/api/audit", dict(META, path=up["path"], name=name, file_kind=1))
assert r.get("ok") is True, r
return r["session_id"]
def test_adopt_side_name_match(server): # noqa: F811
sid = _import(server, "决策273项目.cos4", "决策273项目", norm_extra=TPL_XML)
# 模板行组随造件导入自动入库(norm_side 通道 2026-11-03)——校验落到 s_norms.x_side
scon = __import__("sqlite3").connect("file:%s?mode=ro" % server["sessdb"], uri=True)
try:
bid = scon.execute("SELECT id FROM s_bills WHERE Name LIKE '%清单项%'").fetchone()[0]
xs = scon.execute("SELECT x_side FROM s_norms WHERE bill_id=? AND Number='N-1'",
(bid,)).fetchone()[0]
assert xs, "模板行组未入库"
finally:
scon.close()
# 外部件导入(批次;不再 audit 开会话——作为采纳源)
fix2 = server["root"] / "fixsrc"
make_cos4(str(fix2 / "决策273外部.cos4"), "决策273外部",
[("030507008002", "外部清单项", "", "台", 2, 400)],
[("DEXT001", "外部定额", "", "m3", "5", "777")])
make_cos4(str(fix2 / "决策273外部.cos4"), "决策273外部",
[("030507008002", "外部清单项", "", "台", 2, 400)],
[("DEXT001", "外部定额", "", "m3", "5", "777")],
norm_extra=EXT_XML)
up2 = raw_post(server, "/api/admin/upload", "决策273外部.cos4",
(fix2 / "决策273外部.cos4").read_bytes())
assert up2["ok"] is True, up2
r2 = post(server, "/api/admin/import",
dict(META, path=up2["path"], name="决策273外部", file_kind=1))
assert r2["ok"] is True, r2
con = __import__("sqlite3").connect(server["db"])
try:
src_bill = con.execute(
"SELECT id FROM bills WHERE batch_id=?", (r2["batch_id"],)).fetchone()[0]
finally:
con.close()
# 采纳:目标清单(本项目)← 外部源清单
post(server, "/api/session/bill/adopt",
{"session_id": sid, "bill_id": bid, "src_bill_id": src_bill})
# 断言:采纳落库的新定额行 x_side=模板行组+Total 按 Name 匹配
scon2 = __import__("sqlite3").connect("file:%s?mode=ro" % server["sessdb"], uri=True)
scon2.row_factory = __import__("sqlite3").Row
try:
row = scon2.execute(
"SELECT x_side FROM s_norms WHERE bill_id=? AND x_adopt=1", (bid,)).fetchone()
finally:
scon2.close()
assert row and row["x_side"], "采纳行应带行组"
side = json.loads(row["x_side"])
rows = [x["attrs"] for x in side if x.get("tag") == "UnitPriceCalculationOfItem"]
names = [x["Name"] for x in rows]
assert names == ["人工费", "材料费", "子目单价"], names # 行与列=本项目模板(3 行)
by = {x["Name"]: x["Total"] for x in rows}
assert by["人工费"] == "999.00", by # Name 一致 → 外部值
assert by["子目单价"] == "777.00", by # Name 一致 → 外部值
assert by["材料费"] == "", by # 外部无同名行 → 留空
# 导出前:从件1 源文件提炼骨架入库(骨架回灌引擎需要骨架在库)
import classic_skeleton as _cs
_up = str(next(iter(server["root"].rglob("*决策273项目.cos4")))) # 件1 副本(内容=原件)
_hc = __import__("sqlite3").connect(server["db"])
try:
_cs.seed_frame(_hc, _up, "骨架273")
finally:
_hc.close()
# 导出原格式:采纳定额块单价行同口径
resp = server["client"].get("/api/session/export",
params={"session_id": sid, "name": "决策273项目",
"province": "广东省", "city": "珠海市",
"period_from": PERIOD_202607})
assert resp.status_code == 200, resp.text[:200]
z = zipfile.ZipFile(io.BytesIO(resp.content))
import re as _re
got = []
for n in z.namelist():
if n.endswith(".xml") and n != "Project.xml":
txt = z.read(n).decode("utf-8", "replace")
for mm in _re.finditer(r'<Norm\b[^>]*Name="外部清单项安装".*?</Norm>', txt, _re.S):
blk = mm.group(0)
got += [dict(_re.findall(r'(\w+)="([^"]*)"', x.group(1)))
for x in _re.finditer(r"<UnitPriceCalculationOfItem\b([^>]*?)/>", blk)]
if not got:
import sys as _sys
for n2 in z.namelist():
if n2.endswith(".xml"):
txt2 = z.read(n2).decode("utf-8", "replace")
print("=== 条目", n2, "len", len(txt2), file=_sys.stderr)
print(txt2[:1600], file=_sys.stderr)
assert got, "导出件应有采纳定额单价行"
byx = {x["Name"]: x["Total"] for x in got}
assert byx.get("人工费") == "999.00", byx
assert byx.get("子目单价") == "777.00", byx
assert byx.get("材料费") == "", byx # 留空
post(server, "/api/session/delete", {"ids": [sid]})