From 30ad9501e6cf7c24e47b2e005523f90c18371ed7 Mon Sep 17 00:00:00 2001 From: jbannon Date: Fri, 1 Jul 2022 14:00:00 +0000 Subject: [PATCH] working tests, still have more --- src/ytdl_sub/subscriptions/subscription.py | 5 ++++- src/ytdl_sub/utils/file_handler.py | 14 ++++++++----- .../enhanced_download_archive.py | 20 +++++++++++++++++-- tests/e2e/expected_download.py | 12 +++++++++++ .../test_playlist_as_kodi_music_videos.py | 16 ++++++++++++++- 5 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/ytdl_sub/subscriptions/subscription.py b/src/ytdl_sub/subscriptions/subscription.py index 8284f912..ce6625a2 100644 --- a/src/ytdl_sub/subscriptions/subscription.py +++ b/src/ytdl_sub/subscriptions/subscription.py @@ -18,6 +18,7 @@ from ytdl_sub.downloaders.downloader import DownloaderValidator from ytdl_sub.entries.entry import Entry from ytdl_sub.plugins.plugin import Plugin from ytdl_sub.plugins.plugin import PluginOptions +from ytdl_sub.utils.file_handler import FileHandlerTransactionLog from ytdl_sub.utils.thumbnail import convert_download_thumbnail from ytdl_sub.ytdl_additions.enhanced_download_archive import EnhancedDownloadArchive @@ -226,7 +227,7 @@ class Subscription: return plugins - def download(self, dry_run: bool = False): + def download(self, dry_run: bool = False) -> FileHandlerTransactionLog: """ Performs the subscription download @@ -270,6 +271,8 @@ class Subscription: overrides=self.overrides, output_directory=self.output_directory ) + return self._enhanced_download_archive.get_file_handler_transaction_log() + @classmethod def from_preset(cls, preset: Preset, config: ConfigFile) -> "Subscription": """ diff --git a/src/ytdl_sub/utils/file_handler.py b/src/ytdl_sub/utils/file_handler.py index 1e83c208..f43664c1 100644 --- a/src/ytdl_sub/utils/file_handler.py +++ b/src/ytdl_sub/utils/file_handler.py @@ -32,11 +32,6 @@ class FileHandlerTransactionLog: if not file_metadata: file_metadata = FileMetadata() - if file_name in self.files_created: - raise ValueError( - "Adding a file to the file handler transaction log that already exists" - ) - self.files_created[file_name] = file_metadata return self @@ -56,6 +51,15 @@ class FileHandler: self.output_directory = output_directory self._file_handler_transaction_log = FileHandlerTransactionLog() + @property + def file_handler_transaction_log(self) -> FileHandlerTransactionLog: + """ + Returns + ------- + Transaction logs of this file handler + """ + return self._file_handler_transaction_log + @classmethod def copy(cls, src_file_path: Union[str, Path], dst_file_path: Union[str, Path]): copyfile(src=src_file_path, dst=dst_file_path) diff --git a/src/ytdl_sub/ytdl_additions/enhanced_download_archive.py b/src/ytdl_sub/ytdl_additions/enhanced_download_archive.py index 36b6e490..dd57bddc 100644 --- a/src/ytdl_sub/ytdl_additions/enhanced_download_archive.py +++ b/src/ytdl_sub/ytdl_additions/enhanced_download_archive.py @@ -14,6 +14,7 @@ from yt_dlp import DateRange from ytdl_sub.entries.entry import Entry from ytdl_sub.utils.file_handler import FileHandler +from ytdl_sub.utils.file_handler import FileHandlerTransactionLog from ytdl_sub.utils.logger import Logger @@ -399,7 +400,7 @@ class EnhancedDownloadArchive: return f".ytdl-sub-{self.subscription_name}-download-archive.json" @property - def _mapping_output_file_path(self): + def _mapping_output_file_path(self) -> str: """ Returns ------- @@ -407,6 +408,15 @@ class EnhancedDownloadArchive: """ return str(Path(self.output_directory) / self._mapping_file_name) + @property + def _mapping_working_file_path(self) -> str: + """ + Returns + ------- + The download mapping's file path in the working directory. + """ + return str(Path(self.working_directory) / self._mapping_file_name) + @property def _archive_working_file_path(self) -> str: """ @@ -524,7 +534,10 @@ class EnhancedDownloadArchive: download_archive.remove_entry(entry_id) # Save the updated mapping file to the output directory - self._download_mapping.to_file(output_json_file=self._mapping_output_file_path) + # TODO: Make this cleaner. It writes the file to the working dir, the copies it to the + # output dir. Should be just a single write + self._download_mapping.to_file(output_json_file=self._mapping_working_file_path) + self.save_file(file_name=self._mapping_file_name, output_file_name=self._mapping_file_name) return self @@ -547,3 +560,6 @@ class EnhancedDownloadArchive: self._file_handler.copy_file_to_output_directory( file_name=file_name, output_file_name=output_file_name ) + + def get_file_handler_transaction_log(self) -> FileHandlerTransactionLog: + return self._file_handler.file_handler_transaction_log diff --git a/tests/e2e/expected_download.py b/tests/e2e/expected_download.py index 65e49f3b..ffc5dde3 100644 --- a/tests/e2e/expected_download.py +++ b/tests/e2e/expected_download.py @@ -6,6 +6,8 @@ from typing import List from typing import Optional from typing import Union +from ytdl_sub.utils.file_handler import FileHandlerTransactionLog + class ExpectedDownload: """ @@ -58,3 +60,13 @@ class ExpectedDownload: f"MD5 hash for {str(relative_path)} does not match: " f"{md5_hash} != {expected_md5_hash}" ) + + def assert_dry_run_files_logged(self, transaction_log: FileHandlerTransactionLog): + assert ( + len(transaction_log.files_created) == self.file_count + ), "Mismatch in number of created files" + + for relative_path in self.expected_md5_file_hashes.keys(): + assert ( + str(relative_path) in transaction_log.files_created + ), f"Expected {str(relative_path)} to be a file but it is not" diff --git a/tests/e2e/youtube/test_playlist_as_kodi_music_videos.py b/tests/e2e/youtube/test_playlist_as_kodi_music_videos.py index f4aa5a71..f8f09699 100644 --- a/tests/e2e/youtube/test_playlist_as_kodi_music_videos.py +++ b/tests/e2e/youtube/test_playlist_as_kodi_music_videos.py @@ -148,8 +148,22 @@ class TestPlaylistAsKodiMusicVideo: playlist_subscription.download() expected_playlist_download.assert_files_exist(relative_directory=output_directory) + def test_playlist_dry_run( + self, playlist_subscription, expected_playlist_download, output_directory + ): + file_transaction_log = playlist_subscription.download(dry_run=True) + expected_playlist_download.assert_dry_run_files_logged(transaction_log=file_transaction_log) + def test_single_video_download( self, single_video_subscription, expected_single_video_download, output_directory ): - single_video_subscription.download(dry_run=True) + single_video_subscription.download() expected_single_video_download.assert_files_exist(relative_directory=output_directory) + + def test_single_video_dry_run( + self, single_video_subscription, expected_single_video_download, output_directory + ): + file_transaction_log = single_video_subscription.download(dry_run=True) + expected_single_video_download.assert_dry_run_files_logged( + transaction_log=file_transaction_log + )