Add cleanup and artifact test coverage
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
|
||||
@@ -11,6 +12,20 @@ import server
|
||||
client = TestClient(server.app)
|
||||
|
||||
|
||||
class FakeTemporaryDirectory:
|
||||
def __init__(self, root: Path, prefix: str, created_paths: list[Path]) -> None:
|
||||
self.path = root / f"{prefix}{len(created_paths)}"
|
||||
self.created_paths = created_paths
|
||||
|
||||
def __enter__(self) -> str:
|
||||
self.path.mkdir(parents=True, exist_ok=False)
|
||||
self.created_paths.append(self.path)
|
||||
return str(self.path)
|
||||
|
||||
def __exit__(self, exc_type, exc, tb) -> None:
|
||||
shutil.rmtree(self.path, ignore_errors=True)
|
||||
|
||||
|
||||
def test_validate_output_format_rejects_unknown() -> None:
|
||||
try:
|
||||
server.validate_output_format("docx")
|
||||
@@ -21,6 +36,13 @@ def test_validate_output_format_rejects_unknown() -> None:
|
||||
|
||||
|
||||
def test_transcriptions_returns_generated_artifact(monkeypatch, tmp_path: Path) -> None:
|
||||
created_paths: list[Path] = []
|
||||
monkeypatch.setattr(
|
||||
server,
|
||||
"TemporaryDirectory",
|
||||
lambda prefix="": FakeTemporaryDirectory(tmp_path, prefix, created_paths),
|
||||
)
|
||||
|
||||
def fake_run(command: list[str], check: bool, capture_output: bool, text: bool):
|
||||
output_dir = Path(command[command.index("--output_dir") + 1])
|
||||
(output_dir / "clip.txt").write_text("hello world", encoding="utf-8")
|
||||
@@ -43,9 +65,18 @@ def test_transcriptions_returns_generated_artifact(monkeypatch, tmp_path: Path)
|
||||
assert response.status_code == 200
|
||||
assert response.text == "hello world"
|
||||
assert response.headers["x-whisper-output-format"] == "txt"
|
||||
assert created_paths
|
||||
assert all(not path.exists() for path in created_paths)
|
||||
|
||||
|
||||
def test_transcriptions_maps_subprocess_failure(monkeypatch) -> None:
|
||||
def test_transcriptions_maps_subprocess_failure(monkeypatch, tmp_path: Path) -> None:
|
||||
created_paths: list[Path] = []
|
||||
monkeypatch.setattr(
|
||||
server,
|
||||
"TemporaryDirectory",
|
||||
lambda prefix="": FakeTemporaryDirectory(tmp_path, prefix, created_paths),
|
||||
)
|
||||
|
||||
def fake_run(command: list[str], check: bool, capture_output: bool, text: bool):
|
||||
class Result:
|
||||
returncode = 1
|
||||
@@ -64,3 +95,5 @@ def test_transcriptions_maps_subprocess_failure(monkeypatch) -> None:
|
||||
|
||||
assert response.status_code == 502
|
||||
assert response.json()["detail"] == "bad whisper day"
|
||||
assert created_paths
|
||||
assert all(not path.exists() for path in created_paths)
|
||||
|
||||
Reference in New Issue
Block a user