Files
syops/backend/tests/test_shipping_process_gate_models.py
T
hugoandCursor 323928626b feat(shipping-process-gates): 落地全门禁中断恢复并消除双真相
纳入 Execution/Gate/Attempt 模型与迁移、Worker/API/前端恢复卡片与 Admin 诊断,
Gate 阻断时清零旧 action_buttons,异常摘要统一脱敏,并为运维筛选与 Attempt Diff 补齐入口。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-01 13:08:32 +08:00

192 lines
7.9 KiB
Python

"""
tests/test_shipping_process_gate_models.py
用途:验证门禁恢复三模型的唯一约束、活动 Attempt 部分唯一索引与编号不可覆盖。
依赖:Alembic Operations、SQLite 内存库、d0e1f2a3b4c6 迁移建表结果。
逻辑:在最小父表桩上 upgrade 后插入 Execution/Gate/Attempt,断言重复 Execution、
重复幂等键与双活动 Attempt 被数据库拒绝;终态后可创建下一 Attempt。
"""
from __future__ import annotations
import importlib
from datetime import datetime
import pytest
import sqlalchemy as sa
from alembic.migration import MigrationContext
from alembic.operations import Operations
def _prepare_gate_tables(monkeypatch):
"""
创建迁移所需最小父表并执行 upgrade,返回 connection 与表对象。
"""
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_workflows', metadata,
sa.Column('id', sa.Integer(), primary_key=True),
)
sa.Table(
'shipping_notice_documents', 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),
)
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)
connection = engine.connect()
connection.execute(sa.text('INSERT INTO shipping_notice_workflow_items (id) VALUES (1)'))
connection.execute(sa.text('INSERT INTO shipping_notice_workflow_item_versions (id) VALUES (1)'))
connection.execute(sa.text('INSERT INTO shipping_notice_workflow_item_versions (id) VALUES (2)'))
connection.commit()
operations = Operations(MigrationContext.configure(connection))
monkeypatch.setattr(migration, 'op', operations)
migration.upgrade()
executions = sa.Table('shipping_process_executions', sa.MetaData(), autoload_with=connection)
gates = sa.Table('shipping_process_gates', sa.MetaData(), autoload_with=connection)
attempts = sa.Table('shipping_process_gate_attempts', sa.MetaData(), autoload_with=connection)
return connection, migration, executions, gates, attempts
def test_duplicate_execution_constraints_rejected(monkeypatch):
"""重复 execution_no 与同一 version 的第二 Execution 必须被拒绝。"""
connection, _migration, executions, gates, _attempts = _prepare_gate_tables(monkeypatch)
now = datetime.utcnow()
connection.execute(executions.insert().values(
id=1, execution_no='SPE-1', workflow_item_id=1, workflow_item_version_id=1,
status='active', lock_version=1, created_at=now, updated_at=now,
))
connection.commit()
with pytest.raises(sa.exc.IntegrityError):
connection.execute(executions.insert().values(
id=2, execution_no='SPE-1', workflow_item_id=1, workflow_item_version_id=2,
status='active', lock_version=1, created_at=now, updated_at=now,
))
connection.commit()
connection.rollback()
with pytest.raises(sa.exc.IntegrityError):
connection.execute(executions.insert().values(
id=3, execution_no='SPE-2', workflow_item_id=1, workflow_item_version_id=1,
status='active', lock_version=1, created_at=now, updated_at=now,
))
connection.commit()
connection.rollback()
connection.execute(gates.insert().values(
id=1, execution_id=1, gate_code='inventory_source_routing', sequence_no=10,
status='blocked_retryable', evaluation_version=1, lock_version=1,
created_at=now, updated_at=now,
))
connection.commit()
connection.close()
def test_active_attempt_partial_unique_and_idempotency(monkeypatch):
"""同一 Gate 不能有两个 queued/running Attempt;幂等键全局唯一。"""
connection, _migration, executions, gates, attempts = _prepare_gate_tables(monkeypatch)
now = datetime.utcnow()
connection.execute(executions.insert().values(
id=1, execution_no='SPE-ACTIVE', workflow_item_id=1, workflow_item_version_id=1,
status='active', lock_version=1, created_at=now, updated_at=now,
))
connection.execute(gates.insert().values(
id=1, execution_id=1, gate_code='inventory_source_routing', sequence_no=10,
status='blocked_retryable', evaluation_version=1, lock_version=1,
created_at=now, updated_at=now,
))
connection.execute(attempts.insert().values(
id=1, gate_id=1, attempt_no=1, status='running', trigger_type='user_retry',
actor_type='user', actor_id='1', idempotency_key='idem-1', created_at=now,
))
connection.commit()
with pytest.raises(sa.exc.IntegrityError):
connection.execute(attempts.insert().values(
id=2, gate_id=1, attempt_no=2, status='queued', trigger_type='user_retry',
actor_type='user', actor_id='1', idempotency_key='idem-2', created_at=now,
))
connection.commit()
connection.rollback()
with pytest.raises(sa.exc.IntegrityError):
connection.execute(attempts.insert().values(
id=3, gate_id=1, attempt_no=2, status='failed', trigger_type='user_retry',
actor_type='user', actor_id='1', idempotency_key='idem-1', created_at=now,
))
connection.commit()
connection.rollback()
connection.close()
def test_succeeded_attempt_allows_next_number_not_reuse(monkeypatch):
"""成功 Attempt 后可创建下一编号;不得复用同一 attempt_no。"""
connection, _migration, executions, gates, attempts = _prepare_gate_tables(monkeypatch)
now = datetime.utcnow()
connection.execute(executions.insert().values(
id=1, execution_no='SPE-OK', workflow_item_id=1, workflow_item_version_id=1,
status='active', lock_version=1, created_at=now, updated_at=now,
))
connection.execute(gates.insert().values(
id=1, execution_id=1, gate_code='task_creation', sequence_no=12,
status='passed', evaluation_version=1, lock_version=1,
created_at=now, updated_at=now,
))
connection.execute(attempts.insert().values(
id=1, gate_id=1, attempt_no=1, status='succeeded', trigger_type='auto',
actor_type='system', idempotency_key='idem-ok-1', finished_at=now, created_at=now,
))
connection.commit()
connection.execute(attempts.insert().values(
id=2, gate_id=1, attempt_no=2, status='queued', trigger_type='user_retry',
actor_type='user', actor_id='9', idempotency_key='idem-ok-2', created_at=now,
))
connection.commit()
count = connection.execute(sa.text(
'SELECT COUNT(*) FROM shipping_process_gate_attempts WHERE gate_id = 1'
)).scalar_one()
assert count == 2
with pytest.raises(sa.exc.IntegrityError):
connection.execute(attempts.insert().values(
id=3, gate_id=1, attempt_no=1, status='failed', trigger_type='auto',
actor_type='system', idempotency_key='idem-ok-3', created_at=now,
))
connection.commit()
connection.rollback()
connection.close()