From 8b590d6867596d3b331c6b60a1e8dd05f5fa5891 Mon Sep 17 00:00:00 2001 From: Roland Date: Tue, 21 Dec 2021 09:19:26 -0700 Subject: [PATCH] Testing that a video can be deleted without a channel. --- modules/videos/common.py | 4 ++-- modules/videos/conftest.py | 12 +++++++++++- modules/videos/test/test_video.py | 15 +++++++++++++++ wrolpi/common.py | 4 ++-- wrolpi/conftest.py | 2 +- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/modules/videos/common.py b/modules/videos/common.py index a8df39b1..41d3b0f2 100644 --- a/modules/videos/common.py +++ b/modules/videos/common.py @@ -5,7 +5,7 @@ import subprocess import tempfile from functools import partial from pathlib import Path -from typing import Union, Tuple, List, Set, Iterable +from typing import Union, Tuple, List, Set, Iterable, Optional import PIL from PIL import Image @@ -148,7 +148,7 @@ def get_absolute_video_files(video: Video) -> List[MediaPath]: return list(_get()) -def get_video_info_json(video) -> Union[dict, None]: +def get_video_info_json(video) -> Optional[dict]: """Get the info_json object from a video's meta-file. Return an empty dict if not possible.""" if not video.channel or not video.channel.directory: return diff --git a/modules/videos/conftest.py b/modules/videos/conftest.py index a4bdd9d0..b0f71e34 100644 --- a/modules/videos/conftest.py +++ b/modules/videos/conftest.py @@ -1,7 +1,7 @@ import pytest from modules.videos.channel.lib import spread_channel_downloads -from modules.videos.models import Channel +from modules.videos.models import Channel, Video from wrolpi.downloader import DownloadFrequency @@ -31,3 +31,13 @@ def download_channel(simple_channel) -> Channel: simple_channel.download_frequency = DownloadFrequency.weekly spread_channel_downloads() return simple_channel + + +@pytest.fixture +def simple_video(test_session, test_directory, simple_channel) -> Video: + video_path = test_directory / 'simple_video.mp4' + video = Video(video_path=video_path, channel_id=simple_channel.id) + test_session.add(video) + test_session.commit() + video = test_session.query(Video).filter_by(id=video.id).one() + return video diff --git a/modules/videos/test/test_video.py b/modules/videos/test/test_video.py index 1a2d0f43..9f5c4048 100644 --- a/modules/videos/test/test_video.py +++ b/modules/videos/test/test_video.py @@ -10,3 +10,18 @@ def test_timezone(test_session): test_session.add(video) test_session.commit() assert video.upload_date.tzinfo + + +def test_delete_video_no_channel(test_session, simple_video): + """ + A Video can be deleted even if it does not have a channel. + """ + assert simple_video.channel + video_id = simple_video.id + simple_video.channel_id = None + test_session.commit() + + simple_video: Video = test_session.query(Video).filter_by(id=video_id).one() + + assert not simple_video.channel + simple_video.delete() diff --git a/wrolpi/common.py b/wrolpi/common.py index c62fb1bb..82f16e0b 100644 --- a/wrolpi/common.py +++ b/wrolpi/common.py @@ -12,7 +12,7 @@ from functools import wraps from itertools import islice from multiprocessing import Event, Queue from pathlib import Path -from typing import Union, Callable, Tuple, Dict, Mapping, List, Iterable +from typing import Union, Callable, Tuple, Dict, Mapping, List, Iterable, Optional from urllib.parse import urlunsplit, urlparse import yaml @@ -433,7 +433,7 @@ def get_relative_to_media_directory(path: str) -> Path: return absolute.relative_to(get_media_directory()) -def minimize_dict(d: dict, keys: Iterable) -> Union[dict, None]: +def minimize_dict(d: dict, keys: Iterable) -> Optional[dict]: """ Return a new dictionary that contains only the keys provided. """ diff --git a/wrolpi/conftest.py b/wrolpi/conftest.py index c1ad8c5e..81fd6b65 100644 --- a/wrolpi/conftest.py +++ b/wrolpi/conftest.py @@ -71,7 +71,7 @@ def test_session() -> Session: @pytest.fixture(autouse=True) -def test_directory(): +def test_directory() -> pathlib.Path: """ Overwrite the media directory with a temporary directory. """