Stabilize backend route tests

This commit is contained in:
2026-06-30 17:12:22 +08:00
parent 1d3891d949
commit 4eb9452760
3 changed files with 22 additions and 20 deletions

View File

@@ -1,12 +1,19 @@
from fastapi.testclient import TestClient
import asyncio
from app import jobs
from app.commands import CommandSpec
from app.jobs import default_conda_env_for_job
from app.main import _job_with_progress, app
from app.main import _job_with_progress
from app.schemas import JobCreate
async def _stream_text(response) -> str:
chunks = []
async for chunk in response.body_iterator:
chunks.append(chunk.decode("utf-8") if isinstance(chunk, bytes) else chunk)
return "".join(chunks)
def test_mmseg_jobs_use_mmseg_conda_env_by_default():
assert default_conda_env_for_job("mmseg.train") == "seg_mmcv"
assert default_conda_env_for_job("segmodel.train") == "seg_smp"
@@ -57,8 +64,9 @@ def test_job_events_respect_log_offset(tmp_path, monkeypatch):
return {"id": job_id, "status": "success", "log_path": str(log_path)}
monkeypatch.setattr(main.db, "get_job", fake_get_job)
response = TestClient(app).get(f"/api/jobs/job1/events?offset={len(old_chunk.encode('utf-8'))}")
response = asyncio.run(main.api_job_events("job1", offset=len(old_chunk.encode("utf-8"))))
text = asyncio.run(_stream_text(response))
assert response.status_code == 200
assert "new chunk" in response.text
assert "old chunk" not in response.text
assert response.media_type == "text/event-stream"
assert "new chunk" in text
assert "old chunk" not in text