Testing that downloads have absolute destinations. Handling 'undefined', 'null', etc, dates.

This commit is contained in:
Roland 2025-03-21 17:14:34 -06:00
parent 2728e1bf50
commit 399cb0f107
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
6 changed files with 25 additions and 6 deletions

View file

@ -19,7 +19,7 @@ from yt_dlp.extractor import YoutubeTabIE # noqa
from wrolpi.cmd import YT_DLP_BIN
from wrolpi.common import logger, get_media_directory, escape_file_name, resolve_generators, background_task, \
trim_file_name, cached_multiprocessing_result
trim_file_name, cached_multiprocessing_result, get_absolute_media_path
from wrolpi.dates import now
from wrolpi.db import get_db_session
from wrolpi.db import optional_session
@ -182,7 +182,8 @@ class ChannelDownloader(Downloader, ABC):
if channel:
settings.update(dict(channel_id=channel.id, channel_url=download.url))
if download.destination:
settings['destination'] = str(download.destination) # Need str for JSON conversion
destination = get_absolute_media_path(download.destination)
settings['destination'] = str(destination) # Need str for JSON conversion
if download.tag_names:
settings['tag_names'] = download.tag_names
if video_resolutions := download_settings.get('video_resolutions'):
@ -370,6 +371,7 @@ class VideoDownloader(Downloader, ABC):
# Make output directory. (Maybe string from settings)
out_dir = out_dir if isinstance(out_dir, pathlib.Path) else pathlib.Path(out_dir)
out_dir = get_absolute_media_path(out_dir)
out_dir.mkdir(exist_ok=True, parents=True)
if tag_names and logger.isEnabledFor(logging.DEBUG):

View file

@ -20,7 +20,11 @@ from ..lib import save_channels_config, get_channels_config, import_channels_con
def test_get_absolute_media_path(test_directory):
path = get_absolute_media_path('videos')
assert str(path).endswith('videos')
assert str(path) == f'{test_directory}/videos'
# An absolute path is not changed.
path = get_absolute_media_path(path)
assert str(path) == f'{test_directory}/videos'
def test_extract_video_duration(test_directory, corrupted_video_file):

View file

@ -390,7 +390,7 @@ def set_test_media_directory(path):
def get_media_directory() -> Path:
"""Get the media directory.
This will typically be /opt/wrolpi, or something else from the .env.
This will typically be /media/wrolpi, or something else from the .env.
During testing, this function returns TEST_MEDIA_DIRECTORY.
"""

View file

@ -46,6 +46,9 @@ def strpdate(dt: str) -> datetime:
"""
Attempt to parse a datetime string. Tries to find a Timezone, if possible. DB requires timezone.
"""
if not dt or dt in ('undefined', 'null'):
InvalidDatetime(f'Unable to parse empty datetime string: {dt}')
try:
if dt.count('/') == 2:
# No timezone info.

View file

@ -145,3 +145,9 @@ def test_invalid_strpdate():
with pytest.raises(InvalidDatetime):
dates.strpdate('27/04/2024 18:52:55')
with pytest.raises(InvalidDatetime):
dates.strpdate('undefined')
with pytest.raises(InvalidDatetime):
dates.strpdate('null')
with pytest.raises(InvalidDatetime):
dates.strpdate(None)

View file

@ -419,7 +419,7 @@ async def test_get_status(async_client, test_session):
@pytest.mark.asyncio
async def test_download_crud(test_session, async_client, test_download_manager_config, tag_factory):
async def test_download_crud(test_session, async_client, test_download_manager_config, tag_factory, test_directory):
"""Test creating once-downloads and recurring downloads."""
tag1, tag2 = await tag_factory(), await tag_factory()
@ -432,6 +432,7 @@ async def test_download_crud(test_session, async_client, test_download_manager_c
urls=['https://example.com/1', ],
frequency=1_000,
downloader='archive',
destination='archives/example.com',
tag_names=None, # tag_names can be a None.
)
request, response = await async_client.post('/api/download', content=json.dumps(body))
@ -452,6 +453,8 @@ async def test_download_crud(test_session, async_client, test_download_manager_c
download1, download2, download3 = test_session.query(Download).order_by(Download.url).all()
assert not download1.settings
# destination is absolute
assert download1.destination == test_directory / 'archives/example.com'
expected_download = dict(download1.__json__()).copy()
# Update a download.
@ -461,7 +464,8 @@ async def test_download_crud(test_session, async_client, test_download_manager_c
frequency=1_000,
tag_names=[tag1.name, tag2.name],
)
expected_download.update({'url': 'https://example.com/1', 'tag_names': [tag1.name, tag2.name]})
expected_download.update(
{'url': 'https://example.com/1', 'tag_names': [tag1.name, tag2.name], 'destination': None})
request, response = await async_client.put(f'/api/download/{download1.id}', json=body)
assert response.status_code == HTTPStatus.NO_CONTENT
test_session.flush([download2])