From c5f771faec16c3f13a4a710c735ae7996b3affee Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Thu, 16 Nov 2023 23:26:55 -0800 Subject: [PATCH] test --- tests/conftest.py | 30 +++++++++++++++++-------- tests/e2e/youtube/test_channel.py | 23 +++++++++++++++++++ tests/expected_transaction_log.py | 2 +- tests/resources.py | 4 ++++ tests/resources/file_fixtures/empty.txt | 1 + 5 files changed, 50 insertions(+), 10 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 3881fb6c..aa35b6ed 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,7 +14,9 @@ from unittest.mock import patch import pytest from expected_download import _get_files_in_directory -from resources import copy_file_fixture, file_fixture_path +from resources import copy_file_fixture +from resources import file_fixture_path +from yt_dlp.utils import sanitize_filename from ytdl_sub.config.config_file import ConfigFile from ytdl_sub.subscriptions.subscription_download import SubscriptionDownload @@ -68,13 +70,13 @@ def working_directory() -> str: @pytest.fixture() -def output_directory() -> Path: +def output_directory() -> str: with tempfile.TemporaryDirectory() as temp_dir: yield temp_dir @pytest.fixture() -def reformat_directory() -> Path: +def reformat_directory() -> str: with tempfile.TemporaryDirectory() as temp_dir: yield temp_dir @@ -163,25 +165,35 @@ def preset_dict_to_subscription_yaml_generator() -> Callable: return _preset_dict_to_subscription_yaml_generator + ################################################################################################### # Staging a mock already-existing download + @pytest.fixture def pz_channel_mock_downloaded_with_archive_factory(output_directory: Path) -> Callable: - def _pz_channel_mock_downloaded_with_archive_factory(subscription_name: str, download_archive_file_name: str): - copy_file_fixture(fixture_name="pz_download_archive.json", output_file_path=output_directory / download_archive_file_name) - with open(file_fixture_path("pz_download_archive.json"), "r", encoding="utf-8") as archive_file: + def _pz_channel_mock_downloaded_with_archive_factory(tv_show_name: str, archive_file_name: str): + subscription_path = Path(output_directory) / sanitize_filename(tv_show_name) + copy_file_fixture( + fixture_name="pz_download_archive.json", + output_file_path=subscription_path / archive_file_name, + ) + + with open( + file_fixture_path("pz_download_archive.json"), "r", encoding="utf-8" + ) as archive_file: archive_dict = json.load(archive_file) assert isinstance(archive_dict, dict) for uid, metadata in archive_dict.items(): assert isinstance(metadata, dict) - for filename in metadata['file_names']: + for filename in metadata["file_names"]: assert isinstance(filename, str) + output_file_path = subscription_path / filename if filename.endswith(".mp4"): - copy_file_fixture("sample_vid.mp4", output_directory / filename) + copy_file_fixture("sample_vid.mp4", output_file_path) else: - copy_file_fixture("empty.txt", output_directory / filename) + copy_file_fixture("empty.txt", output_file_path) return _pz_channel_mock_downloaded_with_archive_factory diff --git a/tests/e2e/youtube/test_channel.py b/tests/e2e/youtube/test_channel.py index 585abaa4..82962f8a 100644 --- a/tests/e2e/youtube/test_channel.py +++ b/tests/e2e/youtube/test_channel.py @@ -1,7 +1,11 @@ +from typing import Callable +from typing import Dict + import pytest from expected_download import assert_expected_downloads from expected_transaction_log import assert_transaction_log_matches +from ytdl_sub.config.config_file import ConfigFile from ytdl_sub.subscriptions.subscription import Subscription @@ -65,3 +69,22 @@ class TestChannel: dry_run=dry_run, expected_download_summary_file_name="youtube/test_channel_full.json", ) + + def test_full_channel_existing_archive_downloads_nothing( + self, + pz_channel_mock_downloaded_with_archive_factory: Callable, + tv_show_config: ConfigFile, + channel_preset_dict: Dict, + ): + subscription_name = "pz" + full_channel_subscription = Subscription.from_dict( + config=tv_show_config, preset_name=subscription_name, preset_dict=channel_preset_dict + ) + tv_show_name = channel_preset_dict["overrides"]["tv_show_name"] + archive_file_name = f".ytdl-sub-{subscription_name}-download-archive.json" + + pz_channel_mock_downloaded_with_archive_factory( + tv_show_name=tv_show_name, archive_file_name=archive_file_name + ) + transaction_log = full_channel_subscription.download(dry_run=True) + assert transaction_log.is_empty diff --git a/tests/expected_transaction_log.py b/tests/expected_transaction_log.py index d6b186a0..3eddf176 100644 --- a/tests/expected_transaction_log.py +++ b/tests/expected_transaction_log.py @@ -11,7 +11,7 @@ _TRANSACTION_LOG_SUMMARY_PATH = RESOURCE_PATH / "transaction_log_summaries" def assert_transaction_log_matches( - output_directory: Path, + output_directory: Path | str, transaction_log: FileHandlerTransactionLog, transaction_log_summary_file_name: str, ): diff --git a/tests/resources.py b/tests/resources.py index d961f290..8a426c9e 100644 --- a/tests/resources.py +++ b/tests/resources.py @@ -1,3 +1,4 @@ +import os import shutil from pathlib import Path @@ -6,8 +7,11 @@ REGENERATE_FIXTURES: bool = False RESOURCE_PATH: Path = Path("tests") / "resources" _FILE_FIXTURE_PATH: Path = RESOURCE_PATH / "file_fixtures" + def file_fixture_path(fixture_name: str) -> Path: return _FILE_FIXTURE_PATH / fixture_name + def copy_file_fixture(fixture_name: str, output_file_path: Path) -> None: + os.makedirs(os.path.dirname(output_file_path), exist_ok=True) shutil.copy(file_fixture_path(fixture_name), output_file_path) diff --git a/tests/resources/file_fixtures/empty.txt b/tests/resources/file_fixtures/empty.txt index e69de29b..7b4d68d7 100644 --- a/tests/resources/file_fixtures/empty.txt +++ b/tests/resources/file_fixtures/empty.txt @@ -0,0 +1 @@ +empty \ No newline at end of file