Files
geMoldInsight/tests/test_redis_no_fallback.py
T
2026-09-16 17:55:04 +08:00

58 lines
2.3 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.
"""批次 2(D7 / D9)回归测试。
- D7:Redis 任务管理器不再有进程内存回退——Redis 不可用时写 no-op、读返回 None,
状态查询路径落到 PG(PG 为单一事实源)
- D9:存储服务的数据本体写方法只 flush 不 commit,事务由编排层收口
"""
import pytest
from unittest.mock import AsyncMock, MagicMock
from shared.services.redis_task_manager import RedisTaskManager
@pytest.mark.asyncio
async def test_no_memory_fallback_when_disconnected():
"""Redis 未连接:写 no-op、读 None——不得再出现进程内可见的副本。"""
mgr = RedisTaskManager() # 不 connect
assert not mgr.is_connected
await mgr.set_task("t1", {"status": "processing"})
assert await mgr.get_task("t1") is None
await mgr.update_task("t1", {"status": "completed"}) # no-op,不得抛异常
assert await mgr.get_all_tasks() == {}
assert await mgr.get_task_count() == 0
await mgr.delete_task("t1") # no-op,不得抛异常
assert await mgr.get_task("t1") is None
def test_fallback_storage_removed():
"""防回归:内存回退存储必须已删除,防止静默回归。"""
assert not hasattr(RedisTaskManager, "_fallback_tasks")
assert not hasattr(RedisTaskManager, "_fallback_set")
assert not hasattr(RedisTaskManager, "cleanup_old_tasks")
@pytest.mark.asyncio
async def test_storage_writes_flush_but_never_commit():
"""D9:数据本体写方法仅 flush;commit 由编排层/请求侧负责。"""
pytest.importorskip("minio")
from sqlalchemy.ext.asyncio import AsyncSession
from moldinsight.services.storage_integration_rustfs import StorageIntegrationService
svc = StorageIntegrationService()
session = AsyncMock(spec=AsyncSession)
# update_task_parameters:select 返回 None(任务不存在)→ 直接 return
result_mock = MagicMock()
result_mock.scalar_one_or_none.return_value = None
session.execute.return_value = result_mock
await svc.update_task_parameters(session, "task-x", {"a": 1})
await svc.create_processing_task(session, "task-y", stp_file_id=1, batch_id="b-1")
assert session.flush.await_count >= 1
# 注意:不能用 .awaited(AsyncMock 上访问会自动创建 truthy 子 mock),用 await_count
assert session.commit.await_count == 0, "存储写方法不得自行 commit(D9 事务收口)"