feat(shipping-notice): 落地字段资产治理与唯一 Schema,自动建单随版本冻结
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
b7307fc573
commit
3a1dbf7ce5
144 files changed
+15167
-4480
No files matched your search
@@ -3,7 +3,8 @@ tests/test_shipping_fulfillment_policy.py
|
||||
|
||||
用途:验证货代字段能力绑定与版本化履约策略。
|
||||
依赖:订单 Schema 服务、履约策略注册表与测试数据库。
|
||||
逻辑:策略只允许受控能力绑定稳定 field_id;发布后冻结保存版本快照,错误类型或范围必须阻断。
|
||||
逻辑:策略只允许受控能力绑定稳定 field_id;发布后冻结保存版本快照,错误类型或范围必须阻断;
|
||||
无已发布 Schema 时不得发布策略。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -11,7 +12,7 @@ from __future__ import annotations
|
||||
import pytest
|
||||
|
||||
from app import db
|
||||
from app.core.errors import ValidationError
|
||||
from app.core.errors import ConflictError, ValidationError
|
||||
from app.models import ShippingFulfillmentPolicy, ShippingOrderField
|
||||
from app.services.shipping_notice.fulfillment_policy_registry import create_draft, get_state, publish
|
||||
from app.services.shipping_notice.order_schema import service as schema_service
|
||||
@@ -42,8 +43,24 @@ def _field_config(field_id: str, *, display_name: str, value_type: str = 'string
|
||||
}
|
||||
|
||||
|
||||
def _publish_minimal_schema() -> None:
|
||||
"""发布含履约绑定字段的最小 Schema,供策略发布门禁使用。"""
|
||||
fields = {item.field_code: item for item in ShippingOrderField.query.filter_by(status='active').all()}
|
||||
draft = schema_service.create_draft({}, actor_id=None)
|
||||
configs = [
|
||||
_field_config(fields['reservation_id'].id, display_name='预定单号'),
|
||||
_field_config(fields['part_number'].id, display_name='料号'),
|
||||
_field_config(fields['qty'].id, display_name='数量', value_type='decimal'),
|
||||
_field_config(fields['inventory_source_hint'].id, display_name='库存源线索'),
|
||||
]
|
||||
saved = schema_service.save_draft(draft['id'], {
|
||||
'fields': configs, 'expected_lock_version': draft['lock_version'],
|
||||
}, actor_id=None)
|
||||
schema_service.publish(draft['id'], actor_id=None, expected_lock_version=saved['lock_version'])
|
||||
|
||||
|
||||
def test_policy_initialization_and_publish_versions_are_isolated(app):
|
||||
"""初始化只创建待确认草稿;发布后新版本退役旧版且保留稳定字段绑定。"""
|
||||
"""初始化只创建待确认草稿;有已发布 Schema 后,发布策略会退役旧版且保留稳定字段绑定。"""
|
||||
with app.app_context():
|
||||
schema_service.initialize_schema({'display_name': '货代字段'}, actor_id=None)
|
||||
initial = ShippingFulfillmentPolicy.query.filter_by(status='draft').one()
|
||||
@@ -52,6 +69,7 @@ def test_policy_initialization_and_publish_versions_are_isolated(app):
|
||||
'fulfillment.reservation_identity', 'fulfillment.part_number', 'fulfillment.quantity',
|
||||
}
|
||||
|
||||
_publish_minimal_schema()
|
||||
initial = publish(initial.id, actor_id=None, expected_lock_version=initial.lock_version)
|
||||
assert initial['status'] == 'published'
|
||||
|
||||
@@ -63,6 +81,16 @@ def test_policy_initialization_and_publish_versions_are_isolated(app):
|
||||
assert len(get_state()['capabilities']) == 4
|
||||
|
||||
|
||||
def test_policy_publish_requires_published_schema(app):
|
||||
"""断言:同族尚无已发布 Schema 时,不得发布履约策略。"""
|
||||
with app.app_context():
|
||||
schema_service.initialize_schema({'display_name': '货代字段'}, actor_id=None)
|
||||
initial = ShippingFulfillmentPolicy.query.filter_by(status='draft').one()
|
||||
with pytest.raises(ConflictError) as exc_info:
|
||||
publish(initial.id, actor_id=None, expected_lock_version=initial.lock_version)
|
||||
assert exc_info.value.code == 'SHIPPING.FULFILLMENT_POLICY_SCHEMA_REQUIRED'
|
||||
|
||||
|
||||
def test_policy_rejects_scope_or_type_drift(app):
|
||||
"""能力绑定只能选择匹配 scope/value_type 的字段,不能借字段目录绕过履约契约。"""
|
||||
with app.app_context():
|
||||
@@ -76,22 +104,20 @@ def test_policy_rejects_scope_or_type_drift(app):
|
||||
assert exc_info.value.code == 'SHIPPING.FULFILLMENT_POLICY_INVALID'
|
||||
|
||||
|
||||
def test_policy_allows_a_custom_field_to_bind_a_compatible_capability(app):
|
||||
"""自定义字段只要 scope/type 匹配,即可经策略承担履约能力而不依赖字段编码。"""
|
||||
def test_policy_rejects_extension_on_core_capability(app):
|
||||
"""核心履约能力不得绑定扩展字段;须使用系统标准表身字段。"""
|
||||
with app.app_context():
|
||||
schema_service.initialize_schema({'display_name': '货代字段'}, actor_id=None)
|
||||
custom = schema_service.save_field({
|
||||
'field_code': 'customer_reservation_reference', 'scope': 'line', 'value_type': 'string',
|
||||
'display_name': '客户预约单号',
|
||||
}, actor_id=None)
|
||||
bindings = _bindings()
|
||||
bindings[0]['field_id'] = custom['id']
|
||||
|
||||
draft = create_draft({'field_bindings': bindings, 'change_note': '客户预约单字段'}, actor_id=None)
|
||||
|
||||
reservation = next(item for item in draft['definition']['field_bindings']
|
||||
if item['capability_code'] == 'fulfillment.reservation_identity')
|
||||
assert reservation['field_id'] == custom['id']
|
||||
assert reservation['field_code'] == 'customer_reservation_reference'
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
create_draft({'field_bindings': bindings, 'change_note': '客户预约单字段'}, actor_id=None)
|
||||
assert exc_info.value.code == 'SHIPPING.FULFILLMENT_FIELD_INVALID'
|
||||
|
||||
|
||||
def test_policy_publish_rejects_an_active_field_omitted_from_published_schema(app):
|
||||
@@ -100,22 +126,17 @@ def test_policy_publish_rejects_an_active_field_omitted_from_published_schema(ap
|
||||
schema_service.initialize_schema({'display_name': '货代字段'}, actor_id=None)
|
||||
fields = {item.field_code: item for item in ShippingOrderField.query.filter_by(status='active').all()}
|
||||
draft = schema_service.create_draft({}, actor_id=None)
|
||||
# 故意不纳入 inventory_source_hint,策略绑定仍包含该字段时应被拒绝。
|
||||
configs = [
|
||||
_field_config(fields['reservation_id'].id, display_name='预定单号'),
|
||||
_field_config(fields['part_number'].id, display_name='料号'),
|
||||
_field_config(fields['qty'].id, display_name='数量', value_type='decimal'),
|
||||
_field_config(fields['inventory_source_hint'].id, display_name='库存源线索'),
|
||||
]
|
||||
saved = schema_service.save_draft(draft['id'], {
|
||||
'fields': configs, 'expected_lock_version': draft['lock_version'],
|
||||
}, actor_id=None)
|
||||
schema_service.publish(draft['id'], actor_id=None, expected_lock_version=saved['lock_version'])
|
||||
custom = schema_service.save_field({
|
||||
'field_code': 'unpublished_reservation_reference', 'scope': 'line', 'value_type': 'string',
|
||||
}, actor_id=None)
|
||||
bindings = _bindings()
|
||||
bindings[0]['field_id'] = custom['id']
|
||||
policy_draft = create_draft({'field_bindings': bindings}, actor_id=None)
|
||||
policy_draft = create_draft({'field_bindings': _bindings()}, actor_id=None)
|
||||
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
publish(policy_draft['id'], actor_id=None, expected_lock_version=policy_draft['lock_version'])
|
||||
@@ -148,4 +169,7 @@ def test_schema_publish_cannot_omit_a_field_bound_by_published_policy(app):
|
||||
|
||||
with pytest.raises(Exception) as exc_info:
|
||||
schema_service.publish(second['id'], actor_id=None, expected_lock_version=saved['lock_version'])
|
||||
assert getattr(exc_info.value, 'code', None) == 'SHIPPING.ORDER_SCHEMA_PUBLISH_DEPENDENCY_BLOCKED'
|
||||
assert getattr(exc_info.value, 'code', None) in {
|
||||
'SHIPPING.ORDER_SCHEMA_PUBLISH_DEPENDENCY_BLOCKED',
|
||||
'SHIPPING.FIELD_IMPACT_BLOCKED',
|
||||
}
|
||||
Reference in new issue
Block a user