diff --git a/backend/tests/test_artifacts.py b/backend/tests/test_artifacts.py index bb77d11..8f32857 100644 --- a/backend/tests/test_artifacts.py +++ b/backend/tests/test_artifacts.py @@ -1,9 +1,5 @@ from pathlib import Path -from fastapi.testclient import TestClient - -from app.main import app - def test_project_var_artifact_path_is_served(tmp_path, monkeypatch): from app import main @@ -30,6 +26,7 @@ def test_project_var_artifact_path_is_served(tmp_path, monkeypatch): ) monkeypatch.setattr(main, "settings", patched) - response = TestClient(app).get("/api/artifacts/var/uploads/datasets/case/labels/label.txt") - assert response.status_code == 200 - assert response.text == "ok" + response = main.api_artifact("var/uploads/datasets/case/labels/label.txt") + + assert Path(response.path) == artifact + assert response.media_type == "text/plain" diff --git a/backend/tests/test_jobs.py b/backend/tests/test_jobs.py index 07303e6..9f173c8 100644 --- a/backend/tests/test_jobs.py +++ b/backend/tests/test_jobs.py @@ -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 diff --git a/backend/tests/test_results_service.py b/backend/tests/test_results_service.py index 80c7f7e..4f5ad7a 100644 --- a/backend/tests/test_results_service.py +++ b/backend/tests/test_results_service.py @@ -1,9 +1,6 @@ from pathlib import Path -from fastapi.testclient import TestClient - from app import main -from app.main import app from app.modules.results.service import parse_training_curve @@ -39,9 +36,9 @@ def test_results_api_honors_limit(monkeypatch): return [{"name": "a.png", "relative_path": "var/a.png"}] monkeypatch.setattr(main, "scan_results", fake_scan_results) - response = TestClient(app).get("/api/results?limit=123") + response = main.api_results(limit=123) - assert response.status_code == 200 + assert response == [{"name": "a.png", "relative_path": "var/a.png"}] assert calls["limit"] == 123 @@ -53,7 +50,7 @@ def test_results_curve_api_honors_limit(monkeypatch): return [{"name": "results", "relative_path": "var/results.csv"}] monkeypatch.setattr(main, "scan_training_curves", fake_scan_training_curves) - response = TestClient(app).get("/api/results/curves?limit=77") + response = main.api_result_curves(limit=77) - assert response.status_code == 200 + assert response == [{"name": "results", "relative_path": "var/results.csv"}] assert calls["limit"] == 77