Files
syops/backend/tests/test_shipping_audit_db_immutability.py
T
hugo b352c281d5 feat(shipping-notice): 增强发货通知与订单抽取功能,支持多进程处理与紧急停止
- 移除不再使用的 SHIPPING_NOTICE_LAYOUT_PARSER_REVISION 配置
- 在环境配置中新增 SHIPPING_NOTICE_WORKER_CONCURRENCY 和 SHIPPING_NOTICE_JOB_RUNNING_TIMEOUT_SECONDS
- 实现发货通知 worker 进程,支持并发处理订单抽取任务
- 引入紧急停止功能,允许管理员在运行中控制订单解析状态
- 更新相关 API,支持读取与设置紧急停止状态
- 增强文档与代码注释,反映新的业务逻辑与功能要求
2026-07-31 18:00:49 +08:00

98 lines
4.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
backend/tests/test_shipping_audit_db_immutability.py
用途:验收 shipping_audit_events 不可变约束(ORM 层始终;PostgreSQL 触发器在 CI/生产)。
依赖:ShippingAuditEvent ORM 守卫、audit_service.append_shipping_audit_event、SQLAlchemy dialect。
逻辑:pytest TestingConfig 强制 SQLite create_all,无 DB 触发器 —— 仅断言 ORM append-only;
触发器断言在非 PostgreSQL 时 pytest.skip(迁移 a0e1f2b3c4d5 仅对 PG 生效)。
目标 PostgreSQL 环境发布前须另跑:
``python scripts/verify_shipping_audit_pg_immutability.py``(日志不输出连接凭据);
本地 dev DATABASE_URL=postgresql 时该脚本应输出 UPDATE/DELETE blocked。
"""
from __future__ import annotations
import pytest
from sqlalchemy import text
from app import db
from app.core.errors import ValidationError
from app.models import ShippingAuditEvent
from app.services.shipping_notice.audit_service import append_shipping_audit_event
def _is_postgresql() -> bool:
"""当前应用绑定是否为 PostgreSQL。"""
bind = db.session.bind
return bind is not None and bind.dialect.name == 'postgresql'
def test_shipping_audit_orm_immutability_still_holds(app):
"""任意方言:已提交审计经 ORM update/delete 必须被拒绝(测试库通常无 DB 触发器)。"""
with app.app_context():
event = append_shipping_audit_event(
event_type='workflow_item_created',
aggregate_type='shipping_notice_workflow_item',
aggregate_id='WFI-DB-IMM-1',
workflow_id='WF-DB-IMM-1',
workflow_item_id='WFI-DB-IMM-1',
)
db.session.commit()
event_id = int(event.id)
event.reason_code = 'tampered'
with pytest.raises(ValidationError, match='append-only'):
db.session.commit()
db.session.rollback()
persisted = db.session.get(ShippingAuditEvent, event_id)
db.session.delete(persisted)
with pytest.raises(ValidationError, match='append-only'):
db.session.commit()
db.session.rollback()
# 关键断言:删除失败后记录仍在。
assert db.session.get(ShippingAuditEvent, event_id) is not None
def test_shipping_audit_pg_trigger_blocks_core_update(app):
"""PostgreSQL:Core UPDATE 须被触发器拒绝;SQLite 跳过。"""
with app.app_context():
if not _is_postgresql():
pytest.skip('DB triggers only exist on PostgreSQL after migration a0e1f2b3c4d5')
event = append_shipping_audit_event(
event_type='workflow_created',
aggregate_type='shipping_notice_workflow',
aggregate_id='WF-DB-IMM-TRIG',
workflow_id='WF-DB-IMM-TRIG',
)
db.session.commit()
with pytest.raises(Exception):
db.session.execute(
text('UPDATE shipping_audit_events SET reason_code = :rc WHERE id = :eid'),
{'rc': 'x', 'eid': int(event.id)},
)
db.session.commit()
db.session.rollback()
def test_shipping_audit_pg_trigger_blocks_core_delete_without_purge_guc(app):
"""PostgreSQL:未设置 purge GUC 时 Core DELETE 须被触发器拒绝;SQLite 跳过。"""
with app.app_context():
if not _is_postgresql():
pytest.skip('DB triggers only exist on PostgreSQL after migration a0e1f2b3c4d5')
event = append_shipping_audit_event(
event_type='workflow_created',
aggregate_type='shipping_notice_workflow',
aggregate_id='WF-DB-IMM-DEL',
workflow_id='WF-DB-IMM-DEL',
)
db.session.commit()
with pytest.raises(Exception):
db.session.execute(
text('DELETE FROM shipping_audit_events WHERE id = :eid'),
{'eid': int(event.id)},
)
db.session.commit()
db.session.rollback()
assert db.session.get(ShippingAuditEvent, int(event.id)) is not None