Files

166 lines
5.4 KiB
Python

# -*- coding: utf-8 -*-
"""回归测试:分拣工位「不称重分拣」(v1.0.28)。
分拣位经常没秤:应拣多少斤是单据上写死的,操作员点得清实际装了多少,
不该被迫每件上秤。这里验证:
① 分拣站 noweigh 行包含斤行,入库站不含(入库必须按实收复核);
② 斤行提交时 picked_weight 传的是业务单位(斤),不是 kg;
③ 台账里反算的 kg 正确(20 斤 → 10kg);
④ 超拣不拦,但留日志;
⑤ 件/包这类仍然进 picked_weight=0,结算走件数。
不需要秤/摄像头/网络。
"""
import sys
import io
import os
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from core.flow import FlowEngine
CFG = {
'flow': {'min_scale_kg': 0.05, 'stable_ms': 400, 'settle_ms': 650,
'stable_delta_kg': 0.03, 'done_timeout_s': 12,
'done_lift_off_kg': 0.2, 'dup_window_s': 180},
'scale': {'simulate': True},
'camera': {'simulate': True},
'vision': {'min_confidence': 65},
'server': {'mock': True},
'noweigh': {'picking_allow_weight_unit': True},
}
logs = []
eng = FlowEngine(CFG, lambda ev, p=None: logs.append((ev, p)))
eng.refresh_pending = lambda initial=False: None
ROWS = [
{'id': 'r1', 'item_name': '圆萝卜', 'unit': '斤', 'required_qty': 20,
'pick_code': 'PK001'},
{'id': 'r2', 'item_name': '土豆', 'unit': '包', 'required_qty': 3,
'pick_code': 'PK001'},
]
eng.rows = ROWS
class FakeJournal:
def begin(self, *a, **k):
return {'op': a[0] if a else 'op'}
def update(self, *a, **k):
pass
def conclude(self, *a, **k):
pass
def has_unresolved(self):
return False
class FakeApi:
user_id = 'u1'
base_url = 'http://test'
def __init__(self):
self.calls = []
def commit_picking(self, row, qty, weight, op, photo_expected):
self.calls.append({'row_id': row['id'], 'qty': qty,
'weight': weight, 'op': op})
return {'operation_status': 'committed_by_this_operation',
'client_operation_id': op}
def reset(station):
eng.station = station
eng.state = 'IDLE'
eng._count_mode = False
eng._count_qty = 0.0
eng._frozen_kg = 0.0
eng._frozen_row = None
eng.api = FakeApi()
eng.journal = FakeJournal()
eng.frames = type('F', (), {
'capture_confirmed': lambda self: {'qualified': False,
'reason': 'sim', 'path': None},
'poke_focus': lambda self: None})()
class FS:
def state(self, flow=None):
return {'weight': 0.0, 'fresh': True, 'present': False,
'overload': False, 'zero': 0.0, 'tare': 0.0,
'raw': 0.0, 'at': 0, 'stable': True}
eng.scale = FS()
eng._record = lambda *a, **k: None
eng._print_async = lambda *a, **k: None
eng._upload_photo_later = lambda *a, **k: None
print('=== 1. 分拣站:斤行也能不称重 ===')
reset('picking')
names = [r['item_name'] for r in eng.noweigh_rows()]
print(' noweigh 行 =', names, ' 旧 count_rows =',
[r['item_name'] for r in eng.count_rows()])
assert '圆萝卜' in names and '土豆' in names
assert '圆萝卜' not in [r['item_name'] for r in eng.count_rows()]
print('=== 2. 入库站:斤行仍然不许不称重 ===')
reset('inspecting')
names = [r['item_name'] for r in eng.noweigh_rows()]
print(' noweigh 行 =', names)
assert '圆萝卜' not in names and '土豆' in names
eng.state = 'COUNT_CONFIRM'
ok, msg = eng.submit_count(ROWS[0], 20)
print(' 入库站提交斤行 →', ok, msg)
assert not ok and '按重量计量' in msg
print('=== 3. 分拣站提交 20 斤:picked_weight 传 20(斤),台账 kg=10 ===')
reset('picking')
eng.state = 'COUNT_CONFIRM'
ok, msg = eng.submit_count(ROWS[0], 20)
assert ok, msg
print(' _frozen_kg =', eng._frozen_kg, ' _count_qty =', eng._count_qty)
assert abs(eng._frozen_kg - 10.0) < 1e-6
eng._submit_worker(ROWS[0], 20, '不称重')
call = eng.api.calls[-1]
print(' 提交参数 =', call)
assert call['qty'] == 20 and call['weight'] == 20
assert eng.state == 'DONE'
print('=== 4. 超拣:不拦,但留日志 ===')
reset('picking')
eng.state = 'COUNT_CONFIRM'
ok, msg = eng.submit_count(ROWS[0], 22)
print(' 提交 22 斤(应拣 20)→', ok, msg)
assert ok
assert any('超过应拣' in str(p) for e, p in logs if e == 'log')
print('=== 5. 件/包类:picked_weight 仍为 0,结算走件数 ===')
reset('picking')
eng.state = 'COUNT_CONFIRM'
ok, msg = eng.submit_count(ROWS[1], 3)
assert ok, msg
eng._submit_worker(ROWS[1], 3, '不称重')
call = eng.api.calls[-1]
print(' 提交参数 =', call, ' 台账 kg =', eng._frozen_kg)
assert call['qty'] == 3 and call['weight'] == 0
assert eng._frozen_kg == 0.0
print('=== 6. 不称重确认态下,秤上没货不触发重量复核 ===')
reset('picking')
eng.state = 'COUNT_CONFIRM'
eng._count_mode = True
eng._frozen_kg = 10.0
ok = eng._snapshot_ok(ROWS[0])
print(' _snapshot_ok =', ok)
assert ok
print('=== 7. 关闭开关后(高级设置)分拣站也退回只认件/包 ===')
eng.cfg['noweigh']['picking_allow_weight_unit'] = False
reset('picking')
print(' noweigh 行 =', [r['item_name'] for r in eng.noweigh_rows()])
assert [r['item_name'] for r in eng.noweigh_rows()] == ['土豆']
print('\n全部通过 ✔')