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

101 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 -*-
"""决策 243「源」防回归:工料机来源追溯(api_lme_src/_lme_src)。
钉死四件事:
①会话侧直挂:数量=含量×定额量(_norm_qty 公式优先/存储量兜底),聚合到清单粒度;
②台班/配合比传导一层:数量=定额中台班量 × 台班里本码含量 × 定额量;
③合计=各行数量之和(前端浮层底部「总数量」取同一组行求和);
④批次库分支:bills 无 uw_name 列 → LEFT JOIN unit_works 取单位工程名称(列探测自动适配)。
"""
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
T_SESS = srv.TBL["session"]
T_BATCH = srv.TBL["batch"]
def _mk_session():
"""最小会话库:A4-1 清单(量 10)→ 甲定额(量 2,柴油 3/kg)→ 台班 TB1(0.5);
台班 TB1 子级含柴油 CY1(4)与机下人工 RG1(1);乙清单直挂 RG1 0.2。"""
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,
uw_name TEXT, Number TEXT, Name TEXT, Quantity REAL)""")
c.execute("""CREATE TABLE s_norms(id INTEGER PRIMARY KEY, bill_id INT,
Number TEXT, Name TEXT, Quantity REAL)""")
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.executemany("INSERT INTO s_bills VALUES(?,?,?,?,?,?,?)",
[(1, SID, 101, "2#楼", "A4-1", "浇筑清单", 10.0),
(2, SID, 102, "3#楼", "B7-2", "回填清单", 5.0)])
c.executemany("INSERT INTO s_norms VALUES(?,?,?,?,?)",
[(10, 1, "A4-1H", "浇筑定额", 2.0), # 定额量 2(无公式→存储量)
(11, 2, "B7-2H", "回填定额", 1.0)])
c.executemany("INSERT INTO s_norm_res VALUES(?,?,?,?)",
[(100, 10, "TB1", 0.5), # 甲定额用台班 0.5
(101, 11, "RG1", 0.2)]) # 乙清单直挂机下人工 0.2
c.executemany("INSERT INTO s_lme_res(session_id, p_code, res_code, qty, seq, name) VALUES(?,?,?,?,?,?)",
[(SID, "TB1", "CY1", 4.0, 1, "柴油"), (SID, "TB1", "RG1", 1.0, 2, "机下人工")])
sc.commit()
return sc
def test_session_direct_and_relay():
"""①②③:柴油=纯传导(0.5×4×定额量2=4.0,一行);机下人工=直挂+传导合并到清单粒度
(乙清单 0.2×1.0=0.2;甲清单 0.5×1×2=1.0 → 两行合计 1.2)。"""
con = _mk_session()
d = srv._lme_src(con, T_SESS, SID, "CY1")
assert d["ok"] and d["total"] == 1
r = d["rows"][0]
assert (r["uw_name"], r["bid"], r["bill"]) == ("2#楼", 1, "A4-1 浇筑清单")
assert abs(r["qty"] - 4.0) < 1e-6 # 0.5(台班)×4(柴油含量)×2(定额量)
d2 = srv._lme_src(con, T_SESS, SID, "RG1")
m = {x["bid"]: x["qty"] for x in d2["rows"]}
assert abs(m[1] - 1.0) < 1e-6 and abs(m[2] - 0.2) < 1e-6
assert abs(sum(m.values()) - 1.2) < 1e-6 # 合计=行之和(弹框「总数量」同源)
def test_batch_branch_join_uworks():
"""④批次库分支:bills 无 uw_name 列 → join unit_works.Name 取单位工程名称;数量口径同款。"""
sc = sqlite3.connect(":memory:")
sc.row_factory = sqlite3.Row
c = sc.cursor()
# 刻意**不建** bills.uw_name 列(镜像批次库 schema.sql 真实形态)
c.execute("""CREATE TABLE bills(id INTEGER PRIMARY KEY, batch_id INT, uw_id INT,
Number TEXT, Name TEXT, Quantity REAL)""")
c.execute("""CREATE TABLE unit_works(id INTEGER PRIMARY KEY, batch_id INT,
Number TEXT, Name TEXT)""")
c.execute("""CREATE TABLE norms(id INTEGER PRIMARY KEY, bill_id INT,
Number TEXT, Name TEXT, Quantity REAL)""")
c.execute("""CREATE TABLE norm_res(id INTEGER PRIMARY KEY, norm_id INT,
res_code TEXT, qty REAL)""")
c.execute("""CREATE TABLE lme_res(id INTEGER PRIMARY KEY, batch_id INT,
p_code TEXT, res_code TEXT, qty REAL, seq INTEGER, name TEXT)""")
c.executemany("INSERT INTO unit_works VALUES(?,?,?,?)",
[(101, 1, "UW1", "南屏-土建"), (102, 1, "UW2", "南屏-安装")])
c.executemany("INSERT INTO bills VALUES(?,?,?,?,?,?)",
[(1, 1, 101, "A4-1", "浇筑清单", 10.0),
(2, 1, 102, "B7-2", "回填清单", 5.0)])
c.executemany("INSERT INTO norms VALUES(?,?,?,?,?)",
[(10, 1, "A4-1H", "浇筑定额", 2.0), (11, 2, "B7-2H", "回填定额", 1.0)])
c.executemany("INSERT INTO norm_res VALUES(?,?,?,?)",
[(100, 10, "TB1", 0.5), (101, 11, "RG1", 0.2)])
c.executemany("INSERT INTO lme_res(batch_id, p_code, res_code, qty, seq, name) VALUES(?,?,?,?,?,?)",
[(1, "TB1", "CY1", 4.0, 1, "柴油")])
sc.commit()
d = srv._lme_src(sc, T_BATCH, 1, "CY1") # batch_id=1
assert d["ok"] and d["total"] == 1
r = d["rows"][0]
assert r["uw_name"] == "南屏-土建" # join unit_works 取名成功
assert abs(r["qty"] - 4.0) < 1e-6