feat(shipping_notice): 支持客户 ODM 白名单与整单 ODM 选择收敛导出
- 客户主数据新增 odm_ids 白名单字段(含 Alembic 迁移),表单与管理端支持维护 - 导出库存命中行按客户白名单与整单 ODM 裁决收敛,新增 notice_odm_choice_service - 工作台新增 odm-options / odm-choice 接口,投影行级候选并持久化用户选择 - 修复复核工作台跨 Run 切换时接收解析上下文残留 - 同步四语言 i18n、测试用例、funclist 与 shipping-notice 技术文档
This commit is contained in:
1 parent
a032354cb9
commit
d396371020
51 files changed
+2717
-109
No files matched your search
@@ -29,6 +29,14 @@ RESERVATION_DEFINITION = {
|
||||
],
|
||||
}
|
||||
|
||||
LN_RESERVATION_DEFINITION = {
|
||||
'reservation_id_region': {'region': {'x0': .1, 'y0': .1, 'x1': .9, 'y1': .2}, 'page_no': 1},
|
||||
'fields': DEFINITION['fields'] + [
|
||||
{'field_id': 'line-reservation', 'field_code': 'reservation_id', 'scope': 'line', 'required': True},
|
||||
{'field_id': 'ln', 'field_code': 'customer_po_line_no', 'scope': 'line'},
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def _row(po: str, part: str) -> dict:
|
||||
return {'status': 'parsed', 'issues': [], 'fields': [
|
||||
@@ -38,6 +46,12 @@ def _row(po: str, part: str) -> dict:
|
||||
]}
|
||||
|
||||
|
||||
def _ln_row(po: str, part: str, ln: str) -> dict:
|
||||
row = _row(po, part)
|
||||
row['fields'].append({'field_id': 'ln', 'value': ln, 'quote': ln})
|
||||
return row
|
||||
|
||||
|
||||
def _page_glyphs(*lines: str) -> list[dict]:
|
||||
"""构造跨页 Reservation 路由回归所需的最小坐标文字。"""
|
||||
glyphs = []
|
||||
@@ -152,6 +166,63 @@ def test_approved_pdf_marks_conflicting_reservation_region_for_review():
|
||||
assert (orders or [])[0]['reservation_region_status'] == 'needs_review'
|
||||
|
||||
|
||||
def test_approved_pdf_maps_reservation_by_printed_ln_when_ln_non_contiguous():
|
||||
"""PO_6075309 回归:LN=1/2/4/5 非连续时 L#n 必须按 LN 值落行,整单不转复核。
|
||||
|
||||
关键断言:L#4/L#5 分别写入 LN=4/LN=5 行(旧位置序会把 L#4 错配到 LN=5 并把
|
||||
L#5 误报 line_out_of_range);行级 reservation 已全部归属后 source_missing_fields
|
||||
不得残留 line_index 0 的假缺失(``or -1`` falsy bug)。
|
||||
"""
|
||||
orders, reason = _build_approved_profile_order_result(
|
||||
unit=SimpleNamespace(document_id=9), intake={'intake_no': 'SNI-1'}, schema=SCHEMA,
|
||||
definition=LN_RESERVATION_DEFINITION,
|
||||
table={'rows': [
|
||||
_ln_row('PO-1', 'P-1', '1'), _ln_row('PO-1', 'P-2', '2'),
|
||||
_ln_row('PO-1', 'P-4', '4'), _ln_row('PO-1', 'P-5', '5'),
|
||||
]},
|
||||
reservation_region_candidates=[
|
||||
{'value': '1000159370', 'scope': 'line', 'line_no': 1, 'raw_text': 'Reservation L#1: 1000159370'},
|
||||
{'value': '1000159366', 'scope': 'line', 'line_no': 2, 'raw_text': 'Reservation L#2: 1000159366'},
|
||||
{'value': '1000159379', 'scope': 'line', 'line_no': 4, 'raw_text': 'Reservation L#4: 1000159379'},
|
||||
{'value': '1000159236', 'scope': 'line', 'line_no': 5, 'raw_text': 'Reservation L#5: 1000159236'},
|
||||
],
|
||||
)
|
||||
|
||||
assert reason is None
|
||||
order = (orders or [])[0]
|
||||
assert order.get('reservation_region_status') != 'needs_review'
|
||||
assert 'source_missing_fields' not in order
|
||||
expected_by_ln = {'1': '1000159370', '2': '1000159366', '4': '1000159379', '5': '1000159236'}
|
||||
for line in order['lines']:
|
||||
ln = next(item['observations'][0]['value'] for item in line['fields'] if item['field_id'] == 'ln')
|
||||
assert any(
|
||||
item['field_id'] == 'line-reservation' and item['observations'][0]['value'] == expected_by_ln[ln]
|
||||
for item in line['fields']
|
||||
)
|
||||
|
||||
|
||||
def test_approved_pdf_keeps_reservation_review_when_referenced_ln_row_is_missing():
|
||||
"""被引用的印刷行号在解析行中不存在时必须转复核,不得按位置静默写入。"""
|
||||
orders, reason = _build_approved_profile_order_result(
|
||||
unit=SimpleNamespace(document_id=9), intake={'intake_no': 'SNI-1'}, schema=SCHEMA,
|
||||
definition=LN_RESERVATION_DEFINITION,
|
||||
table={'rows': [_ln_row('PO-1', 'P-1', '1'), _ln_row('PO-1', 'P-2', '2')]},
|
||||
reservation_region_candidates=[
|
||||
{'value': '1000159236', 'scope': 'line', 'line_no': 5, 'raw_text': 'Reservation L#5: 1000159236'},
|
||||
],
|
||||
)
|
||||
|
||||
assert reason is None
|
||||
order = (orders or [])[0]
|
||||
assert order['reservation_region_status'] == 'needs_review'
|
||||
assert order['reservation_region_review_reasons'] == ['line_out_of_range']
|
||||
# LN=1/LN=2 行无对应候选,reservation 保持缺失供人工补录。
|
||||
assert all(
|
||||
not any(item['field_id'] == 'line-reservation' for item in line['fields'])
|
||||
for line in order['lines']
|
||||
)
|
||||
|
||||
|
||||
def test_enabled_reservation_region_reads_vendor_comments_across_pages():
|
||||
"""已标注专区时,正式路由不能使用旧 page_no/region,必须跨页按文本锚点读取。"""
|
||||
result = _reservation_region_candidates(
|
||||
|
||||
@@ -896,3 +896,260 @@ def test_export_commit_failure_removes_generated_file(app, tmp_path, monkeypatch
|
||||
with pytest.raises(RuntimeError, match='commit failed'):
|
||||
_commit_generated_export(str(path))
|
||||
assert not path.exists()
|
||||
|
||||
|
||||
def _build_multi_odm_notice(
|
||||
*, tmp_path, odm_values=None, customer_odm_ids=None, choice_odm=None, line_specs=None,
|
||||
):
|
||||
"""
|
||||
构造「订单行命中多个 ODM ID」的整单导出场景;须在 app_context 内调用。
|
||||
|
||||
Args:
|
||||
odm_values: 单订单行场景下库存行命中的 ODM 值列表。
|
||||
line_specs: 多订单行场景 [{'part': 料号, 'odm_values': [...]}];提供时忽略 odm_values。
|
||||
customer_odm_ids: 客户数据的 ODM ID 范围(None = 不限定)。
|
||||
choice_odm: 整单用户选择(写入 OT.chosen_odm_id)
|
||||
Returns:
|
||||
(notice, customer)。
|
||||
"""
|
||||
from tests.conftest import confirm_test_customer_resolution
|
||||
|
||||
schema = ShippingOrderSchema(schema_code=DEFAULT_SCHEMA_FAMILY_CODE, display_name='Order')
|
||||
db.session.add(schema); db.session.flush()
|
||||
part = _lookup_standard_fields('part_number')['part_number']
|
||||
version = ShippingOrderSchemaVersion(schema_id=schema.id, version=1, status='published', definition_json={})
|
||||
db.session.add(version); db.session.flush()
|
||||
draft = excel_template.create_draft(_workbook_upload(), {
|
||||
'template_code': 'ODMSCOPE', 'display_name': 'OdmScope',
|
||||
'definition': {'filename_pattern': '{task_no}.xlsx', 'mappings': [
|
||||
{'sheet': 'Detail', 'row_mode': 'header', 'start_row': 2, 'target_column': 1, 'source_kind': 'inventory_field', 'source_key': _inventory_field_id('odm_id'), 'formatter': 'text'},
|
||||
]},
|
||||
}, actor_id=None)
|
||||
published = excel_template.publish(draft['id'], actor_id=None)
|
||||
parsing = ShippingInventoryParsingProfile(
|
||||
profile_code='ODMSCOPE', display_name='OdmScope', version=1, status='published', file_format='xlsx',
|
||||
definition_json={'field_mapping': {_inventory_field_id('part_number'): 'Part No.', _inventory_field_id('odm_id'): 'ODM ID'}, 'match_keys': [{'order_field_id': part.id, 'inventory_field_id': _inventory_field_id('part_number')}]},
|
||||
)
|
||||
login = ShippingRpaProfile(
|
||||
profile_code='ODMSCOPE', display_name='OdmScope', connector_type='dbs', login_url='https://invalid',
|
||||
login_config_json={'auth_mode': 'password'},
|
||||
)
|
||||
db.session.add_all([parsing, login]); db.session.flush()
|
||||
db.session.add(ShippingInventorySource(
|
||||
source_code='ODMSCOPE', display_name='OdmScope', connector_type='dbs', rpa_profile_id=login.id,
|
||||
parsing_profile_id=parsing.id, excel_template_id=published['id'], warehouse_resolution_mode='fixed',
|
||||
fixed_warehouse_id='DUB', report_path_json={}, query_parameters_json=[],
|
||||
))
|
||||
customer = ShippingCustomer(
|
||||
standard_name='ODM范围客户', short_code='ODMSC', status='active',
|
||||
odm_ids=list(customer_odm_ids) if customer_odm_ids is not None else None,
|
||||
)
|
||||
db.session.add(customer); db.session.flush()
|
||||
ot = ShippingOrderTask(task_no='OT-ODMSCOPE', user_id=1, status='ready', current_step='customer_review')
|
||||
db.session.add(ot); db.session.flush()
|
||||
extraction = ShippingOrderExtraction(
|
||||
task_id=ot.id, schema_version_id=version.id, input_snapshot_hash='i' * 64, status='accepted',
|
||||
)
|
||||
db.session.add(extraction); db.session.flush()
|
||||
specs = line_specs or [{'part': 'PN-A', 'odm_values': odm_values}]
|
||||
snapshot_lines = []
|
||||
for index, spec in enumerate(specs, start=1):
|
||||
order_line = ShippingOrderLine(extraction_id=extraction.id, position=index, line_no=index)
|
||||
db.session.add(order_line); db.session.flush()
|
||||
decision = ShippingOrderFieldDecision(
|
||||
extraction_id=extraction.id, line_id=order_line.id, field_id=part.id,
|
||||
decision_status='accepted', resolved_value_json=spec['part'],
|
||||
)
|
||||
db.session.add(decision); db.session.flush()
|
||||
db.session.add(ShippingOrderFieldValue(
|
||||
extraction_id=extraction.id, line_id=order_line.id, field_id=part.id,
|
||||
value_text=spec['part'], decision_id=decision.id,
|
||||
))
|
||||
snapshot_lines.append((index, part.id, spec['part'], 1, f'100000000{index}'))
|
||||
confirm_test_customer_resolution(ot, customer=customer, extraction=extraction)
|
||||
inventory_rows = []
|
||||
for spec in specs:
|
||||
for odm in spec['odm_values']:
|
||||
inventory_rows.append({_inventory_field_id('part_number'): spec['part'], _inventory_field_id('odm_id'): odm})
|
||||
inventory_file = ShippingInventoryFile(
|
||||
inventory_file_no='SIF-ODMSCOPE', user_id=None, source_file_name='odm-scope.xlsx',
|
||||
storage_path=str(tmp_path / 'odm-scope.xlsx'),
|
||||
inventory_lines_json=inventory_rows,
|
||||
line_count=len(inventory_rows), share_mode='task_only', warehouse_id='DUB', source_code='ODMSCOPE',
|
||||
snapshot_status='ready',
|
||||
)
|
||||
db.session.add(inventory_file); db.session.flush()
|
||||
snapshot = _freeze_export_snapshot(
|
||||
ot, extraction, version,
|
||||
lines=snapshot_lines,
|
||||
customer_id=customer.id, inventory_file_id=inventory_file.id,
|
||||
)
|
||||
notice = _materialize_export_notice(
|
||||
ot=ot, snapshot=snapshot, inventory_file=inventory_file, source_code='ODMSCOPE', notice_no='TASK-ODMSCOPE',
|
||||
)
|
||||
if choice_odm is not None:
|
||||
for match in ShippingNoticeLineInventoryMatch.query.filter_by(task_id=int(ot.id), is_current=True).all():
|
||||
match.chosen_odm_id = choice_odm
|
||||
db.session.flush()
|
||||
return notice, customer
|
||||
|
||||
|
||||
def test_export_narrows_odm_by_customer_range(app, tmp_path):
|
||||
"""客户 ODM ID 范围非空时,导出只取范围内库存行;范围命中唯一值写入表头。"""
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, _customer = _build_multi_odm_notice(
|
||||
tmp_path=tmp_path, odm_values=['ODM-1', 'ODM-2'], customer_odm_ids=['odm-2'],
|
||||
)
|
||||
|
||||
path, _filename_value, _artifact = generate_shipment_excel(task)
|
||||
|
||||
workbook = load_workbook(path, data_only=True)
|
||||
try:
|
||||
assert workbook['Detail'].cell(2, 1).value == 'ODM-2'
|
||||
finally:
|
||||
workbook.close()
|
||||
|
||||
|
||||
def test_export_odm_range_excluding_all_raises_specific_error(app, tmp_path):
|
||||
"""客户范围把全部命中排除时,必须报指向客户数据的可行动错误而非「没有匹配」。"""
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, _customer = _build_multi_odm_notice(
|
||||
tmp_path=tmp_path, odm_values=['ODM-1', 'ODM-2'], customer_odm_ids=['ODM-9'],
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match='不在客户数据维护的 ODM ID 范围'):
|
||||
generate_shipment_excel(task)
|
||||
|
||||
|
||||
def test_export_applies_notice_odm_choice(app, tmp_path):
|
||||
"""整单用户选择优先于多值冲突;选择经 casefold 归一后命中库存行。"""
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, _customer = _build_multi_odm_notice(
|
||||
tmp_path=tmp_path, odm_values=['ODM-1', 'ODM-2'], choice_odm='odm-2',
|
||||
)
|
||||
|
||||
path, _filename_value, _artifact = generate_shipment_excel(task)
|
||||
|
||||
workbook = load_workbook(path, data_only=True)
|
||||
try:
|
||||
assert workbook['Detail'].cell(2, 1).value == 'ODM-2'
|
||||
finally:
|
||||
workbook.close()
|
||||
|
||||
|
||||
def test_export_auto_narrows_unique_feasible_odm_without_manual_choice(app, tmp_path):
|
||||
"""无范围、无选择时,整单唯一可行 ODM(各行命中交集恰一个)自动收敛。"""
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, _customer = _build_multi_odm_notice(tmp_path=tmp_path, line_specs=[
|
||||
{'part': 'PN-A', 'odm_values': ['ODM-1', 'ODM-2']},
|
||||
{'part': 'PN-B', 'odm_values': ['ODM-2']},
|
||||
])
|
||||
|
||||
path, _filename_value, _artifact = generate_shipment_excel(task)
|
||||
|
||||
workbook = load_workbook(path, data_only=True)
|
||||
try:
|
||||
assert workbook['Detail'].cell(2, 1).value == 'ODM-2'
|
||||
finally:
|
||||
workbook.close()
|
||||
|
||||
|
||||
def test_export_keeps_conflict_when_odm_scope_cannot_decide(app, tmp_path):
|
||||
"""无范围、无选择且多值并存时维持折叠冲突阻断,等待用户整单选择。"""
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, _customer = _build_multi_odm_notice(tmp_path=tmp_path, odm_values=['ODM-1', 'ODM-2'])
|
||||
|
||||
with pytest.raises(ValueError, match='库存字段“ODM 编号”'):
|
||||
generate_shipment_excel(task)
|
||||
|
||||
|
||||
def test_notice_odm_options_and_choice_roundtrip(app, tmp_path):
|
||||
"""整单候选按客户范围逐行投影;合法选择落库并回显,非法选择有明确错误。"""
|
||||
from app.core.errors import AppError
|
||||
from app.services.shipping_notice.notice_odm_choice_service import (
|
||||
notice_odm_options,
|
||||
set_notice_odm_choice,
|
||||
)
|
||||
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, customer = _build_multi_odm_notice(
|
||||
tmp_path=tmp_path, odm_values=['ODM-1', 'ODM-2'], customer_odm_ids=['ODM-2', 'ODM-3'],
|
||||
)
|
||||
|
||||
options = notice_odm_options(task)
|
||||
|
||||
# [断言] 逐行候选是客户范围过滤后的集合,且保留库存行原始大小写。
|
||||
assert options['whitelist_applied'] is True
|
||||
assert options['customer_odm_ids'] == ['ODM-2', 'ODM-3']
|
||||
assert len(options['lines']) == 1
|
||||
assert options['lines'][0]['odm_values'] == ['ODM-2']
|
||||
assert options['lines'][0]['chosen_odm_id'] is None
|
||||
|
||||
snapshot_line_id = options['lines'][0]['snapshot_line_id']
|
||||
result = set_notice_odm_choice(task, snapshot_line_id=snapshot_line_id, odm_id='ODM-2')
|
||||
|
||||
assert result['odm_id'] == 'ODM-2'
|
||||
matches = ShippingNoticeLineInventoryMatch.query.filter_by(
|
||||
task_id=int(task.order_task_id), is_current=True,
|
||||
).all()
|
||||
assert matches and {m.chosen_odm_id for m in matches} == {'ODM-2'}
|
||||
options = notice_odm_options(task)
|
||||
assert options['lines'][0]['chosen_odm_id'] == 'ODM-2'
|
||||
|
||||
with pytest.raises(AppError) as exc:
|
||||
set_notice_odm_choice(task, snapshot_line_id=snapshot_line_id, odm_id='ODM-9')
|
||||
assert getattr(exc.value, 'code', '') == 'SHIPPING.NOTICE_ODM_CHOICE_INVALID'
|
||||
|
||||
|
||||
def test_notice_odm_choice_syncs_same_snapshot_file_lines(app, tmp_path):
|
||||
"""同一快照文件的行:改任一行 ODM ID,其余行同步落库同一选择。"""
|
||||
from app.services.shipping_notice.notice_odm_choice_service import (
|
||||
notice_odm_options,
|
||||
set_notice_odm_choice,
|
||||
)
|
||||
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, _customer = _build_multi_odm_notice(tmp_path=tmp_path, line_specs=[
|
||||
{'part': 'PN-A', 'odm_values': ['ODM-1', 'ODM-2']},
|
||||
{'part': 'PN-B', 'odm_values': ['ODM-2', 'ODM-3']},
|
||||
])
|
||||
|
||||
options = notice_odm_options(task)
|
||||
# [断言] 每行有自己的候选;同文件两行的可行集=交集。
|
||||
by_sid = {str(item['snapshot_line_id']): item for item in options['lines']}
|
||||
assert len(by_sid) == 2
|
||||
assert options['lines'][0]['odm_values'] == ['ODM-1', 'ODM-2']
|
||||
|
||||
target_sid = options['lines'][0]['snapshot_line_id']
|
||||
result = set_notice_odm_choice(
|
||||
task, snapshot_line_id=target_sid, odm_id='ODM-2',
|
||||
)
|
||||
|
||||
# [断言] 同步范围=同一快照文件:两行当前匹配行写同一选择。
|
||||
assert result['synced_line_count'] == 2
|
||||
matches = ShippingNoticeLineInventoryMatch.query.filter_by(
|
||||
task_id=int(task.order_task_id), is_current=True,
|
||||
).all()
|
||||
assert len(matches) == 2
|
||||
assert {m.chosen_odm_id for m in matches} == {'ODM-2'}
|
||||
options = notice_odm_options(task)
|
||||
assert all(item['chosen_odm_id'] == 'ODM-2' for item in options['lines'])
|
||||
|
||||
|
||||
def test_notice_odm_options_empty_when_template_ignores_odm(app, tmp_path):
|
||||
"""模板不消费 odm 字段时投影为空候选,不产生任何选择入口。"""
|
||||
from app.services.shipping_notice.notice_odm_choice_service import notice_odm_options
|
||||
|
||||
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
||||
with app.app_context():
|
||||
task, _customer = _build_multi_odm_notice(tmp_path=tmp_path, odm_values=['ODM-1'])
|
||||
options = notice_odm_options(task)
|
||||
assert options['odm_field_id'] is not None
|
||||
assert options['lines'] and options['lines'][0]['odm_values'] == ['ODM-1']
|
||||
@@ -212,6 +212,35 @@ def test_reservation_region_requires_review_when_numbered_value_has_no_line_targ
|
||||
assert applied.review_reasons == ('line_field_unavailable',)
|
||||
|
||||
|
||||
def test_reservation_region_matches_line_by_printed_ln_number_not_position():
|
||||
"""LN 非连续(部分行已拆单)时必须按 LN 值定位逻辑行,禁止位置错配或误报越界。"""
|
||||
applied = apply_reservation_region_candidates(
|
||||
candidates=extract_reservation_from_region_text(
|
||||
"Reservation L#1: 1000159370\nReservation L#2: 1000159366\n"
|
||||
"Reservation L#4: 1000159379\nReservation L#5: 1000159236"
|
||||
),
|
||||
header_field_id='header-reservation', line_field_id='line-reservation',
|
||||
line_count=4, line_numbers=[1, 2, 4, 5],
|
||||
)
|
||||
|
||||
assert applied.needs_review is False
|
||||
# 键是逻辑行序号:L#4→第 3 行(LN=4)、L#5→第 4 行(LN=5)。
|
||||
assert applied.line_values == {1: '1000159370', 2: '1000159366', 3: '1000159379', 4: '1000159236'}
|
||||
|
||||
|
||||
def test_reservation_region_flags_review_when_referenced_ln_number_is_absent():
|
||||
"""被引用的印刷行号在解析行中不存在时必须转复核,不得静默按位置写入。"""
|
||||
applied = apply_reservation_region_candidates(
|
||||
candidates=extract_reservation_from_region_text("Reservation L#5: 1000159236"),
|
||||
header_field_id='header-reservation', line_field_id='line-reservation',
|
||||
line_count=2, line_numbers=[1, 2],
|
||||
)
|
||||
|
||||
assert applied.line_values == {}
|
||||
assert applied.needs_review is True
|
||||
assert applied.review_reasons == ('line_out_of_range',)
|
||||
|
||||
|
||||
def test_reservation_gate_blocks_empty_and_malformed_order_lines():
|
||||
"""空订单或畸形行都不能被视为 Reservation 门禁通过。"""
|
||||
from app.services.shipping_notice.reservation_gate import reservation_gate_issues
|
||||
|
||||
@@ -2962,3 +2962,254 @@ def test_run_lineage_root_walk_loads_parent_runs_without_frozen_snapshot_columns
|
||||
for statement in point_loads:
|
||||
assert 'input_snapshot_json' not in statement
|
||||
assert 'result_json' not in statement
|
||||
|
||||
|
||||
def test_review_queue_flags_blocked_gate_cases_as_needs_attention(app):
|
||||
"""门禁 blocked_manual(如库存缺口)的已建任务单,队列必须下发 needs_attention=True。"""
|
||||
from app.models import ShippingProcessExecution, ShippingProcessGate
|
||||
|
||||
with app.app_context():
|
||||
user, agent, session, intake, document = _seed_run_input(suffix='REVIEW-QUEUE-NEEDS-ATTN')
|
||||
run = _materialized_run(user=user, agent=agent, session=session, intake=intake)
|
||||
candidate = ShippingOrderCandidate(
|
||||
candidate_no='CAND-REVIEW-QUEUE-NEEDS-ATTN',
|
||||
intake_id=intake.id,
|
||||
primary_document_id=document.id,
|
||||
candidate_key='review-queue-needs-attn',
|
||||
order_extraction_run_id=run.id,
|
||||
po_number='PO-QUEUE-ATTN',
|
||||
)
|
||||
db.session.add(candidate)
|
||||
db.session.flush()
|
||||
case = ensure_review_case_for_unit(run=run, unit=run.extraction_plan.units[0])
|
||||
task = ShippingOrderTask(
|
||||
task_no='OT-REVIEW-QUEUE-NEEDS-ATTN',
|
||||
user_id=user.id,
|
||||
conversation_id=session.session_id,
|
||||
extraction_run_id=run.id,
|
||||
status='processing',
|
||||
stage='inventory_preparation',
|
||||
)
|
||||
db.session.add(task)
|
||||
db.session.flush()
|
||||
execution = ShippingProcessExecution(
|
||||
execution_no='SPE-REVIEW-QUEUE-NEEDS-ATTN',
|
||||
task_id=task.id,
|
||||
review_case_id=case.id,
|
||||
status='active',
|
||||
current_gate_code='inventory_sufficiency',
|
||||
)
|
||||
db.session.add(execution)
|
||||
db.session.flush()
|
||||
db.session.add(ShippingProcessGate(
|
||||
execution_id=execution.id,
|
||||
gate_code='inventory_sufficiency',
|
||||
sequence_no=16,
|
||||
status='blocked_manual',
|
||||
))
|
||||
task.active_process_execution_id = execution.id
|
||||
case.status = 'confirmed'
|
||||
case.confirmed_task_id = task.id
|
||||
db.session.commit()
|
||||
|
||||
queue = serialize_review_queue([run])
|
||||
|
||||
# 关键断言:库存缺口类门禁阻塞必须在队列可见,不再只依赖当前单的完整详情。
|
||||
assert queue['items'][0]['needs_attention'] is True
|
||||
assert queue['items'][0]['lifecycle_stage'] == 'fulfillment_prep'
|
||||
# 关键断言:needs_attention 抬升 summary needs_action 桶,不得被计成完成/在办。
|
||||
assert queue['summary'] == {'total': 1, 'needs_action': 1, 'processing': 0, 'completed': 0}
|
||||
|
||||
|
||||
def test_review_queue_waiting_external_gate_is_not_needs_attention(app):
|
||||
"""RPA 外部等待(waiting_external)属于系统在办,不得标记 needs_attention。"""
|
||||
from app.models import ShippingProcessExecution, ShippingProcessGate
|
||||
|
||||
with app.app_context():
|
||||
user, agent, session, intake, document = _seed_run_input(suffix='REVIEW-QUEUE-WAIT-EXT')
|
||||
run = _materialized_run(user=user, agent=agent, session=session, intake=intake)
|
||||
candidate = ShippingOrderCandidate(
|
||||
candidate_no='CAND-REVIEW-QUEUE-WAIT-EXT',
|
||||
intake_id=intake.id,
|
||||
primary_document_id=document.id,
|
||||
candidate_key='review-queue-wait-ext',
|
||||
order_extraction_run_id=run.id,
|
||||
po_number='PO-QUEUE-WAIT',
|
||||
)
|
||||
db.session.add(candidate)
|
||||
db.session.flush()
|
||||
case = ensure_review_case_for_unit(run=run, unit=run.extraction_plan.units[0])
|
||||
task = ShippingOrderTask(
|
||||
task_no='OT-REVIEW-QUEUE-WAIT-EXT',
|
||||
user_id=user.id,
|
||||
conversation_id=session.session_id,
|
||||
extraction_run_id=run.id,
|
||||
status='processing',
|
||||
stage='freight_uploading',
|
||||
)
|
||||
db.session.add(task)
|
||||
db.session.flush()
|
||||
execution = ShippingProcessExecution(
|
||||
execution_no='SPE-REVIEW-QUEUE-WAIT-EXT',
|
||||
task_id=task.id,
|
||||
review_case_id=case.id,
|
||||
status='waiting_external',
|
||||
current_gate_code='freight_upload_rpa',
|
||||
)
|
||||
db.session.add(execution)
|
||||
db.session.flush()
|
||||
db.session.add(ShippingProcessGate(
|
||||
execution_id=execution.id,
|
||||
gate_code='freight_upload_rpa',
|
||||
sequence_no=18,
|
||||
status='waiting_external',
|
||||
))
|
||||
task.active_process_execution_id = execution.id
|
||||
case.status = 'confirmed'
|
||||
case.confirmed_task_id = task.id
|
||||
db.session.commit()
|
||||
|
||||
item = serialize_review_queue([run])['items'][0]
|
||||
|
||||
# 关键断言:外部等待是在办不是待办,图标应走进行中而非需人工。
|
||||
assert item['needs_attention'] is False
|
||||
|
||||
|
||||
def test_review_queue_without_execution_is_not_needs_attention(app):
|
||||
"""已建任务但尚无 Execution(或缓存未命中)时,needs_attention 必须为 False。"""
|
||||
with app.app_context():
|
||||
user, agent, session, intake, document = _seed_run_input(suffix='REVIEW-QUEUE-NO-EXEC')
|
||||
run = _materialized_run(user=user, agent=agent, session=session, intake=intake)
|
||||
candidate = ShippingOrderCandidate(
|
||||
candidate_no='CAND-REVIEW-QUEUE-NO-EXEC',
|
||||
intake_id=intake.id,
|
||||
primary_document_id=document.id,
|
||||
candidate_key='review-queue-no-exec',
|
||||
order_extraction_run_id=run.id,
|
||||
po_number='PO-QUEUE-NOEXEC',
|
||||
)
|
||||
db.session.add(candidate)
|
||||
db.session.flush()
|
||||
case = ensure_review_case_for_unit(run=run, unit=run.extraction_plan.units[0])
|
||||
task = ShippingOrderTask(
|
||||
task_no='OT-REVIEW-QUEUE-NO-EXEC',
|
||||
user_id=user.id,
|
||||
conversation_id=session.session_id,
|
||||
extraction_run_id=run.id,
|
||||
status='processing',
|
||||
stage='inventory_preparation',
|
||||
)
|
||||
db.session.add(task)
|
||||
db.session.flush()
|
||||
case.status = 'confirmed'
|
||||
case.confirmed_task_id = task.id
|
||||
db.session.commit()
|
||||
|
||||
item = serialize_review_queue([run])['items'][0]
|
||||
|
||||
# 关键断言:无门禁事实时不臆造需人工标志。
|
||||
assert item['needs_attention'] is False
|
||||
|
||||
|
||||
def test_review_queue_summary_counts_terminal_stage_as_completed(app):
|
||||
"""OT.stage=completed(通知完成)的已建任务单计入 completed,不再计入 processing。"""
|
||||
with app.app_context():
|
||||
user, agent, session, intake, document = _seed_run_input(suffix='REVIEW-QUEUE-SUM-DONE')
|
||||
run = _materialized_run(user=user, agent=agent, session=session, intake=intake)
|
||||
candidate = ShippingOrderCandidate(
|
||||
candidate_no='CAND-REVIEW-QUEUE-SUM-DONE',
|
||||
intake_id=intake.id,
|
||||
primary_document_id=document.id,
|
||||
candidate_key='review-queue-sum-done',
|
||||
order_extraction_run_id=run.id,
|
||||
po_number='PO-QUEUE-SUMDONE',
|
||||
)
|
||||
db.session.add(candidate)
|
||||
db.session.flush()
|
||||
case = ensure_review_case_for_unit(run=run, unit=run.extraction_plan.units[0])
|
||||
task = ShippingOrderTask(
|
||||
task_no='OT-REVIEW-QUEUE-SUM-DONE',
|
||||
user_id=user.id,
|
||||
conversation_id=session.session_id,
|
||||
extraction_run_id=run.id,
|
||||
status='processing',
|
||||
stage='completed',
|
||||
)
|
||||
db.session.add(task)
|
||||
db.session.flush()
|
||||
case.status = 'confirmed'
|
||||
case.confirmed_task_id = task.id
|
||||
db.session.commit()
|
||||
|
||||
summary = serialize_review_queue([run])['summary']
|
||||
|
||||
# 关键断言:summary 与五态图标同口径——confirmed+终态=completed 而非 processing。
|
||||
assert summary['total'] == 1
|
||||
assert summary['completed'] == 1
|
||||
assert summary['processing'] == 0
|
||||
assert summary['needs_action'] == 0
|
||||
|
||||
|
||||
def test_review_queue_summary_counts_quality_issue_as_needs_action(app):
|
||||
"""quality_issue(文件质量异常)从 completed 改归 needs_action,与黄⚠图标一致。"""
|
||||
with app.app_context():
|
||||
user, agent, session, intake, document = _seed_run_input(suffix='REVIEW-QUEUE-SUM-QI')
|
||||
run = _materialized_run(user=user, agent=agent, session=session, intake=intake)
|
||||
candidate = ShippingOrderCandidate(
|
||||
candidate_no='CAND-REVIEW-QUEUE-SUM-QI',
|
||||
intake_id=intake.id,
|
||||
primary_document_id=document.id,
|
||||
candidate_key='review-queue-sum-qi',
|
||||
order_extraction_run_id=run.id,
|
||||
po_number='PO-QUEUE-SUMQI',
|
||||
)
|
||||
db.session.add(candidate)
|
||||
db.session.flush()
|
||||
case = ensure_review_case_for_unit(run=run, unit=run.extraction_plan.units[0])
|
||||
case.status = 'quality_issue'
|
||||
db.session.commit()
|
||||
|
||||
summary = serialize_review_queue([run])['summary']
|
||||
|
||||
# 关键断言:质量异常是待办不是完成。
|
||||
assert summary['needs_action'] == 1
|
||||
assert summary['completed'] == 0
|
||||
assert summary['processing'] == 0
|
||||
|
||||
|
||||
def test_review_queue_summary_counts_plain_confirmed_as_processing(app):
|
||||
"""已建任务、无阻塞、非终态的 confirmed 单计入 processing(对应转圈图标)。"""
|
||||
with app.app_context():
|
||||
user, agent, session, intake, document = _seed_run_input(suffix='REVIEW-QUEUE-SUM-PROC')
|
||||
run = _materialized_run(user=user, agent=agent, session=session, intake=intake)
|
||||
candidate = ShippingOrderCandidate(
|
||||
candidate_no='CAND-REVIEW-QUEUE-SUM-PROC',
|
||||
intake_id=intake.id,
|
||||
primary_document_id=document.id,
|
||||
candidate_key='review-queue-sum-proc',
|
||||
order_extraction_run_id=run.id,
|
||||
po_number='PO-QUEUE-SUMPROC',
|
||||
)
|
||||
db.session.add(candidate)
|
||||
db.session.flush()
|
||||
case = ensure_review_case_for_unit(run=run, unit=run.extraction_plan.units[0])
|
||||
task = ShippingOrderTask(
|
||||
task_no='OT-REVIEW-QUEUE-SUM-PROC',
|
||||
user_id=user.id,
|
||||
conversation_id=session.session_id,
|
||||
extraction_run_id=run.id,
|
||||
status='processing',
|
||||
stage='inventory_preparation',
|
||||
)
|
||||
db.session.add(task)
|
||||
db.session.flush()
|
||||
case.status = 'confirmed'
|
||||
case.confirmed_task_id = task.id
|
||||
db.session.commit()
|
||||
|
||||
summary = serialize_review_queue([run])['summary']
|
||||
|
||||
# 关键断言:已创建任务是在办不是完成。
|
||||
assert summary['processing'] == 1
|
||||
assert summary['completed'] == 0
|
||||
assert summary['needs_action'] == 0
|
||||
Reference in new issue
Block a user