mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-13 21:10:48 +01:00
transcribe_helpers.py is 289 lines against the 250-line cap and was completely untested. Tests land first, in their own commit, so the split that follows is verified against tests that predate the seam. Left at its current path rather than moved under python_pkg/: transcribe.sh invokes it by path inside a dedicated ML venv, and transcribe_fw.py imports two siblings by bare name, so the closure is five files (1236 lines), not three. Tests go in linux_configuration/tests/ instead, whose conftest already puts standalone script directories on sys.path - the same arrangement the usage_report work used, and still collected by CI. The ML dependencies are never installed in CI and the module reaches them only through _try_import, so patching that one seam covers every probe in both directions. An AST scan at every nesting level confirmed there is no other path to them: the only direct numpy imports sit under TYPE_CHECKING. 41 tests, 100% line and branch. The last gap was the dispatch guard at line 284, unreachable through the CLI because argparse's choices list and the dispatch dict carry the same eight commands; covering it by admitting a ninth choice also documents why the guard is there. Split across four test files because a single module hit 291 lines - over the cap this work exists to clear. Verified: python-version prints 3.14, check-faster-whisper exits 7 with no ML deps present, generate-wav writes a 96044-byte WAV; 243 tests pass in the directory, up from 202 by exactly the 41 added. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
144 lines
4.6 KiB
Python
144 lines
4.6 KiB
Python
"""Tests for transcribe.sh's model download and CUDA probe.
|
|
|
|
Both reach faster_whisper through ``_try_import``, so the package itself is
|
|
never needed; the double records how the model would have been constructed.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import TYPE_CHECKING
|
|
|
|
import transcribe_helpers as helpers
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Callable
|
|
|
|
from conftest import FakeWhisper
|
|
import pytest
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# prepare_model / test_cuda
|
|
# --------------------------------------------------------------------------- #
|
|
def test_prepare_model_downloads_with_cpu_int8(
|
|
fake_whisper: type[FakeWhisper],
|
|
importer: Callable[[dict[str, object]], object],
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""The model is fetched on CPU in int8, into the requested directory."""
|
|
fw = fake_whisper()
|
|
monkeypatch.setattr(helpers, "_try_import", importer({"faster_whisper": fw}))
|
|
|
|
assert helpers.prepare_model("small", "/models") is True
|
|
assert fw.calls == [
|
|
{
|
|
"name": "small",
|
|
"device": "cpu",
|
|
"compute_type": "int8",
|
|
"download_root": "/models",
|
|
}
|
|
]
|
|
|
|
|
|
def test_prepare_model_enables_progress_bars_when_hub_is_present(
|
|
fake_whisper: type[FakeWhisper],
|
|
importer: Callable[[dict[str, object]], object],
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""huggingface_hub progress bars are turned back on for the download."""
|
|
fw = fake_whisper()
|
|
hub_logging = type("L", (), {"set_verbosity_info": lambda self: None})()
|
|
hub = type("H", (), {"constants": type("C", (), {})()})()
|
|
monkeypatch.setattr(
|
|
helpers,
|
|
"_try_import",
|
|
importer(
|
|
{
|
|
"faster_whisper": fw,
|
|
"huggingface_hub.logging": hub_logging,
|
|
"huggingface_hub": hub,
|
|
}
|
|
),
|
|
)
|
|
monkeypatch.delenv("HF_HUB_DISABLE_PROGRESS_BARS", raising=False)
|
|
|
|
assert helpers.prepare_model("small", "/models") is True
|
|
assert hub.constants.HF_HUB_DISABLE_PROGRESS_BARS is False
|
|
|
|
|
|
def test_prepare_model_fails_without_faster_whisper(
|
|
fake_whisper: type[FakeWhisper],
|
|
importer: Callable[[dict[str, object]], object],
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
caplog: pytest.LogCaptureFixture,
|
|
) -> None:
|
|
"""Without the package there is nothing to download."""
|
|
monkeypatch.setattr(helpers, "_try_import", importer({}))
|
|
|
|
with caplog.at_level(logging.ERROR):
|
|
result = helpers.prepare_model("small", "/models")
|
|
|
|
assert result is False
|
|
assert "not installed" in caplog.text
|
|
|
|
|
|
def test_prepare_model_reports_a_download_failure(
|
|
fake_whisper: type[FakeWhisper],
|
|
importer: Callable[[dict[str, object]], object],
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
caplog: pytest.LogCaptureFixture,
|
|
) -> None:
|
|
"""A network or disk error is logged and reported, not raised."""
|
|
|
|
failing = fake_whisper(error=OSError("no space left"))
|
|
monkeypatch.setattr(helpers, "_try_import", importer({"faster_whisper": failing}))
|
|
|
|
with caplog.at_level(logging.ERROR):
|
|
result = helpers.prepare_model("small", "/models")
|
|
|
|
assert result is False
|
|
assert "Failed to prepare model" in caplog.text
|
|
|
|
|
|
def test_test_cuda_initialises_a_tiny_model_on_the_gpu(
|
|
fake_whisper: type[FakeWhisper],
|
|
importer: Callable[[dict[str, object]], object],
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""The CUDA probe loads the tiny model in float16 on the GPU."""
|
|
fw = fake_whisper()
|
|
monkeypatch.setattr(helpers, "_try_import", importer({"faster_whisper": fw}))
|
|
|
|
assert helpers.test_cuda() is True
|
|
assert fw.calls == [{"name": "tiny", "device": "cuda", "compute_type": "float16"}]
|
|
|
|
|
|
def test_test_cuda_fails_without_faster_whisper(
|
|
fake_whisper: type[FakeWhisper],
|
|
importer: Callable[[dict[str, object]], object],
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""No package means no CUDA test."""
|
|
monkeypatch.setattr(helpers, "_try_import", importer({}))
|
|
|
|
assert helpers.test_cuda() is False
|
|
|
|
|
|
def test_test_cuda_reports_an_unusable_gpu(
|
|
fake_whisper: type[FakeWhisper],
|
|
importer: Callable[[dict[str, object]], object],
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
caplog: pytest.LogCaptureFixture,
|
|
) -> None:
|
|
"""A machine without a working CUDA stack reports False, not a crash."""
|
|
|
|
no_cuda = fake_whisper(error=RuntimeError("CUDA driver not found"))
|
|
monkeypatch.setattr(helpers, "_try_import", importer({"faster_whisper": no_cuda}))
|
|
|
|
with caplog.at_level(logging.ERROR):
|
|
result = helpers.test_cuda()
|
|
|
|
assert result is False
|
|
assert "CUDA test failed" in caplog.text
|