test dl args

This commit is contained in:
jbannon 2022-08-07 21:36:48 +00:00
parent e6f8dfd8dd
commit 1e3b6323d7
4 changed files with 90 additions and 21 deletions

View file

@ -89,14 +89,14 @@ def _download_subscription_from_cli(
return subscription, subscription.download(dry_run=args.dry_run) return subscription, subscription.download(dry_run=args.dry_run)
def main(): def main() -> List[Tuple[Subscription, FileHandlerTransactionLog]]:
""" """
Entrypoint for ytdl-sub, without the error handling Entrypoint for ytdl-sub, without the error handling
""" """
# If no args are provided, print help and exit # If no args are provided, print help and exit
if len(sys.argv) < 2: if len(sys.argv) < 2:
parser.print_help() parser.print_help()
return return []
args, extra_args = parser.parse_known_args() args, extra_args = parser.parse_known_args()
@ -123,3 +123,4 @@ def main():
# Ran successfully, so we can delete the debug file # Ran successfully, so we can delete the debug file
Logger.cleanup(delete_debug_file=True) Logger.cleanup(delete_debug_file=True)
return transaction_logs

View file

@ -1,7 +1,8 @@
import contextlib import contextlib
import logging import logging
from typing import Callable from typing import Any
from unittest.mock import MagicMock from typing import Dict
from typing import List
from unittest.mock import patch from unittest.mock import patch
from ytdl_sub.utils.logger import Logger from ytdl_sub.utils.logger import Logger
@ -26,3 +27,35 @@ def assert_debug_log(logger: logging.Logger, expected_message: str):
return return
assert False, f"{expected_message} was not found in a logger.debug call" assert False, f"{expected_message} was not found in a logger.debug call"
def preset_dict_to_dl_args(preset_dict: Dict) -> str:
"""
Parameters
----------
preset_dict
Preset dict to convert
Returns
-------
Preset dict converted to CLI parameters
"""
def _recursive_preset_args(cli_key: str, current_value: Dict | Any) -> List[str]:
if isinstance(current_value, dict):
preset_args: List[str] = []
for v_key, v_value in current_value.items():
preset_args.extend(
_recursive_preset_args(
cli_key=f"{cli_key}.{v_key}" if cli_key else v_key, current_value=v_value
)
)
return preset_args
elif isinstance(current_value, list):
return [
f"--{cli_key}[{idx + 1}] {current_value[idx]}" for idx in range(len(current_value))
]
else:
return [f"--{cli_key} {current_value}"]
return " ".join(_recursive_preset_args(cli_key="", current_value=preset_dict))

View file

@ -1,8 +1,15 @@
import sys
import tempfile import tempfile
from typing import List
from typing import Tuple
from unittest.mock import patch
import pytest import pytest
from ytdl_sub.cli.main import main
from ytdl_sub.config.config_file import ConfigFile from ytdl_sub.config.config_file import ConfigFile
from ytdl_sub.subscriptions.subscription import Subscription
from ytdl_sub.utils.file_handler import FileHandlerTransactionLog
@pytest.fixture() @pytest.fixture()
@ -12,8 +19,13 @@ def output_directory():
@pytest.fixture() @pytest.fixture()
def music_video_config(): def music_video_config_path():
return ConfigFile.from_file_path(config_path="examples/kodi_music_videos_config.yaml") return "examples/kodi_music_videos_config.yaml"
@pytest.fixture()
def music_video_config(music_video_config_path):
return ConfigFile.from_file_path(config_path=music_video_config_path)
@pytest.fixture() @pytest.fixture()
@ -41,3 +53,9 @@ def timestamps_file_path():
tmp.writelines(timestamps) tmp.writelines(timestamps)
tmp.seek(0) tmp.seek(0)
yield tmp.name yield tmp.name
def mock_run_from_cli(args: str) -> List[Tuple[Subscription, FileHandlerTransactionLog]]:
args_list = ["ytdl-sub"] + args.split()
with patch.object(sys, "argv", args_list):
return main()

View file

@ -1,6 +1,8 @@
from pathlib import Path from pathlib import Path
import pytest import pytest
from conftest import preset_dict_to_dl_args
from e2e.conftest import mock_run_from_cli
from e2e.expected_download import ExpectedDownloadFile from e2e.expected_download import ExpectedDownloadFile
from e2e.expected_download import ExpectedDownloads from e2e.expected_download import ExpectedDownloads
from e2e.expected_transaction_log import assert_transaction_log_matches from e2e.expected_transaction_log import assert_transaction_log_matches
@ -8,21 +10,6 @@ from e2e.expected_transaction_log import assert_transaction_log_matches
from ytdl_sub.subscriptions.subscription import Subscription from ytdl_sub.subscriptions.subscription import Subscription
@pytest.fixture
def playlist_preset_dict(output_directory):
return {
"preset": "yt_music_video_playlist",
"youtube": {"playlist_url": "https://youtube.com/playlist?list=PL5BC0FC26BECA5A35"},
# override the output directory with our fixture-generated dir
"output_options": {"output_directory": output_directory},
# download the worst format so it is fast
"ytdl_options": {
"format": "worst[ext=mp4]",
},
"overrides": {"artist": "JMC"},
}
@pytest.fixture @pytest.fixture
def single_video_preset_dict(output_directory): def single_video_preset_dict(output_directory):
return { return {
@ -38,6 +25,11 @@ def single_video_preset_dict(output_directory):
} }
@pytest.fixture
def single_video_preset_dict_dl_args(single_video_preset_dict):
return preset_dict_to_dl_args(single_video_preset_dict)
@pytest.fixture @pytest.fixture
def expected_single_video_download(): def expected_single_video_download():
# turn off black formatter here for readability # turn off black formatter here for readability
@ -91,6 +83,31 @@ class TestYoutubeVideo:
if not dry_run: if not dry_run:
expected_single_video_download.assert_files_exist(relative_directory=output_directory) expected_single_video_download.assert_files_exist(relative_directory=output_directory)
@pytest.mark.parametrize("dry_run", [True, False])
def test_single_video_download_from_cli_dl(
self,
music_video_config_path,
single_video_preset_dict_dl_args,
expected_single_video_download,
output_directory,
dry_run,
):
args = "--dry-run " if dry_run else ""
args += f"--config {music_video_config_path} "
args += f"dl {single_video_preset_dict_dl_args}"
subscription_transaction_log = mock_run_from_cli(args=args)
assert len(subscription_transaction_log) == 1
transaction_log = subscription_transaction_log[0][1]
assert_transaction_log_matches(
output_directory=output_directory,
transaction_log=transaction_log,
transaction_log_summary_file_name="youtube/test_video.txt",
)
if not dry_run:
expected_single_video_download.assert_files_exist(relative_directory=output_directory)
@pytest.mark.parametrize("dry_run", [True, False]) @pytest.mark.parametrize("dry_run", [True, False])
def test_single_video_with_timestamp_chapters_download( def test_single_video_with_timestamp_chapters_download(
self, self,