Files
inquiry_robot/inquiry-agent/tests/test_air_chargeable.py
T
linmianxun 9d7bc2b787 员工岗位与数据范围权限上线,企微群机器人接入与报价回执去重
- 新增员工岗位(InquiryStaffRole)实体、服务与后台页面,数据范围默认按岗位,看板由「仅本人」切换为岗位范围,补充业务负责人与角色说明迁移
- 新增企微群 webhook 通道、access_gate/staff_roles 鉴权与智价通/双 BOT 回复配置
- 报价回执增加去重防抖(quote_receipt_debounce),询价原文落库并补单测
- 同义词补陆运类型修正与地名通配 v2,更新种子生成脚本
- 空运/陆运/海运询价流程、报价单按需生成、转人工与多段处理调整
- 后台工单需求字段、BOT 显示名、站点标题与看板配套更新
2026-12-04 20:16:28 +08:00

262 lines
11 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.
"""
空运计费重单测:体积重 = 体积(CBM) × 166.7,计费重 = max(毛重, 体积重)。
覆盖场景(产品口径 2026-11-22):
- TMS 没有报价、航线按 KG 手工报价时,由工单体积/毛重算出计费重;
- 计费重进报价对象、KG 费用行按「单价×计费重」落金额、合计跟着重算;
- 群里和报价单都看得到同一个数;
- 读不到体积也读不到毛重时绝不编数字;
- 海运/陆运不套空运体积重。
线程:纯函数单测,不碰网络与 DB。
"""
from __future__ import annotations
import unittest
from agent.schema.air_chargeable import (
apply_to_quote,
compute_chargeable,
format_kg,
harvest_declared_chargeable,
parse_quantity,
volume_weight_kg,
)
class ParseQuantityTest(unittest.TestCase):
"""数字解析只去单位,不做语义猜测。"""
def test_strips_weight_and_volume_units(self) -> None:
self.assertEqual(parse_quantity("1,200KG"), 1200.0)
self.assertEqual(parse_quantity("500KGS"), 500.0)
self.assertEqual(parse_quantity("3CBM"), 3.0)
self.assertEqual(parse_quantity("2.5 立方米"), 2.5)
def test_ton_is_projected_to_kg(self) -> None:
# 1.5 吨 = 1500KG,确定性单位投影,不是猜货型。
self.assertEqual(parse_quantity("1.5吨"), 1500.0)
def test_hedge_words_do_not_break_ton(self) -> None:
# 「大概4吨」「4吨左右」都要按吨换算,不能读成 4。
self.assertEqual(parse_quantity("大概4吨"), 4000.0)
self.assertEqual(parse_quantity("4吨左右"), 4000.0)
def test_currency_prefix_is_not_a_quantity(self) -> None:
self.assertEqual(parse_quantity("USD 11.00"), 11.0)
def test_blank_and_garbage_return_none(self) -> None:
for raw in (None, "", "-", "—", "左右", "电放"):
self.assertIsNone(parse_quantity(raw), raw)
class ChargeableFormulaTest(unittest.TestCase):
"""体积重与取大规则。"""
def test_volume_weight_uses_166_7(self) -> None:
self.assertAlmostEqual(volume_weight_kg(1.0) or 0, 166.7, places=4)
self.assertAlmostEqual(volume_weight_kg(3.0) or 0, 500.1, places=4)
def test_volume_heavier_than_gross(self) -> None:
out = compute_chargeable({"体积": "3", "毛重": "500"})
self.assertIsNotNone(out)
assert out is not None
self.assertAlmostEqual(out["chargeable_kg"], 500.1, places=4)
self.assertEqual(out["basis"], "volume")
self.assertEqual(format_kg(out["chargeable_kg"]), "500.1")
def test_gross_heavier_than_volume(self) -> None:
out = compute_chargeable({"体积": "1", "毛重": "500"})
assert out is not None
self.assertAlmostEqual(out["chargeable_kg"], 500.0, places=4)
self.assertEqual(out["basis"], "gross")
def test_only_one_side_still_works(self) -> None:
only_volume = compute_chargeable({"体积": "2"})
assert only_volume is not None
self.assertAlmostEqual(only_volume["chargeable_kg"], 333.4, places=4)
only_weight = compute_chargeable({"毛重": "800"})
assert only_weight is not None
self.assertAlmostEqual(only_weight["chargeable_kg"], 800.0, places=4)
def test_explicit_fact_wins(self) -> None:
out = compute_chargeable({"计费重": "800", "体积": "10", "毛重": "100"})
assert out is not None
self.assertAlmostEqual(out["chargeable_kg"], 800.0, places=4)
self.assertEqual(out["basis"], "explicit")
def test_no_numbers_returns_none(self) -> None:
self.assertIsNone(compute_chargeable({}))
self.assertIsNone(compute_chargeable({"体积": "-", "毛重": "待定"}))
class DeclaredChargeableTextTest(unittest.TestCase):
"""航线在群里明说计费重:以他说的为准,且不能被抽成费用行。"""
def test_declared_number_is_harvested(self) -> None:
self.assertEqual(harvest_declared_chargeable("计费重800KG,USD11/kg"), 800.0)
self.assertEqual(harvest_declared_chargeable("计费重量:1,200.5 公斤"), 1200.5)
self.assertIsNone(harvest_declared_chargeable("计费重按实重"))
def test_declared_chargeable_is_not_a_fee_row(self) -> None:
from agent.llm.extract_text import extract_product_quote
quote = extract_product_quote(
"计费重800KG,空运费USD11/kg", business_line="AIR", transport_mode="空运"
)
names = [str(row.get("name") or "") for row in quote.get("fee_rows") or []]
self.assertIn("空运费", names)
self.assertNotIn("计费", names)
class ApplyToQuoteTest(unittest.TestCase):
"""报价对象补齐:计费重 + 行金额 + 合计。"""
def _manual_quote(self) -> dict:
# 与 extract_product_quote("USD11/kg") 的产出同形:只有单价,没有数量。
return {
"total": "11",
"currency": "USD",
"fee_rows": [
{"name": "空运费", "amount": "11", "currency": "USD", "unit": "KG", "unit_price": "11"}
],
"fee_lines": [
{"name": "空运费", "amount": "11", "currency": "USD", "unit": "KG", "unit_price": "11"}
],
}
def test_kg_row_becomes_unit_price_times_chargeable(self) -> None:
out = apply_to_quote(self._manual_quote(), {"体积": "3", "毛重": "500"})
self.assertEqual(str(out.get("chargeableWeightKg")), "500.1")
self.assertEqual(str(out.get("volumeWeightKg")), "500.1")
row = dict((out.get("fee_rows") or [])[0])
self.assertEqual(row.get("quantity"), "500.1")
self.assertAlmostEqual(float(row.get("amount")), 5501.1, places=4)
self.assertEqual(str(out.get("total")), "5501.1")
# 币种不能被计费重改动。
self.assertEqual(str(out.get("currency")), "USD")
def test_total_only_sums_same_currency(self) -> None:
quote = self._manual_quote()
quote["fee_rows"].append({"name": "报关费", "amount": "400", "currency": "CNY"})
quote["fee_lines"] = list(quote["fee_rows"])
out = apply_to_quote(quote, {"毛重": "500"})
# 合计只加第一行币种(USD),CNY 报关费留在明细里不折算。
self.assertEqual(str(out.get("total")), "5500")
def test_existing_chargeable_is_not_overwritten(self) -> None:
quote = self._manual_quote()
quote["chargeableWeightKg"] = "800"
out = apply_to_quote(quote, {"体积": "10", "毛重": "100"})
self.assertEqual(str(out.get("chargeableWeightKg")), "800")
self.assertIn((out.get("fee_rows") or [])[0].get("quantity"), (None, ""))
def test_airline_total_without_qty_is_kept(self) -> None:
# 「空运费 USD8800,单价 11/kg」:航线给了整票金额,不能按体积重覆盖。
quote = {
"total": "8800",
"currency": "USD",
"fee_rows": [
{"name": "空运费", "amount": "8800", "currency": "USD", "unit": "KG", "unit_price": "11"}
],
"fee_lines": [],
}
quote["fee_lines"] = list(quote["fee_rows"])
out = apply_to_quote(quote, {"体积": "3", "毛重": "500"})
self.assertEqual(str((out.get("fee_rows") or [])[0].get("amount")), "8800")
self.assertEqual(str(out.get("total")), "8800")
def test_non_air_lines_are_untouched(self) -> None:
for line in ("SEA", "LAND"):
out = apply_to_quote(self._manual_quote(), {"体积": "3", "毛重": "500"}, business_line=line)
self.assertNotIn("chargeableWeightKg", out)
def test_no_facts_keeps_quote_as_is(self) -> None:
out = apply_to_quote(self._manual_quote(), {})
self.assertEqual(str(out.get("total")), "11")
self.assertNotIn("chargeableWeightKg", out)
class AirManualLayoutChargeableTest(unittest.TestCase):
"""会话界面:人工报价版式里要有计费重。"""
def test_layout_prints_chargeable_without_basis(self) -> None:
"""
计费重只写数字。
产品 2026-11-22 晚在空运协同群核对:人工报价里「计费重:500KG(按实重)」
「计费重:333.4KG(按体积重)」的括注要全部去掉,销售只看 KG 数。
"""
from agent.policy import inquiry_copy as copy
quote = {
"currency": "USD",
"fee_rows": [
{"name": "空运费", "amount": "11", "currency": "USD", "unit": "KG", "unit_price": "11"}
],
"fee_lines": [],
}
quote["fee_lines"] = list(quote["fee_rows"])
facts = {"毛重": "500", "体积": "3", "起运港": "NKG", "目的港": "KUL"}
text = "\n".join(
copy._manual_quote_lines(quote, business_line="AIR", facts=facts, layout="manual")
)
self.assertIn("计费重:500.1KG", text)
self.assertNotIn("按体积重", text)
self.assertNotIn("按实重", text)
def test_stored_basis_never_leaks_hint(self) -> None:
"""
报价对象上已落库 chargeable_weight_basis(volume/gross)时也不能把括注写出来。
这是本次 BUG 的原始路径:落库时写了依据,渲染时又拿依据拼括注。
依据只留在数据里供排查,不参与对外文字。
"""
from agent.policy import inquiry_copy as copy
quote = {
"currency": "USD",
"chargeableWeightKg": "333.4",
"chargeable_weight_basis": "volume",
"fee_rows": [
{"name": "空运费", "amount": "11", "currency": "USD", "unit": "KG", "unit_price": "11"}
],
}
text = "\n".join(
copy._manual_quote_lines(quote, business_line="AIR", facts={}, layout="manual")
)
self.assertIn("计费重:333.4KG", text)
self.assertNotIn("按体积重", text)
self.assertNotIn("按实重", text)
def test_layout_prints_explicit_chargeable_without_hint(self) -> None:
from agent.policy import inquiry_copy as copy
quote = {
"currency": "USD",
"chargeableWeightKg": "800",
"fee_rows": [{"name": "空运费", "amount": "11", "currency": "USD", "unit": "KG"}],
}
text = "\n".join(
copy._manual_quote_lines(quote, business_line="AIR", facts={}, layout="manual")
)
self.assertIn("计费重:800KG", text)
self.assertNotIn("按体积重", text)
def test_no_facts_no_chargeable_line(self) -> None:
from agent.policy import inquiry_copy as copy
quote = {
"currency": "USD",
"fee_rows": [{"name": "空运费", "amount": "11", "currency": "USD", "unit": "KG"}],
}
text = "\n".join(
copy._manual_quote_lines(quote, business_line="AIR", facts={}, layout="manual")
)
self.assertNotIn("计费重", text)
if __name__ == "__main__":
unittest.main()