110 lines
4.0 KiB
Python
110 lines
4.0 KiB
Python
"""shipping_notice batch tests.
|
|
|
|
覆盖 batch 新增能力的最小闭环:
|
|
1. 单/多通知可通过 create_batch 一次建批并生成多 task。
|
|
2. get_batch_status 返回 focus task 与任务列表。
|
|
3. switch_focus_task 可在同批次内切换当前任务。
|
|
"""
|
|
|
|
from app.models import ShippingBatch, ShippingTask
|
|
|
|
|
|
def _tool_headers():
|
|
return {
|
|
"Content-Type": "application/json",
|
|
}
|
|
|
|
|
|
def test_create_batch_tasks_and_switch_focus(app, client):
|
|
"""验证 batch 建单、状态查询、focus task 切换主流程。"""
|
|
headers = _tool_headers()
|
|
|
|
create_res = client.post(
|
|
"/api/shipping-notice/tools/create_batch",
|
|
headers=headers,
|
|
json={
|
|
"source_type": "manual",
|
|
"notice_items": [
|
|
{
|
|
"source_file_name": "notice-a.txt",
|
|
"raw_text": "客户:ACME 料号A001 x 10",
|
|
"structured": {
|
|
"customer_name": "ACME",
|
|
"lines": [{"sku": "A001", "reservation_id": "R-1", "qty": 10, "uom": "EA"}],
|
|
"confidence": 0.95,
|
|
},
|
|
},
|
|
{
|
|
"source_file_name": "notice-b.txt",
|
|
"raw_text": "客户:Wistron 料号B002 x 5",
|
|
"structured": {
|
|
"customer_name": "Wistron",
|
|
"lines": [{"sku": "B002", "reservation_id": "R-2", "qty": 5, "uom": "EA"}],
|
|
"confidence": 0.95,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
)
|
|
assert create_res.status_code == 200
|
|
create_data = create_res.get_json()
|
|
assert create_data["status"] == "ok"
|
|
batch = create_data["data"]["batch"]
|
|
tasks = create_data["data"]["tasks"]
|
|
batch_no = batch["batch_no"]
|
|
assert batch_no.startswith("SB-")
|
|
assert len(tasks) == 2
|
|
assert batch["current_focus_task_no"] == tasks[0]["task_id"]
|
|
assert batch["process_summary"]["total"] == 2
|
|
|
|
status_res = client.post(
|
|
"/api/shipping-notice/tools/get_batch_status",
|
|
headers=headers,
|
|
json={"batch_no": batch_no},
|
|
)
|
|
assert status_res.status_code == 200
|
|
status_data = status_res.get_json()
|
|
assert status_data["status"] == "ok"
|
|
assert status_data["data"]["batch_no"] == batch_no
|
|
assert len(status_data["data"]["tasks"]) == 2
|
|
assert status_data["data"]["focus_task"] is None
|
|
assert status_data["data"]["tasks"][0]["task_no"] == tasks[0]["task_id"]
|
|
|
|
target_task_no = tasks[1]["task_id"]
|
|
switch_res = client.post(
|
|
"/api/shipping-notice/tools/switch_focus_task",
|
|
headers=headers,
|
|
json={"batch_no": batch_no, "task_no": target_task_no},
|
|
)
|
|
assert switch_res.status_code == 200
|
|
switch_data = switch_res.get_json()
|
|
assert switch_data["status"] == "ok"
|
|
assert switch_data["task_no"] == target_task_no
|
|
assert switch_data["data"]["batch"]["current_focus_task_no"] == target_task_no
|
|
assert switch_data["data"]["focus_task"]["task_no"] == target_task_no
|
|
|
|
with app.app_context():
|
|
batch_row = ShippingBatch.query.filter_by(batch_no=batch_no).first()
|
|
task_rows = ShippingTask.query.filter_by(batch_id=batch_row.id).order_by(ShippingTask.batch_index.asc()).all()
|
|
assert batch_row is not None
|
|
assert len(task_rows) == 2
|
|
assert batch_row.current_focus_task_no == target_task_no
|
|
assert task_rows[0].batch_total == 2
|
|
assert task_rows[1].batch_total == 2
|
|
|
|
|
|
def test_create_empty_batch_returns_batch_shell(app, client):
|
|
"""验证无 notice_items 时仍可创建批次壳,供后续续办。"""
|
|
headers = _tool_headers()
|
|
|
|
res = client.post(
|
|
"/api/shipping-notice/tools/create_batch",
|
|
headers=headers,
|
|
json={"file_count": 1},
|
|
)
|
|
assert res.status_code == 200
|
|
data = res.get_json()
|
|
assert data["status"] == "ok"
|
|
assert data["batch_no"].startswith("SB-")
|
|
assert data["data"]["file_count"] == 1
|