wrolpi/modules/videos/channel/test/test_lib.py
Roland 6fa6be50fe
Stopping renderd when reseting map db.
Skipping channel import during test unless explicitly requested.

Deleting gis database during upgrade, if user agrees.  Increasing API verbosity.

Using status context to prevent fetching status in useHotspot.

Disabling wifi toggle button when pending.

Toggling hotspot when clicking nav hotspot icon.

Displaying Hotspot status icon in nav menu.

Importing channels/favorites after refreshing.

Refresh tasks cancel when server shuts down.

Bumping version for release speed.

Refreshing files is 3x faster.

Creating useRecurringTimeout hook which repeatedly calls a callback after a timeout.

Changing intervals to timeout so fetches don't stack up while server is loaded.

Checking that video file exists before fetching.

Creating release candidate branch.  Not indexing model associated files.
2022-10-21 15:21:57 -06:00

131 lines
4.6 KiB
Python

from unittest import mock
import pytest
from modules.videos import schema
from modules.videos.channel import lib
from modules.videos.lib import save_channels_config, import_channels_config
from modules.videos.models import Channel, Video
from wrolpi.errors import UnknownChannel
from wrolpi.files.lib import refresh_files
@pytest.mark.parametrize('params', [
dict(),
dict(channel_id=None),
dict(source_id=None),
dict(url=None),
dict(directory=None),
])
def test_get_bad_channel(test_session, test_directory, params):
"""A Channel cannot be found without data."""
with pytest.raises(UnknownChannel):
lib.get_channel(**params)
def test_get_channel(test_session, test_directory, channel_factory):
"""`get_channel` can find a Channel in multiple ways."""
channel_id_channel = channel_factory()
source_id_channel = channel_factory()
source_id_channel.source_id = 'the source id'
url_channel = channel_factory()
url_channel.url = 'https://example.com/channel'
directory_channel = channel_factory()
directory_channel.directory = test_directory / 'some directory'
test_session.commit()
channel = lib.get_channel(channel_id=channel_id_channel.id, return_dict=False)
assert channel == channel_id_channel
channel = lib.get_channel(source_id='the source id', return_dict=False)
assert channel == source_id_channel
channel = lib.get_channel(url='https://example.com/channel', return_dict=False)
assert channel == url_channel
# Directory can be relative, or absolute.
channel = lib.get_channel(directory='some directory', return_dict=False)
assert channel == directory_channel
channel = lib.get_channel(directory=f'{test_directory}/some directory', return_dict=False)
assert channel == directory_channel
# Channel is found using the priority of params, incorrect data is ignored.
params = [
dict(channel_id=channel_id_channel.id, directory='bad directory'),
dict(channel_id=channel_id_channel.id, directory='some directory'),
dict(channel_id=channel_id_channel.id, source_id='bad source id'),
dict(channel_id=channel_id_channel.id, source_id='the source id'),
]
for p in params:
assert lib.get_channel(**p, return_dict=False) == channel_id_channel
assert lib.get_channel(**p)
def test_channels_no_url(test_session, test_directory):
"""Test that a Channel's URL is coerced to None if it is empty."""
channel1_directory = test_directory / 'channel1'
channel1_directory.mkdir()
channel2_directory = test_directory / 'channel2'
channel2_directory.mkdir()
channel1 = schema.ChannelPostRequest(
name='foo',
directory=str(channel1_directory),
)
channel1.url = ''
lib.create_channel(test_session, data=channel1)
channel2 = schema.ChannelPostRequest(
name='bar',
directory=str(channel2_directory),
url=''
)
lib.create_channel(test_session, data=channel2)
channels = list(test_session.query(Channel))
assert len(channels) == 2
assert all(i.url is None for i in channels), [(i.name, i.url) for i in channels]
save_channels_config()
import_channels_config()
@pytest.mark.asyncio
async def test_refresh_videos_finds_channel(test_session, test_client, channel_factory, video_factory):
"""A Video will be assigned to a Channel by its info json first, then to its directory."""
channel1 = channel_factory(source_id='channel1')
channel2 = channel_factory(source_id='channel2')
# Put the video in the wrong directory. It should be matched to the info json channel first, then to it's directory
channel1.source_id = 'channel source id'
vid1 = video_factory(
with_video_file=(channel2.directory / 'video1.mp4'),
with_info_json={'channel_id': channel1.source_id},
)
test_session.commit()
assert not vid1.channel_id
with mock.patch('wrolpi.common.after_refresh') as mock_after_refresh:
# Channel can be found in `video_cleanup`, prevent that with mock.
mock_after_refresh.side_effect = []
await refresh_files()
# Channel in info_json was found during validation.
assert vid1.channel_id == channel1.id
# Refresh using cleanup, info json should be trusted.
await refresh_files()
assert vid1.channel_id == channel1.id
# Remove the info json, the Video's Channel will be its directory.
vid1.channel_id = None
vid1.info_json_path.unlink()
test_session.commit()
await refresh_files()
test_session.refresh(vid1)
vid1 = test_session.query(Video).one()
assert vid1.channel_id == channel2.id