From 7450f0e72739126f4b2d6680ab01d07a00640a48 Mon Sep 17 00:00:00 2001 From: jbannon Date: Thu, 18 Aug 2022 21:51:47 +0000 Subject: [PATCH] tests complete! --- src/ytdl_sub/plugins/chapters.py | 4 +- src/ytdl_sub/plugins/split_by_chapters.py | 17 ++++-- src/ytdl_sub/utils/chapters.py | 2 +- tests/e2e/plugins/test_split_by_chapters.py | 53 +++++++++++++++++-- ...chapters_with_regex_no_chapters_video.json | 4 ++ ...ters_with_regex_no_chapters_video_drop.txt | 1 + ...ters_with_regex_no_chapters_video_pass.txt | 4 ++ ...ters_with_regex_no_chapters_video_drop.txt | 0 ...ters_with_regex_no_chapters_video_pass.txt | 12 +++++ 9 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 tests/e2e/resources/expected_downloads_summaries/plugins/split_by_chapters_with_regex_no_chapters_video.json create mode 100644 tests/e2e/resources/expected_downloads_summaries/plugins/split_by_chapters_with_regex_no_chapters_video_drop.txt create mode 100644 tests/e2e/resources/expected_downloads_summaries/plugins/split_by_chapters_with_regex_no_chapters_video_pass.txt create mode 100644 tests/e2e/resources/transaction_log_summaries/plugins/split_by_chapters_with_regex_no_chapters_video_drop.txt create mode 100644 tests/e2e/resources/transaction_log_summaries/plugins/split_by_chapters_with_regex_no_chapters_video_pass.txt diff --git a/src/ytdl_sub/plugins/chapters.py b/src/ytdl_sub/plugins/chapters.py index 8394a6e9..0fc9164d 100644 --- a/src/ytdl_sub/plugins/chapters.py +++ b/src/ytdl_sub/plugins/chapters.py @@ -30,13 +30,13 @@ SPONSORBLOCK_CATEGORIES: Set[str] = SPONSORBLOCK_HIGHLIGHT_CATEGORIES | { def _chapters(entry: Entry) -> List[Dict]: if entry.kwargs_contains("chapters"): - return entry.kwargs("chapters") + return entry.kwargs("chapters") or [] return [] def _sponsorblock_chapters(entry: Entry) -> List[Dict]: if entry.kwargs_contains("sponsorblock_chapters"): - return entry.kwargs("sponsorblock_chapters") + return entry.kwargs("sponsorblock_chapters") or [] return [] diff --git a/src/ytdl_sub/plugins/split_by_chapters.py b/src/ytdl_sub/plugins/split_by_chapters.py index 12426ef5..07ba0b34 100644 --- a/src/ytdl_sub/plugins/split_by_chapters.py +++ b/src/ytdl_sub/plugins/split_by_chapters.py @@ -10,6 +10,7 @@ from ytdl_sub.plugins.plugin import Plugin from ytdl_sub.plugins.plugin import PluginOptions from ytdl_sub.utils.chapters import Chapters from ytdl_sub.utils.chapters import Timestamp +from ytdl_sub.utils.exceptions import ValidationException from ytdl_sub.utils.ffmpeg import FFMPEG from ytdl_sub.utils.file_handler import FileHandler from ytdl_sub.utils.file_handler import FileMetadata @@ -145,8 +146,9 @@ class SplitByChaptersPlugin(Plugin[SplitByChaptersOptions]): else: chapters = Chapters.from_embedded_chapters(file_path=entry.get_download_file_path()) - # If no chapters, do not split anything - if not chapters.contains_any_chapters(): + # If no chapters, do not split anything + if not chapters.contains_any_chapters(): + if self.plugin_options.when_no_chapters == "pass": entry.add_variables( { "chapter_title": entry.title, @@ -156,9 +158,16 @@ class SplitByChaptersPlugin(Plugin[SplitByChaptersOptions]): } ) return [(entry, FileMetadata())] + if self.plugin_options.when_no_chapters == "drop": + return [] - # convert the entry thumbnail early so we do not have to guess the thumbnail extension - # when copying it. Do not error if it's not found, in case thumbnail_name is not set + raise ValidationException( + f"Tried to split '{entry.title}' by chapters but it has no chapters" + ) + + # convert the entry thumbnail early so we do not have to guess the thumbnail extension + # when copying it. Do not error if it's not found, in case thumbnail_name is not set + if not self.is_dry_run: convert_download_thumbnail(entry=entry, error_if_not_found=False) for idx, title in enumerate(chapters.titles): diff --git a/src/ytdl_sub/utils/chapters.py b/src/ytdl_sub/utils/chapters.py index a50ad216..3d315d2b 100644 --- a/src/ytdl_sub/utils/chapters.py +++ b/src/ytdl_sub/utils/chapters.py @@ -254,7 +254,7 @@ class Chapters: chapters = {} if entry.kwargs_contains("chapters"): - chapters = entry.kwargs("chapters") + chapters = entry.kwargs("chapters") or [] for chapter in chapters: timestamps.append(Timestamp.from_seconds(int(float(chapter["start_time"])))) diff --git a/tests/e2e/plugins/test_split_by_chapters.py b/tests/e2e/plugins/test_split_by_chapters.py index bc9e05b9..0e865a57 100644 --- a/tests/e2e/plugins/test_split_by_chapters.py +++ b/tests/e2e/plugins/test_split_by_chapters.py @@ -1,9 +1,12 @@ +import re + import mergedeep import pytest from e2e.expected_download import assert_expected_downloads from e2e.expected_transaction_log import assert_transaction_log_matches from ytdl_sub.subscriptions.subscription import Subscription +from ytdl_sub.utils.exceptions import ValidationException @pytest.fixture @@ -79,13 +82,11 @@ class TestSplitByChapters: output_directory=output_directory, transaction_log=transaction_log, transaction_log_summary_file_name=f"plugins/split_by_chapters_video{'-dry-run' if dry_run else ''}.txt", - regenerate_transaction_log=True, ) assert_expected_downloads( output_directory=output_directory, dry_run=dry_run, expected_download_summary_file_name="plugins/split_by_chapters_video.json", - regenerate_expected_download_summary=True, ) @pytest.mark.parametrize("dry_run", [True, False]) @@ -107,11 +108,55 @@ class TestSplitByChapters: output_directory=output_directory, transaction_log=transaction_log, transaction_log_summary_file_name=f"plugins/split_by_chapters_with_regex_video{'-dry-run' if dry_run else ''}.txt", - regenerate_transaction_log=True, ) assert_expected_downloads( output_directory=output_directory, dry_run=dry_run, expected_download_summary_file_name="plugins/split_by_chapters_with_regex_video.json", - regenerate_expected_download_summary=True, + ) + + @pytest.mark.parametrize("dry_run", [True, False]) + @pytest.mark.parametrize("when_no_chapters", ["pass", "drop", "error"]) + def test_video_with_no_chapters_and_regex( + self, + youtube_audio_config, + yt_album_as_chapters_with_regex_preset_dict, + output_directory, + dry_run, + when_no_chapters, + ): + mergedeep.merge( + yt_album_as_chapters_with_regex_preset_dict, + { + "youtube": {"video_url": "https://youtube.com/watch?v=HKTNxEqsN3Q"}, + "split_by_chapters": {"when_no_chapters": when_no_chapters}, + }, + ) + + subscription = Subscription.from_dict( + config=youtube_audio_config, + preset_name="split_by_chapters_with_regex_video", + preset_dict=yt_album_as_chapters_with_regex_preset_dict, + ) + + if when_no_chapters == "error": + with pytest.raises( + ValidationException, + match=re.escape( + "Tried to split 'Oblivion Mod \"Falcor\" p.1' by chapters but it has no chapters" + ), + ): + _ = subscription.download(dry_run=dry_run) + return + + transaction_log = subscription.download(dry_run=dry_run) + assert_transaction_log_matches( + output_directory=output_directory, + transaction_log=transaction_log, + transaction_log_summary_file_name=f"plugins/split_by_chapters_with_regex_no_chapters_video_{when_no_chapters}.txt", + ) + assert_expected_downloads( + output_directory=output_directory, + dry_run=dry_run, + expected_download_summary_file_name=f"plugins/split_by_chapters_with_regex_no_chapters_video_{when_no_chapters}.txt", ) diff --git a/tests/e2e/resources/expected_downloads_summaries/plugins/split_by_chapters_with_regex_no_chapters_video.json b/tests/e2e/resources/expected_downloads_summaries/plugins/split_by_chapters_with_regex_no_chapters_video.json new file mode 100644 index 00000000..1df20954 --- /dev/null +++ b/tests/e2e/resources/expected_downloads_summaries/plugins/split_by_chapters_with_regex_no_chapters_video.json @@ -0,0 +1,4 @@ +{ + "Oblivion Mod \"Falcor\" p.1/01 - Oblivion Mod \"Falcor\" p.1.mp3": "703ffb93964ac025ee66221b98ee4d49", + "Oblivion Mod \"Falcor\" p.1/folder.jpg": "fb95b510681676e81c321171fc23143e" +} \ No newline at end of file diff --git a/tests/e2e/resources/expected_downloads_summaries/plugins/split_by_chapters_with_regex_no_chapters_video_drop.txt b/tests/e2e/resources/expected_downloads_summaries/plugins/split_by_chapters_with_regex_no_chapters_video_drop.txt new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/tests/e2e/resources/expected_downloads_summaries/plugins/split_by_chapters_with_regex_no_chapters_video_drop.txt @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tests/e2e/resources/expected_downloads_summaries/plugins/split_by_chapters_with_regex_no_chapters_video_pass.txt b/tests/e2e/resources/expected_downloads_summaries/plugins/split_by_chapters_with_regex_no_chapters_video_pass.txt new file mode 100644 index 00000000..1df20954 --- /dev/null +++ b/tests/e2e/resources/expected_downloads_summaries/plugins/split_by_chapters_with_regex_no_chapters_video_pass.txt @@ -0,0 +1,4 @@ +{ + "Oblivion Mod \"Falcor\" p.1/01 - Oblivion Mod \"Falcor\" p.1.mp3": "703ffb93964ac025ee66221b98ee4d49", + "Oblivion Mod \"Falcor\" p.1/folder.jpg": "fb95b510681676e81c321171fc23143e" +} \ No newline at end of file diff --git a/tests/e2e/resources/transaction_log_summaries/plugins/split_by_chapters_with_regex_no_chapters_video_drop.txt b/tests/e2e/resources/transaction_log_summaries/plugins/split_by_chapters_with_regex_no_chapters_video_drop.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/e2e/resources/transaction_log_summaries/plugins/split_by_chapters_with_regex_no_chapters_video_pass.txt b/tests/e2e/resources/transaction_log_summaries/plugins/split_by_chapters_with_regex_no_chapters_video_pass.txt new file mode 100644 index 00000000..3c36afab --- /dev/null +++ b/tests/e2e/resources/transaction_log_summaries/plugins/split_by_chapters_with_regex_no_chapters_video_pass.txt @@ -0,0 +1,12 @@ +Files created in '{output_directory}' +---------------------------------------- +Oblivion Mod "Falcor" p.1/01 - Oblivion Mod "Falcor" p.1.mp3 + Music Tags: + album: Oblivion Mod "Falcor" p.1 + albumartist: Project Zombie + artist: Project Zombie + genre: Unset + title: Oblivion Mod "Falcor" p.1 + track: 1 + year: 2010 +Oblivion Mod "Falcor" p.1/folder.jpg \ No newline at end of file