This commit is contained in:
Jesse Bannon 2024-10-02 21:04:08 -07:00
parent 45c42a16ed
commit 68238e6697
7 changed files with 28 additions and 105 deletions

View file

@ -243,9 +243,12 @@ class DownloadArgsParser:
return subscription_dict
def get_dl_subscription_name(self) -> str:
"""
Returns a deterministic name based on input args
"""
to_hash = str(sorted(self._unknown_arguments))
hash = hashlib.sha256(to_hash.encode()).hexdigest()[-8:]
return f"cli-dl-{hash}"
hash_str = hashlib.sha256(to_hash.encode()).hexdigest()[-8:]
return f"cli-dl-{hash_str}"
@classmethod
def from_dl_override(cls, override: str, config: ConfigFile) -> "DownloadArgsParser":

View file

@ -7,7 +7,7 @@ import shutil
import sys
import tempfile
from pathlib import Path
from typing import Any, List
from typing import Any
from typing import Callable
from typing import Dict
from typing import List

View file

@ -25,5 +25,3 @@ def timestamps_file_path():
yield tmp.name
finally:
FileHandler.delete(tmp.name)

View file

@ -2,7 +2,7 @@ from pathlib import Path
from typing import Dict
import pytest
from conftest import assert_logs, mock_run_from_cli
from conftest import assert_logs
from expected_download import assert_expected_downloads
from expected_transaction_log import assert_transaction_log_matches
from mergedeep import mergedeep
@ -10,7 +10,6 @@ from mergedeep import mergedeep
from ytdl_sub.config.config_file import ConfigFile
from ytdl_sub.downloaders.ytdlp import YTDLP
from ytdl_sub.subscriptions.subscription import Subscription
from ytdl_sub.utils.system import IS_WINDOWS
@pytest.fixture
@ -192,79 +191,6 @@ class TestPlaylist:
output_directory=output_directory,
)
@pytest.mark.parametrize("dry_run", [True, False])
def test_playlist_download_from_cli_sub_no_provided_config(
self,
preset_dict_to_subscription_yaml_generator,
playlist_preset_dict,
output_directory,
dry_run,
):
# TODO: Fix CLI parsing on windows when dealing with spaces
if IS_WINDOWS:
return
# No config needed when using only prebuilt presets
with preset_dict_to_subscription_yaml_generator(
subscription_name="music_video_playlist_test", preset_dict=playlist_preset_dict
) as subscription_path:
args = "--dry-run " if dry_run else ""
args += f"sub '{subscription_path}'"
subscriptions = mock_run_from_cli(args=args)
assert len(subscriptions) == 1
transaction_log = subscriptions[0].transaction_log
assert_transaction_log_matches(
output_directory=output_directory,
transaction_log=transaction_log,
transaction_log_summary_file_name="youtube/test_playlist.txt",
)
assert_expected_downloads(
output_directory=output_directory,
dry_run=dry_run,
expected_download_summary_file_name="youtube/test_playlist.json",
)
if not dry_run:
# Ensure another invocation will hit ExistingVideoReached
with assert_logs(
logger=YTDLP.logger,
expected_message="ExistingVideoReached, stopping additional downloads",
log_level="debug",
):
transaction_log = mock_run_from_cli(args=args)[0].transaction_log
assert transaction_log.is_empty
assert_expected_downloads(
output_directory=output_directory,
dry_run=dry_run,
expected_download_summary_file_name="youtube/test_playlist.json",
)
def test_playlist_download_from_cli_sub_with_override_arg(
self,
preset_dict_to_subscription_yaml_generator,
playlist_preset_dict,
output_directory,
):
# TODO: Fix CLI parsing on windows when dealing with spaces
if IS_WINDOWS:
return
# No config needed when using only prebuilt presets
with preset_dict_to_subscription_yaml_generator(
subscription_name="music_video_playlist_test", preset_dict=playlist_preset_dict
) as subscription_path:
args = (
f"--dry-run sub '{subscription_path}' --dl-override '--date_range.after 20240101'"
)
subscriptions = mock_run_from_cli(args=args)
assert len(subscriptions) == 1
assert subscriptions[0].transaction_log.is_empty
def test_tv_show_by_date_downloads_bilateral(
self,
tv_show_by_date_bilateral_dict: Dict,

View file

@ -2,6 +2,7 @@ import pytest
from conftest import preset_dict_to_dl_args
from expected_download import assert_expected_downloads
from expected_transaction_log import assert_transaction_log_matches
from ytdl_sub.subscriptions.subscription import Subscription

View file

@ -2,12 +2,12 @@ from typing import Dict
from unittest.mock import patch
import pytest
from conftest import preset_dict_to_dl_args, mock_run_from_cli
from conftest import mock_run_from_cli
from conftest import preset_dict_to_dl_args
from expected_download import assert_expected_downloads
from expected_transaction_log import assert_transaction_log_matches
from ytdl_sub.cli.parsers.dl import DownloadArgsParser
from ytdl_sub.cli.parsers.dl import DownloadArgsParser
from ytdl_sub.utils.system import IS_WINDOWS
@ -20,15 +20,12 @@ def dl_subscription_dict(output_directory) -> Dict:
"music_video_directory": output_directory,
"url": "https://your.name.here",
},
"nfo_tags": {
"tags": {
"custom_tag": "{%array_at( ['hi', 'mom'], 1 )}"
}
}
"nfo_tags": {"tags": {"custom_tag": "{%array_at( ['hi', 'mom'], 1 )}"}},
}
class TestCliDl:
@pytest.mark.parametrize("config_provided", [True, False])
@pytest.mark.parametrize("dry_run", [True, False])
def test_cli_dl_command(
self,
@ -37,24 +34,30 @@ class TestCliDl:
dl_subscription_dict: Dict,
output_directory: str,
mock_download_collection_entries,
dry_run,
config_provided: bool,
dry_run: bool,
):
# Skip this combo to save time and to avoid mocking working dir
if not config_provided and not dry_run:
return
# TODO: Fix CLI parsing on windows when dealing with spaces
if IS_WINDOWS:
return
args = "--dry-run " if dry_run else ""
args += f"--config {default_config_path} "
args += f"--config {default_config_path} " if config_provided else ""
args += f"dl {preset_dict_to_dl_args(dl_subscription_dict)} "
with (
patch.object(DownloadArgsParser, "get_dl_subscription_name") as mock_subscription_name,
mock_download_collection_entries(
is_youtube_channel=False,
num_urls=1,
is_extracted_audio=False,
is_dry_run=dry_run,
)):
is_youtube_channel=False,
num_urls=1,
is_extracted_audio=False,
is_dry_run=dry_run,
),
):
mock_subscription_name.return_value = subscription_name
subscriptions = mock_run_from_cli(args=args)

View file

@ -186,11 +186,7 @@ def preset_with_subscription_overrides_map(
"elem2",
"elem3",
],
"custom_map": {
"custom_map_key": [
"custom_map_list_value"
]
}
"custom_map": {"custom_map_key": ["custom_map_list_value"]},
}
},
},
@ -295,11 +291,7 @@ def test_subscription_overrides_map(
"elem2",
"elem3",
],
"custom_map": {
"custom_map_key": [
"custom_map_list_value"
]
}
"custom_map": {"custom_map_key": ["custom_map_list_value"]},
}