more unit tests

This commit is contained in:
jbannon 2022-04-12 04:47:54 +00:00
parent 4c01198ce9
commit 1243832178
5 changed files with 31 additions and 51 deletions

View file

@ -42,7 +42,7 @@ def upload_month():
@pytest.fixture
def upload_day():
return 6
return 12
@pytest.fixture
@ -61,8 +61,8 @@ def thumbnail_ext():
@pytest.fixture
def thumbnail(thumbnail_ext):
return f"thumb.{thumbnail_ext}"
def download_thumbnail_name(uid, thumbnail_ext):
return f"{uid}.{thumbnail_ext}"
@pytest.fixture
@ -77,9 +77,7 @@ def mock_entry_to_dict(
ext,
upload_date,
upload_year,
thumbnail,
thumbnail_ext,
extractor,
description,
upload_month,
upload_day,
@ -96,22 +94,22 @@ def mock_entry_to_dict(
"upload_month_padded": _pad(upload_month),
"upload_day": upload_day,
"upload_day_padded": _pad(upload_day),
"thumbnail": thumbnail,
"thumbnail_ext": thumbnail_ext,
"extractor": extractor,
"description": description,
}
@pytest.fixture
def mock_entry_kwargs(uid, title, ext, upload_date, thumbnail, extractor, description):
def mock_entry_kwargs(
uid, title, ext, upload_date, extractor, description, download_thumbnail_name
):
return {
"id": uid,
"extractor": extractor,
"title": title,
"ext": ext,
"upload_date": upload_date,
"thumbnail": thumbnail,
"thumbnail": download_thumbnail_name,
"description": description,
}
@ -128,9 +126,10 @@ def validate_entry_properties(
upload_date,
upload_year,
ext,
thumbnail,
extractor,
thumbnail_ext,
download_file_name,
download_thumbnail_name,
):
def _validate_entry_properties(entry: Entry):
assert entry.uid == uid
@ -140,9 +139,11 @@ def validate_entry_properties(
assert entry.upload_date == upload_date
assert entry.upload_year == upload_year
assert entry.ext == ext
assert entry.thumbnail == thumbnail
assert entry.thumbnail_ext == thumbnail_ext
assert entry.download_file_name == download_file_name
assert entry.download_thumbnail_name == download_thumbnail_name
assert entry.playlist_metadata is None
assert entry.extractor == extractor
return True

View file

@ -1,10 +1,5 @@
import tempfile
import pytest
from ytdl_subscribe.validators.base.string_formatter_validators import StringFormatterValidator
from ytdl_subscribe.validators.exceptions import StringFormattingException
class TestEntry(object):
def test_entry_properties(
@ -22,16 +17,6 @@ class TestEntry(object):
):
assert validate_entry_dict_contains_valid_formatters(mock_entry)
def test_entry_file_path(self, mock_entry, download_file_name):
relative_directory = tempfile.TemporaryDirectory()
try:
file_path = mock_entry.file_path(relative_directory.name)
assert isinstance(file_path, str)
assert file_path == f"{relative_directory.name}/{download_file_name}"
finally:
relative_directory.cleanup()
def test_entry_missing_kwarg(self, mock_entry):
key = "dne"
expected_error_msg = f"Expected '{key}' in Entry but does not exist."

View file

@ -164,3 +164,8 @@ class TestDictFormatterValidator(object):
assert validator.dict["key1"].format_variables == ["variable"]
assert validator.dict["key2"].format_variables == []
assert validator.dict_with_format_strings == {
"key1": key1_format_string,
"key2": key2_format_string,
}

View file

@ -60,7 +60,6 @@ class BaseEntry(ABC):
"""Returns the entry's values as a dictionary"""
return {
"uid": self.uid,
"extractor": self.extractor,
}
@ -125,16 +124,6 @@ class Entry(BaseEntry):
def description(self) -> str:
return self.kwargs("description")
@property
def thumbnail(self) -> str:
"""Returns the entry's thumbnail url"""
return self.kwargs("thumbnail")
@property
def thumbnail_ext(self) -> str:
"""Returns the entry's thumbnail extension"""
return self.thumbnail.split(".")[-1]
@property
def download_file_name(self) -> str:
"""Returns the entry's file name when downloaded locally"""
@ -142,16 +131,15 @@ class Entry(BaseEntry):
@property
def download_thumbnail_name(self) -> str:
"""Returns the thumbnail's file name when downloaded locally TODO: unit test this"""
return f"{self.uid}.{self.thumbnail_ext}"
"""Returns the thumbnail's file name when downloaded locally"""
return self.kwargs("thumbnail")
def file_path(self, relative_directory: str):
"""Returns the entry's file path with respect to the relative directory"""
return str(Path(relative_directory) / self.download_file_name)
def thumbnail_path(self, relative_directory: str):
"""Returns the entry's thumbnail path with respect to the relative directory"""
return str(Path(relative_directory) / self.download_thumbnail_name)
@property
def thumbnail_ext(self) -> str:
"""
:return: The entry's thumbnail extension
"""
return self.download_thumbnail_name.split(".")[-1]
def to_dict(self) -> Dict:
"""Returns the entry's values as a dictionary"""
@ -162,6 +150,7 @@ class Entry(BaseEntry):
"sanitized_title": self.sanitized_title,
"description": self.description,
"ext": self.ext,
"thumbnail_ext": self.thumbnail_ext,
"upload_date": self.upload_date,
"upload_date_standardized": self.upload_date_standardized,
"upload_year": self.upload_year,
@ -169,7 +158,5 @@ class Entry(BaseEntry):
"upload_month_padded": self.upload_month_padded,
"upload_day": self.upload_day,
"upload_day_padded": self.upload_day_padded,
"thumbnail": self.thumbnail,
"thumbnail_ext": self.thumbnail_ext,
},
)

View file

@ -137,7 +137,9 @@ class Subscription(Generic[SourceT], ABC):
Tags the entry's audio file using values defined in the metadata options
"""
id3_options = self.metadata_options.id3
audio_file = music_tag.load_file(entry.file_path(relative_directory=self.working_directory))
entry_source_file_path = Path(self.working_directory) / entry.download_file_name
audio_file = music_tag.load_file(entry_source_file_path)
for tag, tag_formatter in id3_options.tags.dict.items():
audio_file[tag] = self._apply_formatter(formatter=tag_formatter, entry=entry)
audio_file.save()
@ -175,7 +177,7 @@ class Subscription(Generic[SourceT], ABC):
self._archive_entry_file_name(entry=entry, relative_file_name=nfo_file_name)
def _post_process_thumbnail(self, entry: Entry):
source_thumbnail_path = entry.thumbnail_path(relative_directory=self.working_directory)
source_thumbnail_path = Path(self.working_directory) / entry.download_thumbnail_name
# Bug that mismatches webp and jpg extensions. Try to hotfix here
if not os.path.isfile(source_thumbnail_path):
@ -214,7 +216,7 @@ class Subscription(Generic[SourceT], ABC):
def _copy_entry_file_to_output_directory(self, entry: Entry):
# Move the file after all direct file modifications are complete
entry_source_file_path = entry.file_path(relative_directory=self.working_directory)
entry_source_file_path = Path(self.working_directory) / entry.download_file_name
output_file_name = self._apply_formatter(
formatter=self.output_options.file_name, entry=entry