Files
windows-fenjian/tools/_test_group_partial.py
Ubuntu dde18c1d87 fix(inbound): 整组短收按部分成功提示剩余(v1.0.68)
- 配套服务端 2026-11-22 方案A:短收整组入库返回 200 + partialGroup/remainingQty
- 成功提示写明「整组部分入库:已入 N 行,剩余 X 斤,下次称重继续;不要用旧组重试」
- commit_inspection_group 回包带出 partial_group/merged_count/remaining_qty/unallocated_lines
- reject_hint 新增 scope_incomplete / scope_changed / group_changed / scope_mismatch
  的中文可执行提示:刷新待验列表后再称,不要直接重试旧组
- 新增 tools/_test_group_partial.py(15 项源码级回归,本机无需 Windows 运行库)
- 版本 1.0.67 → 1.0.68(build_exe.py / installer.iss / 使用说明.txt)
2026-11-22 19:52:59 +08:00

132 lines
5.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 -*-
"""整组入库·短收部分成功回归(2026-11-22 方案 A)
背景(BR002 生产事故):5 行组、实称 6.48 斤,服务端先入 2 行再 409 scope_incomplete,
客户端把 409 当「未提交」,拿旧组重试又撞 scope_changed。
本机(Linux 构建机)没有 Windows 端运行依赖(numpy/pywin32/opencv…),所以本用例
**不 import 整包**,而是从源码中抽取待测函数做真实行为测试 + 源码契约断言:
① 服务端 200 部分成功时,客户端带出 partial_group / merged_count / remaining_qty / 未入行;
② 成功提示说清「已入 N 行、剩余 X 斤、下次再称,不要用旧组重试」;
③ scope_incomplete / scope_changed 等业务拒绝翻成人话(刷新待验列表、不直接重试旧组)。
设计:docs/design/20261122-weigh-group-partial-inbound.md
"""
import io
import os
import re
import sys
import textwrap
import types
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
FLOW = os.path.join(ROOT, 'core', 'flow.py')
CLIENT = os.path.join(ROOT, 'core', 'api', 'client.py')
checks = 0
def ok(cond, msg):
global checks
checks += 1
if not cond:
raise AssertionError(msg)
print(' ✓', msg)
def read(p):
with open(p, encoding='utf-8') as fh:
return fh.read()
def extract_def(src, header):
"""按 4 空格缩进抽取一个 def 块(方法或顶层函数),返回去缩进源码。"""
start = src.index(header)
m = re.search(r'\n(?= def |\ndef |class )', src[start + len(header):])
end = start + len(header) + (m.start() if m else len(src))
return textwrap.dedent(src[start:end])
flow_src = read(FLOW)
client_src = read(CLIENT)
# ---- ① 客户端回包解析(抽取真实方法源码执行,不联网) ----
print('① 客户端解析 200 短收部分成功回包')
ns = {'__name__': 'client_probe'}
class _ApiError(Exception):
def __init__(self, code, message):
super().__init__(message)
self.code = code
ns['ApiError'] = _ApiError
exec(extract_def(client_src, 'def commit_inspection_group('), ns)
class _FakeApi:
def __init__(self, payload):
self.mock = False
self.cfg = {}
self._payload = payload
def _request(self, *args, **kwargs):
return self._payload
ns['_FakeApi'] = _FakeApi
_FakeApi.commit_inspection_group = ns['commit_inspection_group']
partial = _FakeApi({
'success': True,
'data': {
'merged': True, 'mergedCount': 2, 'partialGroup': True, 'remainingQty': 7.38,
'unallocatedLines': [{'id': 'x', 'prepare_qty': 3}],
},
}).commit_inspection_group({'inspection_ids': ['a', 'b', 'c', 'd', 'e']}, 6.48, 3.24, 'op1')
ok(partial.get('partial_group') is True, 'partial_group=True')
ok(partial.get('merged_count') == 2, 'merged_count=2(已入 2 行)')
ok(abs(float(partial.get('remaining_qty') or 0) - 7.38) < 1e-9, 'remaining_qty=7.38 斤')
ok(len(partial.get('unallocated_lines') or []) == 1, 'unallocated_lines 带出未入行')
ok(partial.get('data', {}).get('mergedCount') == 2, 'data 原样保留(供现有成功判定)')
full = _FakeApi({
'success': True,
'data': {'merged': True, 'mergedCount': 2, 'partialGroup': False, 'remainingQty': 0},
}).commit_inspection_group({'inspection_ids': ['a', 'b']}, 13, 6.5, 'op2')
ok(full.get('partial_group') is False, '整组入完时 partial_group=False')
# ---- ② 成功提示(抽取 _success_extra 方法执行) ----
print('② 成功提示:说清已入行数/剩余量/不要旧组重试')
ns2 = {'__name__': 'flow_probe'}
exec(extract_def(flow_src, 'def _success_extra('), ns2)
_Eng = type('_Eng', (), {'_success_extra': ns2['_success_extra'], '_strict_info': lambda self, r: {}})
eng = _Eng()
eng._frozen_row = {'unit': '斤'}
note = eng._success_extra('ok', {'data': {'partialGroup': True, 'mergedCount': 2, 'remainingQty': 7.38}})
ok('已入 2 行' in note, '提示包含「已入 2 行」')
ok('7.38' in note, '提示包含剩余量 7.38')
ok('不要用旧组重试' in note, '提示明确不要用旧组重试')
ok(eng._success_extra('ok', {'data': {}}) == '', '普通成功不额外加话')
# ---- ③ 业务拒绝翻译(抽取 reject_hint 执行) ----
print('③ 业务拒绝翻译(保底旧后端/范围变化)')
ns3 = {'__name__': 'hint_probe', 'M': types.SimpleNamespace(required_qty_of=lambda row, mode=None: 0.0)}
exec(extract_def(flow_src, 'def reject_hint('), ns3)
reject_hint = ns3['reject_hint']
hint = reject_hint('业务拒绝 409: {"error":"scope_incomplete","error_description":"整组入库行数不一致"}', {'unit': '斤'})
ok('刷新待验列表' in hint and '不要直接重试' in hint, 'scope_incomplete → 刷新后重称、不重试')
hint2 = reject_hint('业务拒绝 409: {"error":"scope_changed"}', {'unit': '斤'})
ok('刷新待验列表' in hint2, 'scope_changed → 刷新待验列表')
hint3 = reject_hint('业务拒绝 409: {"error":"group_changed"}', {'unit': '斤'})
ok('刷新待验列表' in hint3, 'group_changed → 刷新待验列表')
ok(reject_hint('业务拒绝 400: {"error":"invalid_cost"}', {'unit': '斤'}) == '',
'未登记错误保持原样(不硬造提示)')
ok('weigh_tolerance' not in hint, '非容差类错误不误报重量对不上')
print('\n全部通过:%d 项' % checks)