- 将 RPA 管理 API 扩展至出站报表配置,新增出站相关的回调接口。 - 重构 freight_rpa_report_config_service,支持入站与出站报表配置的管理。 - 更新 ORM 模型,增加出站报表相关的持久化模型。 - 统一命名规范,确保在手、入站与出站的概念一致性。
107 lines
5.5 KiB
Python
107 lines
5.5 KiB
Python
"""
|
|
tests/test_inventory_profile_matching.py
|
|
|
|
用途:验证库存文件结构指纹与已发布解析方案的确定性复用推荐。
|
|
依赖:库存结构分析、库存解析方案、订单字段 Schema 服务。
|
|
逻辑:库存数据内容和文件名不参与判断;完全一致与兼容变化只产生管理员建议,绝不自动绑定库存站点。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from io import BytesIO
|
|
|
|
from openpyxl import Workbook
|
|
from werkzeug.datastructures import FileStorage
|
|
|
|
from app.services.freight_rpa import parsing_profile as onhand_parsing_profile
|
|
from app.services.shipping_notice import onhand_structure_analysis
|
|
from app.services.shipping_notice.order_schema import service as order_schema_service
|
|
from app.models import FreightRpaOnhandField
|
|
|
|
|
|
def _part_number_inventory_field_id() -> str:
|
|
"""返回测试用户创建的匹配字段 ID。"""
|
|
return str(FreightRpaOnhandField.query.filter_by(field_code='part_number').one().id)
|
|
|
|
|
|
def _upload(*, qty: int, extra_column: bool = False) -> FileStorage:
|
|
"""构造结构相同但库存数据不同的样本,可选增加无关栏位。"""
|
|
stream = BytesIO()
|
|
workbook = Workbook()
|
|
sheet = workbook.active
|
|
headers = ['Supplier Sku', 'Reservation ID', 'Qty Available']
|
|
if extra_column:
|
|
headers.append('Last Export Time')
|
|
sheet.append(headers)
|
|
row = ['PN-001', '1000140351', qty]
|
|
if extra_column:
|
|
row.append('2026-07-17 10:00')
|
|
sheet.append(row)
|
|
workbook.save(stream)
|
|
stream.seek(0)
|
|
return FileStorage(stream=stream, filename=f'inventory-{qty}.xlsx')
|
|
|
|
|
|
def _field_id() -> str:
|
|
"""创建当前已发布订单契约中的内部料号字段。"""
|
|
order_schema_service.initialize_schema({'display_name': 'Matching'}, actor_id=None)
|
|
field = order_schema_service.save_field({'field_code': 'part_number', 'scope': 'line', 'value_type': 'string'}, actor_id=None)
|
|
draft = order_schema_service.create_draft({}, actor_id=None)
|
|
saved = order_schema_service.save_draft(draft['id'], {'fields': [{
|
|
'field_id': field['id'], 'display_name': '内部料号', 'required': True, 'aliases': [],
|
|
'normalizer_key': 'trim', 'validator_keys': [], 'enum_options': [],
|
|
'evidence_policy': {'required': True, 'source_precedence': ['attachment_table'], 'conflict_policy': 'block', 'allow_single_source': True},
|
|
'relation_role': None, 'comparator_key': 'normalized_text', 'extraction_instruction': '提取内部料号',
|
|
}], 'expected_lock_version': draft['lock_version']})
|
|
order_schema_service.publish(draft['id'], actor_id=None, expected_lock_version=saved['lock_version'])
|
|
return field['id']
|
|
|
|
|
|
def _published_profile(field_id: str) -> dict:
|
|
"""通过一次真实分析创建并发布含结构指纹的库存文件解析方案。"""
|
|
analysis = onhand_structure_analysis.create_sample_upload(_upload(qty=9), actor_id=None)
|
|
draft = onhand_structure_analysis.create_profile_draft(analysis['id'], {
|
|
'profile_code': 'SHARED_INVENTORY', 'display_name': '共享库存文件', 'sheet_name': 'Sheet', 'header_row_index': 1,
|
|
'field_mapping': {
|
|
_part_number_inventory_field_id(): 'Supplier Sku',
|
|
str(FreightRpaOnhandField.query.filter_by(field_code='reservation_id').one().id): 'Reservation ID',
|
|
str(FreightRpaOnhandField.query.filter_by(field_code='available_qty').one().id): 'Qty Available',
|
|
},
|
|
'match_keys': [{'order_field_id': field_id, 'inventory_field_id': _part_number_inventory_field_id()}],
|
|
}, actor_id=None)
|
|
return onhand_parsing_profile.publish(draft['id'], actor_id=None)
|
|
|
|
|
|
def test_same_structure_with_different_data_recommends_exact_profile(app, tmp_path):
|
|
"""库存数量变化不影响模板判断,同表头结构必须推荐同一已发布方案。"""
|
|
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
|
with app.app_context():
|
|
profile = _published_profile(_field_id())
|
|
analysis = onhand_structure_analysis.create_sample_upload(_upload(qty=999), actor_id=None)
|
|
result = analysis['match_result']
|
|
assert result['status'] == 'exact'
|
|
assert result['candidates'][0]['profile_id'] == profile['id']
|
|
|
|
|
|
def test_added_non_critical_column_recommends_compatible_profile(app, tmp_path):
|
|
"""新增无关栏位可推荐已有方案,但不伪装为完全相同结构。"""
|
|
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
|
with app.app_context():
|
|
profile = _published_profile(_field_id())
|
|
analysis = onhand_structure_analysis.create_sample_upload(_upload(qty=12, extra_column=True), actor_id=None)
|
|
candidate = analysis['match_result']['candidates'][0]
|
|
assert analysis['match_result']['status'] == 'compatible'
|
|
assert candidate['profile_id'] == profile['id']
|
|
assert 'last export time' in candidate['difference']['added_columns']
|
|
|
|
|
|
def test_adopting_recommendation_does_not_bind_inventory_source(app, tmp_path):
|
|
"""管理员采用推荐方案只留下分析审计,不能在样本上传阶段修改任何库存站点。"""
|
|
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path / 'shipping')
|
|
with app.app_context():
|
|
profile = _published_profile(_field_id())
|
|
analysis = onhand_structure_analysis.create_sample_upload(_upload(qty=10), actor_id=None)
|
|
adopted = onhand_structure_analysis.adopt_published_profile(analysis['id'], profile['id'])
|
|
assert adopted['match_result']['adopted_profile_id'] == profile['id']
|
|
assert profile['source_count'] == 0
|