纳入 Execution/Gate/Attempt 模型与迁移、Worker/API/前端恢复卡片与 Admin 诊断, Gate 阻断时清零旧 action_buttons,异常摘要统一脱敏,并为运维筛选与 Attempt Diff 补齐入口。 Co-authored-by: Cursor <cursoragent@cursor.com>
88 lines
3.7 KiB
Python
88 lines
3.7 KiB
Python
"""
|
|
tests/test_shipping_process_gate_migration.py
|
|
|
|
用途:验证门禁恢复迁移 upgrade/downgrade 往返及关键索引存在。
|
|
依赖:Alembic Operations、SQLite 内存库、d0e1f2a3b4c6 迁移。
|
|
逻辑:构造最小父表 → upgrade 断言三表与活动 Attempt 唯一索引 → downgrade 断言表消失。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib
|
|
|
|
import sqlalchemy as sa
|
|
from alembic.migration import MigrationContext
|
|
from alembic.operations import Operations
|
|
|
|
|
|
def test_shipping_process_gate_migration_round_trip(monkeypatch):
|
|
"""upgrade 创建三表与索引;downgrade 完整删除。"""
|
|
migration = importlib.import_module(
|
|
'migrations.versions.d0e1f2a3b4c6_add_shipping_process_gate_recovery',
|
|
)
|
|
engine = sa.create_engine('sqlite:///:memory:')
|
|
metadata = sa.MetaData()
|
|
sa.Table('users', metadata, sa.Column('id', sa.Integer(), primary_key=True))
|
|
sa.Table('jobs', metadata, sa.Column('id', sa.Integer(), primary_key=True))
|
|
sa.Table('shipping_tasks', metadata, sa.Column('id', sa.Integer(), primary_key=True))
|
|
sa.Table('shipping_order_review_cases', metadata, sa.Column('id', sa.Integer(), primary_key=True))
|
|
sa.Table('shipping_notice_workflow_items', metadata, sa.Column('id', sa.Integer(), primary_key=True))
|
|
sa.Table(
|
|
'shipping_notice_workflow_item_versions',
|
|
metadata,
|
|
sa.Column('id', sa.Integer(), primary_key=True),
|
|
)
|
|
# 迁移会放宽 shipping_audit_events.actor_type CHECK,测试库需有最小表桩。
|
|
sa.Table(
|
|
'shipping_audit_events',
|
|
metadata,
|
|
sa.Column('id', sa.Integer(), primary_key=True),
|
|
sa.Column('actor_type', sa.String(length=32), nullable=False),
|
|
sa.CheckConstraint(
|
|
"actor_type IN ('user', 'system', 'rpa', 'chatflow', 'admin')",
|
|
name='ck_shipping_audit_events_actor_type',
|
|
),
|
|
)
|
|
metadata.create_all(engine)
|
|
|
|
with engine.begin() as connection:
|
|
operations = Operations(MigrationContext.configure(connection))
|
|
monkeypatch.setattr(migration, 'op', operations)
|
|
|
|
migration.upgrade()
|
|
inspector = sa.inspect(connection)
|
|
tables = set(inspector.get_table_names())
|
|
assert 'shipping_process_executions' in tables
|
|
assert 'shipping_process_gates' in tables
|
|
assert 'shipping_process_gate_attempts' in tables
|
|
|
|
attempt_indexes = {
|
|
item['name'] for item in inspector.get_indexes('shipping_process_gate_attempts')
|
|
}
|
|
assert 'uq_shipping_process_gate_attempts_one_active' in attempt_indexes
|
|
assert 'uq_shipping_process_gate_attempts_idempotency' in {
|
|
*(item['name'] for item in inspector.get_indexes('shipping_process_gate_attempts')),
|
|
*(
|
|
item['name']
|
|
for item in inspector.get_unique_constraints('shipping_process_gate_attempts')
|
|
),
|
|
}
|
|
|
|
execution_indexes = {
|
|
item['name'] for item in inspector.get_indexes('shipping_process_executions')
|
|
}
|
|
assert 'ix_shipping_process_executions_status_updated' in execution_indexes
|
|
assert 'ix_shipping_process_executions_current_gate' in execution_indexes
|
|
|
|
gate_indexes = {
|
|
item['name'] for item in inspector.get_indexes('shipping_process_gates')
|
|
}
|
|
assert 'ix_shipping_process_gates_execution_status' in gate_indexes
|
|
|
|
migration.downgrade()
|
|
inspector = sa.inspect(connection)
|
|
tables_after = set(inspector.get_table_names())
|
|
assert 'shipping_process_executions' not in tables_after
|
|
assert 'shipping_process_gates' not in tables_after
|
|
assert 'shipping_process_gate_attempts' not in tables_after
|