51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from scripts.video_analysis.download_video import (
|
|
build_ydl_args,
|
|
download_video,
|
|
validate_output_path,
|
|
)
|
|
|
|
|
|
def test_validate_output_path_creates_parent(tmp_path: Path) -> None:
|
|
out = tmp_path / "subdir" / "video.mp4"
|
|
result = validate_output_path(out)
|
|
assert result.is_ok()
|
|
assert out.parent.exists()
|
|
|
|
|
|
def test_validate_output_path_rejects_existing_dir(tmp_path: Path) -> None:
|
|
out = tmp_path / "existing_dir"
|
|
out.mkdir()
|
|
result = validate_output_path(out)
|
|
assert result.is_err()
|
|
|
|
|
|
def test_build_ydl_args_basic(tmp_path: Path) -> None:
|
|
out = tmp_path / "v.mp4"
|
|
args = build_ydl_args("https://youtu.be/VID", out)
|
|
assert "--output" in args
|
|
assert str(out) in args
|
|
assert "https://youtu.be/VID" in args
|
|
|
|
|
|
def test_download_video_success(tmp_path: Path) -> None:
|
|
out = tmp_path / "video.mp4"
|
|
out.write_bytes(b"fake-mp4-content")
|
|
with patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(returncode=0, stdout="", stderr="")
|
|
result = download_video("https://youtu.be/VID", out)
|
|
assert result.is_ok()
|
|
assert (tmp_path / "video.log").exists()
|
|
|
|
|
|
def test_download_video_failure(tmp_path: Path) -> None:
|
|
out = tmp_path / "video.mp4"
|
|
with patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(returncode=1, stdout="", stderr="ERROR: video unavailable")
|
|
result = download_video("https://youtu.be/VID", out)
|
|
assert result.is_err()
|