From 5100ed6ba298faf84c15c52b04f7b5547822d112 Mon Sep 17 00:00:00 2001 From: jbannon Date: Sun, 26 Jun 2022 22:07:11 +0000 Subject: [PATCH] multiple values --- .../entries/variables/youtube_variables.py | 4 ++++ tests/e2e/expected_download.py | 12 +++++++--- tests/e2e/youtube/test_merge_playlist.py | 5 ++++- tests/unit/utils/test_chapters.py | 22 +++++++++++++++++++ 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 tests/unit/utils/test_chapters.py diff --git a/src/ytdl_sub/entries/variables/youtube_variables.py b/src/ytdl_sub/entries/variables/youtube_variables.py index 1f68d7af..4ea0aa57 100644 --- a/src/ytdl_sub/entries/variables/youtube_variables.py +++ b/src/ytdl_sub/entries/variables/youtube_variables.py @@ -32,6 +32,8 @@ class YoutubeVideoVariables(EntryVariables): Returns ------- The track title of a music video if it is available, otherwise it falls back to the title. + NOTE: Even if a video has music metadata, this variable does not always get pulled via + yt-dlp. Use with caution. """ # Try to get the track, fall back on title if self.kwargs_contains("track"): @@ -54,6 +56,8 @@ class YoutubeVideoVariables(EntryVariables): Returns ------- The artist of a music video if it is available, otherwise it falls back to the channel. + NOTE: Even if a video has music metadata, this variable does not always get pulled via + yt-dlp. Use with caution. """ if self.kwargs_contains("artist"): return self.kwargs("artist") diff --git a/tests/e2e/expected_download.py b/tests/e2e/expected_download.py index a4f67ea6..65e49f3b 100644 --- a/tests/e2e/expected_download.py +++ b/tests/e2e/expected_download.py @@ -2,7 +2,9 @@ import hashlib import os.path from pathlib import Path from typing import Dict +from typing import List from typing import Optional +from typing import Union class ExpectedDownload: @@ -10,10 +12,11 @@ class ExpectedDownload: To test ytdl-sub downloads work, we compare each downloaded file's md5 hash to an expected md5 hash defined in this class. - If the hash value is None, only assert the file exists + If the hash value is None, only assert the file exists. If the hash value is a list, + try all the hashes (used in case the GitHub env produces different deterministic value). """ - def __init__(self, expected_md5_file_hashes: Dict[Path, Optional[str]]): + def __init__(self, expected_md5_file_hashes: Dict[Path, Optional[Union[str, List[str]]]]): self.expected_md5_file_hashes = expected_md5_file_hashes @property @@ -48,7 +51,10 @@ class ExpectedDownload: with open(full_path, "rb") as file: md5_hash = hashlib.md5(file.read()).hexdigest() - assert md5_hash == expected_md5_hash, ( + if isinstance(expected_md5_hash, str): + expected_md5_hash = [expected_md5_hash] + + assert md5_hash in expected_md5_hash, ( f"MD5 hash for {str(relative_path)} does not match: " f"{md5_hash} != {expected_md5_hash}" ) diff --git a/tests/e2e/youtube/test_merge_playlist.py b/tests/e2e/youtube/test_merge_playlist.py index caea06d9..eb2a2af4 100644 --- a/tests/e2e/youtube/test_merge_playlist.py +++ b/tests/e2e/youtube/test_merge_playlist.py @@ -66,7 +66,10 @@ def expected_playlist_download(): return ExpectedDownload( expected_md5_file_hashes={ Path("JMC - Jesse's Minecraft Server.jpg"): "348e3007fc590d0b1e2f6682501b0b5f", - Path("JMC - Jesse's Minecraft Server.mkv"): "6053c47a8690519b0a33c13fa4b01ac0", + Path("JMC - Jesse's Minecraft Server.mkv"): [ + "6053c47a8690519b0a33c13fa4b01ac0", + "6053c47a8690519b0a33c13fa4b01ac0", + ], Path("JMC - Jesse's Minecraft Server.nfo"): "10df5dcdb65ab18ecf21b3503c77e48b", } ) diff --git a/tests/unit/utils/test_chapters.py b/tests/unit/utils/test_chapters.py new file mode 100644 index 00000000..931fefc9 --- /dev/null +++ b/tests/unit/utils/test_chapters.py @@ -0,0 +1,22 @@ +import pytest + +from ytdl_sub.utils.chapters import Timestamp + + +class TestTimestamp: + @pytest.mark.parametrize( + "timestamp_str, timestamp_int", + [ + ("0:00", 0), + ("0:24", 24), + ("1:11", 71), + ("01:11", 71), + ("00:22", 22), + ("1:01:01", 3600 + 60 + 1), + ("01:01:01", 3600 + 60 + 1), + ("00:00:00", 0), + ], + ) + def test_timestamp_from_str(self, timestamp_str, timestamp_int): + ts = Timestamp.from_str(timestamp_str=timestamp_str) + assert ts.timestamp_sec == timestamp_int