- 编辑闸门翻转为状态制:Excel 生成前+无活动 Attempt 即可编辑,原因不强制(审计自动记录操作人与变更) - 修复 Decimal 字段人工编辑无法持久化(单字段+整行替换两路径) - 预约注册与核验比对改读有效订单值视图,消除编辑后重匹配 RESERVATION_MISMATCH 阻断 - 通知准备值列改读有效视图:保存即显新值,匹配结果列保持最近一轮事实 - 行编辑入口为更多操作列编辑按钮;编辑态替换为保存/取消;库存源行内改选随保存统一提交 - 展示态动作收纳进更多菜单(Teleport 浮层,防表格 overflow 裁剪) - 修复缺口复核投影键错位(has_shortage 恒 False 致入口隐藏)并补齐行级结案入口 - 缺口放行改行内补录条(菜单放行或缺 N 项徽标展开,确认放行/取消收起),放行原因可选 - i18n 四语言;technical.md/funclist.md 语义文档同步
218 lines
10 KiB
Python
218 lines
10 KiB
Python
"""
|
|
tests/test_effective_order_view.py
|
|
|
|
用途:验证「冻结快照 + 冻结后人工修正」的有效订单值视图。
|
|
依赖:pytest、effective_order_view、task_order_field_editor、订单 Schema/快照模型。
|
|
逻辑:覆盖行级修正对位叠加、快照不可变、整行替换后的保守跳过与 Notice 行事实投影。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from decimal import Decimal
|
|
from types import SimpleNamespace
|
|
|
|
from app import db
|
|
from app.models import (
|
|
ShippingNotice,
|
|
ShippingOrderExtraction,
|
|
ShippingOrderFieldDecision,
|
|
ShippingOrderFieldValue,
|
|
ShippingOrderLine,
|
|
ShippingOrderSchema,
|
|
ShippingOrderSchemaVersion,
|
|
ShippingOrderSnapshot,
|
|
ShippingOrderSnapshotLine,
|
|
ShippingOrderTask,
|
|
User,
|
|
)
|
|
from app.services.shipping_notice.effective_order_view import (
|
|
effective_line_facts_for_notice,
|
|
effective_order_field_values,
|
|
)
|
|
from app.services.shipping_notice.order_field_purposes import task_line_purpose_values
|
|
from app.services.shipping_notice.task_order_field_editor import (
|
|
replace_current_line_field_value,
|
|
replace_current_line_fields,
|
|
)
|
|
|
|
|
|
def _seed_frozen_order():
|
|
"""最小冻结订单夹具:OT + accepted Extraction(两行三字段)+ 冻结快照。"""
|
|
user = User(username='eff-view-user', email='eff-view@test.local', role='user', status='active')
|
|
db.session.add(user)
|
|
schema = ShippingOrderSchema(schema_code='EFFVIEW', display_name='EffView')
|
|
db.session.add(schema)
|
|
db.session.flush()
|
|
version = ShippingOrderSchemaVersion(
|
|
schema_id=schema.id, version=1, status='published',
|
|
definition_json={'fields': [
|
|
{'field_id': 'f-part', 'field_code': 'part_number', 'scope': 'line', 'value_type': 'string', 'field_category': 'order_line', 'sequence': 1, 'enabled': True},
|
|
{'field_id': 'f-rsv', 'field_code': 'reservation_id', 'scope': 'line', 'value_type': 'string', 'field_category': 'order_line', 'sequence': 2, 'enabled': True},
|
|
{'field_id': 'f-qty', 'field_code': 'qty', 'scope': 'line', 'value_type': 'decimal', 'field_category': 'order_line', 'sequence': 3, 'enabled': True},
|
|
]},
|
|
)
|
|
db.session.add(version)
|
|
db.session.flush()
|
|
order = ShippingOrderTask(task_no='OT-EFFVIEW-1', source_type='direct_pdf', user_id=user.id)
|
|
db.session.add(order)
|
|
db.session.flush()
|
|
extraction = ShippingOrderExtraction(
|
|
task_id=order.id, schema_version_id=version.id, input_snapshot_hash='a' * 64, status='accepted',
|
|
)
|
|
db.session.add(extraction)
|
|
db.session.flush()
|
|
snapshot = ShippingOrderSnapshot(
|
|
task_id=order.id, extraction_id=extraction.id, schema_version_id=version.id,
|
|
order_data_version=1,
|
|
rule_versions_json={'fulfillment_rule': {'required_field_ids': {
|
|
'part_number': 'f-part', 'reservation_id': 'f-rsv', 'qty': 'f-qty',
|
|
}}},
|
|
header_values_json={},
|
|
)
|
|
db.session.add(snapshot)
|
|
db.session.flush()
|
|
snap_lines = []
|
|
for position, part in enumerate(('D4-A08AR12000002-', 'D4-B07BR07000001')):
|
|
line = ShippingOrderLine(extraction_id=extraction.id, position=position, line_no=position + 1)
|
|
db.session.add(line)
|
|
db.session.flush()
|
|
for field_id, value in (('f-part', part), ('f-rsv', f'RSV-{position + 1}'), ('f-qty', '10')):
|
|
decision = ShippingOrderFieldDecision(
|
|
extraction_id=extraction.id, line_id=line.id, field_id=field_id,
|
|
decision_status='accepted', resolved_value_json=value,
|
|
)
|
|
db.session.add(decision)
|
|
db.session.flush()
|
|
db.session.add(ShippingOrderFieldValue(
|
|
extraction_id=extraction.id, line_id=line.id, field_id=field_id, decision_id=decision.id,
|
|
value_text=str(value), value_json=value,
|
|
))
|
|
snap_line = ShippingOrderSnapshotLine(
|
|
snapshot_id=snapshot.id, position=position, line_no=position + 1,
|
|
part_number=part, reservation_id=f'RSV-{position + 1}', qty=Decimal('10'),
|
|
field_values_json={'f-part': part, 'f-rsv': f'RSV-{position + 1}', 'f-qty': '10'},
|
|
)
|
|
db.session.add(snap_line)
|
|
db.session.flush()
|
|
snap_lines.append(snap_line)
|
|
order.current_order_extraction_id = extraction.id
|
|
order.order_snapshot_id = snapshot.id
|
|
db.session.flush()
|
|
return SimpleNamespace(user=user, order=order, extraction=extraction, snapshot=snapshot, snap_lines=snap_lines)
|
|
|
|
|
|
def test_edited_part_number_flows_into_effective_values(app):
|
|
"""冻结后人工修正料号,有效视图必须返回修正值(匹配/Excel 的读取口径)。"""
|
|
with app.app_context():
|
|
fixture = _seed_frozen_order()
|
|
replace_current_line_field_value(
|
|
task=fixture.order, position=0, field_code='part_number', value='D4-A08AR12000002',
|
|
)
|
|
_header, lines = effective_order_field_values(fixture.order)
|
|
# 断言:第 0 行为修正值、第 1 行保持冻结值,证明对位叠加只影响被改行。
|
|
assert lines[0]['f-part'] == 'D4-A08AR12000002'
|
|
assert lines[1]['f-part'] == 'D4-B07BR07000001'
|
|
|
|
|
|
def test_snapshot_stays_frozen_after_edit(app):
|
|
"""有效视图不得回写快照:冻结事实仍是审计锚点。"""
|
|
with app.app_context():
|
|
fixture = _seed_frozen_order()
|
|
replace_current_line_field_value(
|
|
task=fixture.order, position=0, field_code='part_number', value='D4-A08AR12000002',
|
|
)
|
|
# 断言:快照强类型列与 field_values_json 均保持冻结原值。
|
|
assert fixture.snap_lines[0].part_number == 'D4-A08AR12000002-'
|
|
assert fixture.snap_lines[0].field_values_json['f-part'] == 'D4-A08AR12000002-'
|
|
|
|
|
|
def test_line_set_replacement_skips_line_overlay(app):
|
|
"""整行替换后 position 不再可比:行级叠加保守跳过,返回冻结行值。"""
|
|
with app.app_context():
|
|
fixture = _seed_frozen_order()
|
|
# 注:替换行不带 qty——本测试只验证行集替换守卫;decimal 经整行替换路径的可持久化
|
|
# 已由 test_line_set_replacement_with_decimal_persists_without_json_error 单独覆盖。
|
|
replace_current_line_fields(task=fixture.order, lines=[
|
|
{'part_number': 'D4-A08AR12000002', 'reservation_id': 'RSV-1'},
|
|
])
|
|
_header, lines = effective_order_field_values(fixture.order)
|
|
# 断言:行级叠加被守卫跳过(行集维度属后续特性),值仍来自冻结快照两行原样。
|
|
assert [row['f-part'] for row in lines] == ['D4-A08AR12000002-', 'D4-B07BR07000001']
|
|
|
|
|
|
def test_effective_line_facts_for_notice(app):
|
|
"""Notice 行物化事实按冻结规则映射强类型键并携带修正值。"""
|
|
with app.app_context():
|
|
fixture = _seed_frozen_order()
|
|
replace_current_line_field_value(
|
|
task=fixture.order, position=0, field_code='part_number', value='D4-A08AR12000002',
|
|
)
|
|
facts = effective_line_facts_for_notice(fixture.order)
|
|
first = facts[int(fixture.snap_lines[0].id)]
|
|
# 断言:料号为修正值,数量被安全转为 Decimal,字段值字典完整。
|
|
assert first['part_number'] == 'D4-A08AR12000002'
|
|
assert first['reservation_id'] == 'RSV-1'
|
|
assert first['qty'] == Decimal('10')
|
|
assert first['field_values']['f-rsv'] == 'RSV-1'
|
|
|
|
|
|
def test_decimal_edit_persists_without_json_error(app):
|
|
"""数量(decimal 字段)人工编辑必须可持久化:value_json 存字符串、value_decimal 存 Decimal。"""
|
|
with app.app_context():
|
|
fixture = _seed_frozen_order()
|
|
replace_current_line_field_value(
|
|
task=fixture.order, position=0, field_code='qty', value='12.5',
|
|
)
|
|
rows = [
|
|
item for item in ShippingOrderFieldValue.query.filter_by(
|
|
extraction_id=fixture.extraction.id,
|
|
).all()
|
|
if item.value_json is not None
|
|
]
|
|
# 夹具两行均预置了 f-qty FieldValue(未编辑行 value_json='10'),须限定被编辑的第 0 行再断言。
|
|
edited_line = ShippingOrderLine.query.filter_by(
|
|
extraction_id=fixture.extraction.id, position=0,
|
|
).one()
|
|
qty_rows = [item for item in rows if item.field_id == 'f-qty' and item.line_id == edited_line.id]
|
|
# 断言:JSON 列存规范字符串(与机器路径 _json_value 对齐),类型列保留 Decimal。
|
|
assert qty_rows, 'qty FieldValue 必须存在'
|
|
assert all(item.value_json == '12.5' for item in qty_rows)
|
|
assert any(item.value_decimal == Decimal('12.5') for item in qty_rows)
|
|
|
|
|
|
def test_line_set_replacement_with_decimal_persists_without_json_error(app):
|
|
"""整行替换带 decimal 字段必须同样可持久化:新当前 Extraction 的 qty JSON 列存字符串、类型列存 Decimal。"""
|
|
with app.app_context():
|
|
fixture = _seed_frozen_order()
|
|
replace_current_line_fields(task=fixture.order, lines=[
|
|
{'part_number': 'D4-A08AR12000002', 'reservation_id': 'RSV-1', 'qty': '12.5'},
|
|
])
|
|
rows = ShippingOrderFieldValue.query.filter_by(
|
|
extraction_id=fixture.order.current_order_extraction_id,
|
|
field_id='f-qty',
|
|
).all()
|
|
line_qty_rows = [item for item in rows if item.line_id is not None]
|
|
# 断言:整行替换路径的 Decision/FieldValue 均经 _json_value 落库且不得抛序列化错误。
|
|
assert line_qty_rows, '替换行集的 qty FieldValue 必须存在'
|
|
assert all(item.value_json == '12.5' for item in line_qty_rows)
|
|
assert all(item.value_decimal == Decimal('12.5') for item in line_qty_rows)
|
|
|
|
|
|
def test_matching_purpose_values_use_effective_view(app):
|
|
"""库存匹配用途投影必须读到修正后的料号(修复改值不进匹配的断层)。"""
|
|
with app.app_context():
|
|
fixture = _seed_frozen_order()
|
|
replace_current_line_field_value(
|
|
task=fixture.order, position=0, field_code='part_number', value='D4-A08AR12000002',
|
|
)
|
|
notice = ShippingNotice(
|
|
task_no='SN-EFFVIEW-1', source_type='manual', user_id=fixture.user.id,
|
|
order_task_id=fixture.order.id,
|
|
)
|
|
db.session.add(notice)
|
|
db.session.flush()
|
|
purpose_values = task_line_purpose_values(notice)
|
|
# 断言:第 0 行用途值为修正料号,第 1 行保持冻结值。
|
|
assert purpose_values[0]['part_number'] == 'D4-A08AR12000002'
|
|
assert purpose_values[1]['part_number'] == 'D4-B07BR07000001'
|