feat(shipping-notice): 新增临时库存管理功能,支持创建会话、获取分析结果及字段映射建议
This commit is contained in:
1 parent
46eb63f5bb
commit
e9bdd81f1a
320 files changed
+28361
-10487
No files matched your search
@@ -1,7 +1,7 @@
|
||||
"""
|
||||
backend/tests/test_shipping_customer_auto_resolution.py
|
||||
|
||||
用途:验证受控自动客户确认、降级开关、强规则歧义和历史客户回填。
|
||||
用途:验证受控自动客户确认、降级开关、强规则歧义和编号后客户纠错。
|
||||
依赖:客户主数据/证据、Candidate 影子结果、ShippingTask/Case、customer_auto_resolution。
|
||||
逻辑:构造真实 ORM 证据后直接调用领域服务,确认仅唯一已审核强规则可自动编号。
|
||||
"""
|
||||
@@ -25,7 +25,6 @@ from app.models import (
|
||||
)
|
||||
from app.services.shipping_notice.customer_auto_resolution import (
|
||||
AUTO_RESOLUTION_VERSION,
|
||||
backfill_historical_task_customer,
|
||||
correct_numbered_task_customer,
|
||||
get_auto_resolution_metrics,
|
||||
try_auto_confirm_task_customer,
|
||||
@@ -100,10 +99,17 @@ def _customer_with_identifier(*, code: str, value: str, allowed: bool = True, st
|
||||
return customer
|
||||
|
||||
|
||||
def _allow_auto_confirmation(app) -> None:
|
||||
"""为自动确认专项用例显式放宽门禁,不改变生产默认安全阈值。"""
|
||||
app.config["SHIPPING_NOTICE_AUTO_CUSTOMER_CONFIRM_ENABLED"] = True
|
||||
app.config["SHIPPING_NOTICE_AUTO_CUSTOMER_CONFIRM_MIN_LABELED_SAMPLES"] = 0
|
||||
app.config["SHIPPING_NOTICE_AUTO_CUSTOMER_CONFIRM_MIN_ACCURACY"] = 0.98
|
||||
|
||||
|
||||
def test_unique_approved_strong_rule_auto_confirms_and_numbers(app):
|
||||
"""开关启用且唯一邮箱强规则命中时自动确认客户并生成业务号。"""
|
||||
with app.app_context():
|
||||
app.config["SHIPPING_NOTICE_AUTO_CUSTOMER_CONFIRM_ENABLED"] = True
|
||||
_allow_auto_confirmation(app)
|
||||
customer = _customer_with_identifier(code="WYC", value="buyer@wyc.example.com")
|
||||
task = _seed_candidate_task()
|
||||
|
||||
@@ -132,7 +138,7 @@ def test_switch_off_and_unapproved_rule_fall_back_to_manual(app):
|
||||
assert disabled["status"] == "disabled"
|
||||
assert task.shipping_case.business_no is None
|
||||
|
||||
app.config["SHIPPING_NOTICE_AUTO_CUSTOMER_CONFIRM_ENABLED"] = True
|
||||
_allow_auto_confirmation(app)
|
||||
unmatched = try_auto_confirm_task_customer(task)
|
||||
assert unmatched["status"] == "unmatched"
|
||||
assert task.customer_resolution_status == "pending"
|
||||
@@ -142,7 +148,7 @@ def test_switch_off_and_unapproved_rule_fall_back_to_manual(app):
|
||||
def test_multiple_customers_matching_strong_evidence_is_ambiguous(app):
|
||||
"""同一强证据映射多个客户时必须降级人工确认。"""
|
||||
with app.app_context():
|
||||
app.config["SHIPPING_NOTICE_AUTO_CUSTOMER_CONFIRM_ENABLED"] = True
|
||||
_allow_auto_confirmation(app)
|
||||
_customer_with_identifier(code="AAA", value="buyer@wyc.example.com")
|
||||
_customer_with_identifier(code="BBB", value="buyer@wyc.example.com")
|
||||
task = _seed_candidate_task()
|
||||
@@ -154,29 +160,10 @@ def test_multiple_customers_matching_strong_evidence_is_ambiguous(app):
|
||||
assert task.shipping_case.business_no is None
|
||||
|
||||
|
||||
def test_historical_backfill_binds_customer_without_numbering(app):
|
||||
"""历史任务回填客户后业务号仍为空,不倒占当日流水。"""
|
||||
with app.app_context():
|
||||
customer = ShippingCustomer(standard_name="历史客户", short_code="HIS", status="active")
|
||||
task = ShippingTask(task_no="SN-20200101-HISTORY", source_type="manual", process_status="closed")
|
||||
db.session.add_all([customer, task])
|
||||
db.session.commit()
|
||||
|
||||
result = backfill_historical_task_customer(task, customer_id=customer.id, actor_id=None)
|
||||
assert result["business_no"] is None
|
||||
assert task.shipping_case.customer_id == customer.id
|
||||
assert task.shipping_case.numbering_status == "historical_unnumbered"
|
||||
assert task.shipping_case.business_no is None
|
||||
assert task.customer_resolution_method == "historical_backfill"
|
||||
metrics = get_auto_resolution_metrics()
|
||||
assert metrics["historical_backfilled_count"] == 1
|
||||
assert metrics["recent_events"][0]["event_type"] == "customer_historical_backfilled"
|
||||
|
||||
|
||||
def test_auto_confirmation_can_be_rolled_back_by_outer_transaction(app):
|
||||
"""批次编排失败时,自动确认、编号和审计必须随外层事务整体回滚。"""
|
||||
with app.app_context():
|
||||
app.config["SHIPPING_NOTICE_AUTO_CUSTOMER_CONFIRM_ENABLED"] = True
|
||||
_allow_auto_confirmation(app)
|
||||
_customer_with_identifier(code="WYC", value="buyer@wyc.example.com")
|
||||
task = _seed_candidate_task()
|
||||
task_id = task.id
|
||||
@@ -195,6 +182,48 @@ def test_auto_confirmation_can_be_rolled_back_by_outer_transaction(app):
|
||||
assert ShippingTaskEvent.query.filter_by(event_type="customer_auto_confirmed").count() == 0
|
||||
|
||||
|
||||
def test_quality_gate_blocks_auto_confirmation_when_labeled_samples_are_insufficient(app):
|
||||
"""总开关开启但没有足够人工真值时,必须保持人工客户复核和未编号状态。"""
|
||||
with app.app_context():
|
||||
app.config["SHIPPING_NOTICE_AUTO_CUSTOMER_CONFIRM_ENABLED"] = True
|
||||
app.config["SHIPPING_NOTICE_AUTO_CUSTOMER_CONFIRM_MIN_LABELED_SAMPLES"] = 1
|
||||
app.config["SHIPPING_NOTICE_AUTO_CUSTOMER_CONFIRM_MIN_ACCURACY"] = 0.98
|
||||
_customer_with_identifier(code="WYC", value="buyer@wyc.example.com")
|
||||
task = _seed_candidate_task()
|
||||
|
||||
result = try_auto_confirm_task_customer(task)
|
||||
|
||||
assert result["status"] == "insufficient_labeled_samples"
|
||||
assert result["quality_gate"]["labeled_sample_count"] == 0
|
||||
assert task.customer_resolution_status == "pending"
|
||||
assert task.shipping_case.business_no is None
|
||||
|
||||
|
||||
def test_quality_gate_blocks_auto_confirmation_when_accuracy_is_insufficient(app):
|
||||
"""有足够样本但强规则与人工真值不一致时,自动确认必须安全降级。"""
|
||||
with app.app_context():
|
||||
app.config["SHIPPING_NOTICE_AUTO_CUSTOMER_CONFIRM_ENABLED"] = True
|
||||
app.config["SHIPPING_NOTICE_AUTO_CUSTOMER_CONFIRM_MIN_LABELED_SAMPLES"] = 1
|
||||
app.config["SHIPPING_NOTICE_AUTO_CUSTOMER_CONFIRM_MIN_ACCURACY"] = 0.98
|
||||
matched = _customer_with_identifier(code="WYC", value="buyer@wyc.example.com")
|
||||
truth = ShippingCustomer(standard_name="真实客户", short_code="TRU", status="active")
|
||||
db.session.add(truth)
|
||||
db.session.flush()
|
||||
task = _seed_candidate_task()
|
||||
shadow = ShippingCustomerShadowResolution.query.filter_by(candidate_id=task.source_candidate_id).one()
|
||||
shadow.manual_customer_id = truth.id
|
||||
db.session.commit()
|
||||
|
||||
result = try_auto_confirm_task_customer(task)
|
||||
|
||||
assert matched.id != truth.id
|
||||
assert result["status"] == "insufficient_accuracy"
|
||||
assert result["quality_gate"]["labeled_sample_count"] == 1
|
||||
assert result["quality_gate"]["accuracy"] == 0
|
||||
assert task.customer_resolution_status == "pending"
|
||||
assert task.shipping_case.business_no is None
|
||||
|
||||
|
||||
def test_numbered_customer_correction_voids_old_number_and_replaces_case(app):
|
||||
"""客户误确认纠错必须保留原号作废,生成新客户编号并让原号查询跟随替代单。"""
|
||||
with app.app_context():
|
||||
|
||||
Reference in new issue
Block a user