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

84 lines
4.3 KiB
Python
Raw Permalink 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 -*-
"""类似清单组排序防回归(决策 36,2026-10-11)。
背景:组目录原排序 = 关键词组固定在前(规则序号)+ 系统组按差%降序(决策 19)。
主人定改为「按组合计合价降序,合价最大的编第 1 组」——本测试钉死四条口径:
① groups 严格按 tsum 降序(同额差%降序兜底);
② 关键词组不再固定在前,与系统组按同一口径混排(规则序号只定匹配优先级);
③ 落单组不参与排序,固定殿后(extra),条数守恒;
④ 无合价(Total 全空 → tsum=None)按 0 计,排在有合价的组之后。
"""
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
def _row(rid, name, price, total, attr=""):
return {"id": rid, "Number": "0%d" % rid, "Name": name, "Attr": attr,
"Unit": "m3", "Quantity": 1, "Price": price, "Total": total,
"x_seq": rid, "uw_name": ""}
def _names(res):
"""响应里 groups(含 extra)的每组:(名称集, tsum)。名称取自行内 Name,系统组无组名。"""
out = []
for g in res["groups"] + res["extra"]:
out.append((sorted(r[2] for r in g["rows"]), g.get("tsum")))
return out
def _patch_rules(monkeypatch, rules=None):
monkeypatch.setattr(srv, "_grp_rule", lambda: {"big": 50, "sig_thr": 0.6})
monkeypatch.setattr(srv, "_grp_rules_load", lambda: rules or [])
# 三个名称层组(组内同名),tsum 分别 50 / 500 / 100 → 目录序必须是 500、100、50
def test_tsum_desc(monkeypatch):
_patch_rules(monkeypatch)
rows = [_row(1, "混凝土柱浇筑", 100, 25), _row(2, "混凝土柱浇筑", 120, 25),
_row(3, "钢筋制作安装", 300, 400), _row(4, "钢筋制作安装", 320, 100),
_row(5, "模板支拆运输", 50, 60), _row(6, "模板支拆运输", 55, 40)]
res = srv._group_v2_output(rows)
ts = [t for _, t in _names(res)]
assert ts == sorted(ts, reverse=True) and ts[0] == 500.0, ts
assert res["groups"][0]["rows"][0][2] == "钢筋制作安装" # 合价最大的组编第 1 组
# 关键词组 tsum 居中 → 不再固定在前(原口径关键词组永远排最前)
def test_kw_group_mixed_by_tsum(monkeypatch):
_patch_rules(monkeypatch, [{"seq": 1, "name": "混凝土类", "keywords": "混凝土", "exclude": ""}])
rows = [_row(1, "混凝土柱浇筑", 100, 20), _row(2, "混凝土墙浇筑", 110, 30), # 命中关键词,tsum=50
_row(3, "钢筋制作安装", 300, 400), _row(4, "钢筋制作安装", 320, 100), # 名称层,tsum=500
_row(5, "模板支拆运输", 50, 18), _row(6, "模板支拆运输", 55, 17)] # 名称层,tsum=35
res = srv._group_v2_output(rows)
names = _names(res)
assert names[0][0] == ["钢筋制作安装", "钢筋制作安装"] # 第 1 组 = 合价最大
assert names[1][0] == sorted(["混凝土柱浇筑", "混凝土墙浇筑"]) # 关键词组按 tsum 居中
assert names[2][0] == ["模板支拆运输", "模板支拆运输"]
assert res["groups"][1].get("cat") == "混凝土类"
# 落单行进 extra 殿后,不参与排序;条数守恒
def test_extra_trailing_and_conservation(monkeypatch):
_patch_rules(monkeypatch)
rows = [_row(1, "混凝土柱浇筑", 100, 25), _row(2, "混凝土柱浇筑", 120, 25),
_row(3, "零星杂项拆除", 9, 3), _row(4, "场地平整碾压", 8, 2)] # 各自落单 → extra
res = srv._group_v2_output(rows)
assert len(res["groups"]) == 1 and len(res["extra"]) == 1
assert res["extra"][0]["cat"] == "其他"
assert sum(len(g["rows"]) for g in res["groups"] + res["extra"]) == len(rows)
# Total 全空 → tsum=None 按 0 计,排在有合价的组之后(不抛异常、不插队)
def test_null_tsum_sinks(monkeypatch):
_patch_rules(monkeypatch)
rows = [_row(1, "混凝土柱浇筑", 100, None), _row(2, "混凝土柱浇筑", 120, None),
_row(3, "钢筋制作安装", 300, 400), _row(4, "钢筋制作安装", 320, 100)]
res = srv._group_v2_output(rows)
assert res["groups"][0]["rows"][0][2] == "钢筋制作安装"
assert res["groups"][1]["tsum"] is None