51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
"""任务池存储层单元测试。"""
|
|
import pytest
|
|
|
|
from app.constants import TaskStatus
|
|
from app.models.schemas import TaskInfo
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_and_get(task_repo):
|
|
t = TaskInfo(request_id="r1", task_type="compile", task_tags=["build"], payload={"cmd": "go build"})
|
|
created = await task_repo.create(t)
|
|
assert created is True
|
|
|
|
got = await task_repo.get("r1")
|
|
assert got is not None
|
|
assert got.task_type == "compile"
|
|
assert got.task_tags == ["build"]
|
|
assert got.payload == {"cmd": "go build"}
|
|
assert got.status == TaskStatus.PENDING
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_idempotent_create(task_repo):
|
|
t = TaskInfo(request_id="r1", task_type="compile")
|
|
assert await task_repo.create(t) is True
|
|
assert await task_repo.create(t) is False # 幂等
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_state_sets(task_repo):
|
|
t = TaskInfo(request_id="r1", task_type="compile")
|
|
await task_repo.create(t)
|
|
assert "r1" in await task_repo.pending_tasks()
|
|
|
|
await task_repo.mark_running("r1")
|
|
assert "r1" not in await task_repo.pending_tasks()
|
|
assert "r1" in await task_repo.running_tasks()
|
|
|
|
await task_repo.mark_finished("r1")
|
|
assert "r1" not in await task_repo.running_tasks()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update(task_repo):
|
|
t = TaskInfo(request_id="r1", task_type="compile")
|
|
await task_repo.create(t)
|
|
await task_repo.update("r1", status=TaskStatus.RUNNING, agent_id="a1", progress=50)
|
|
got = await task_repo.get("r1")
|
|
assert got.status == TaskStatus.RUNNING
|
|
assert got.agent_id == "a1"
|
|
assert got.progress == 50 |