Files
yt-shorts-downloader/tests/test_api.py
T

51 lines
1.4 KiB
Python

from __future__ import annotations
from pathlib import Path
import pytest
from yt_shorts_downloader import api
from yt_shorts_downloader.exceptions import InvalidSessionError, InvalidUrlError
from yt_shorts_downloader.models import SessionValidation
def test_download_returns_path_from_downloader(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
expected_path = tmp_path / "video.mp4"
monkeypatch.setattr(
api,
"validate_session_file",
lambda path: SessionValidation(True, True, True, True, str(path)),
)
monkeypatch.setattr(
api,
"download_video",
lambda *, url, output_dir, session_path: expected_path,
)
downloaded_path = api.download(
"https://youtube.com/shorts/example",
session_path="cookies.txt",
output_dir=tmp_path,
)
assert downloaded_path == expected_path
def test_download_rejects_invalid_url() -> None:
with pytest.raises(InvalidUrlError):
api.download("https://example.com/watch?v=1", session_path="cookies.txt")
def test_download_rejects_invalid_session(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
api,
"validate_session_file",
lambda path: SessionValidation(False, False, False, False, "bad session"),
)
with pytest.raises(InvalidSessionError, match="bad session"):
api.download("https://youtube.com/shorts/example", session_path="cookies.txt")