def test_get_generic_torrent_path_handles_ContentError_from_mkdir(mocker, tmp_path): generic_torrents_dirpath = str(tmp_path / 'generic_torrents') mocker.patch('upsies.constants.GENERIC_TORRENTS_DIRPATH', generic_torrents_dirpath) mocker.patch('upsies.utils.fs.mkdir', side_effect=errors.ContentError('nope')) mocker.patch('upsies.utils.torrent._get_torrent_id', return_value='D34DB33F') cache_torrent = Mock() with pytest.raises(errors.TorrentError, match=rf'^{generic_torrents_dirpath}: nope$'): torrent._get_generic_torrent_path(cache_torrent, create_directory=True)
def test_resize_with_uncreatable_target_directory(mocker, tmp_path): mocker.patch('upsies.utils.fs.assert_file_readable') mocker.patch('upsies.utils.image._ffmpeg_executable', return_value='ffmpeg') mkdir_mock = mocker.patch('upsies.utils.fs.mkdir', side_effect=errors.ContentError('mkdir failed')) run_mock = mocker.patch('upsies.utils.subproc.run') target_directory = str(tmp_path / 'foo' / 'bar') with pytest.raises(errors.ImageResizeError, match=r'^mkdir failed$'): image.resize('image/does/not/exist.jpg', target_directory=target_directory) assert mkdir_mock.call_args_list == [call(target_directory)] assert run_mock.call_args_list == []
def test_nonexisting_video_file(mocker): assert_file_readable_mock = mocker.patch( 'upsies.utils.fs.assert_file_readable', side_effect=errors.ContentError('Foo you')) sanitize_path_mock = mocker.patch('upsies.utils.fs.sanitize_path', return_value='sanitized path') run_mock = mocker.patch('upsies.utils.subproc.run') mock_file = 'path/to/foo.mkv' with pytest.raises(errors.ScreenshotError, match=r'^Foo you$'): image.screenshot(mock_file, 123, 'image.png') assert assert_file_readable_mock.call_args_list == [call(mock_file)] assert sanitize_path_mock.call_args_list == [] assert run_mock.call_args_list == []
def test_screenshots_process_fails_to_find_first_video( tmp_path, screenshots_process_patches): screenshots_process_patches.first_video.side_effect = errors.ContentError( 'No video found') _screenshots_process( output_queue=screenshots_process_patches.output_queue, input_queue=screenshots_process_patches.input_queue, content_path='path/to/foo', timestamps=(10 * 60, '20:00'), count=2, output_dir='path/to/destination', overwrite=False, ) assert screenshots_process_patches.mock_calls == [ call.first_video('path/to/foo'), call.output_queue.put((MsgType.error, 'No video found')), ]
async def test_execute_catches_ContentError(tmp_path, mocker): mediainfo_mock = mocker.patch('upsies.utils.video.mediainfo', side_effect=errors.ContentError('Ouch')) mi = MediainfoJob( home_directory=tmp_path, cache_directory=tmp_path, ignore_cache=True, content_path='mock/path', ) assert mediainfo_mock.call_args_list == [] assert mi.output == () assert mi.errors == () assert mi.exit_code is None assert not mi.is_finished mi.execute() await mi.wait() assert mediainfo_mock.call_args_list == [call('mock/path')] assert mi.output == () assert [str(e) for e in mi.errors] == ['Ouch'] assert mi.exit_code == 1 assert mi.is_finished
def test_getting_duration_fails(mocker): assert_file_readable_mock = mocker.patch( 'upsies.utils.fs.assert_file_readable', return_value=True) sanitize_path_mock = mocker.patch('upsies.utils.fs.sanitize_path', return_value='sanitized path') path_exists_mock = mocker.patch('os.path.exists', side_effect=(False, True)) duration_mock = mocker.patch( 'upsies.utils.video.duration', side_effect=errors.ContentError('not a video file')) make_screenshot_cmd_mock = mocker.patch( 'upsies.utils.image._make_screenshot_cmd', return_value='mock cmd') run_mock = mocker.patch('upsies.utils.subproc.run') mock_file = 'path/to/foo.mkv' with pytest.raises(errors.ScreenshotError, match=r'^not a video file$'): image.screenshot(mock_file, 123, 'image.png') assert assert_file_readable_mock.call_args_list == [call(mock_file)] assert sanitize_path_mock.call_args_list == [call('image.png')] assert path_exists_mock.call_args_list == [call('sanitized path')] assert duration_mock.call_args_list == [call(mock_file)] assert make_screenshot_cmd_mock.call_args_list == [] assert run_mock.call_args_list == []
from unittest.mock import Mock, call import pytest from upsies import errors from upsies.utils import release from upsies.utils.types import ReleaseType @pytest.mark.parametrize( argnames='strict, exception, exp_exception', argvalues=( (False, None, None), (False, errors.SceneAbbreviatedFilenameError('foo'), None), (True, errors.SceneAbbreviatedFilenameError('foo'), errors.ContentError(errors.SceneAbbreviatedFilenameError('foo'))), (True, None, None), ), ids=lambda v: str(v), ) def test_strict_argument(strict, exception, exp_exception, mocker): mocker.patch('upsies.utils.scene.assert_not_abbreviated_filename', side_effect=exception) if exp_exception: exp_msg = re.escape(str(exp_exception)) with pytest.raises(type(exp_exception), match=rf'^{exp_msg}$'): release.ReleaseInfo('foo.mkv', strict=strict) else: ri = release.ReleaseInfo('foo.mkv', strict=strict) assert ri['title'] == 'foo'
@pytest.fixture def job(tmp_path): return FooJob(home_directory=tmp_path, cache_directory=tmp_path, ignore_cache=False) @pytest.mark.parametrize( argnames='path, exp_exception', argvalues=( ('path/to/home', None), ('', None), (None, None), ('/root/upsies/test', errors.ContentError('/root/upsies/test: Permission denied')), ), ) def test_home_directory_property(path, exp_exception, tmp_path): if path is None: job = FooJob() assert job.home_directory == '' else: job = FooJob(home_directory=tmp_path / path) if exp_exception: with pytest.raises(type(exp_exception), match=rf'^{re.escape(str(exp_exception))}$'): job.home_directory else: assert job.home_directory == tmp_path / path assert os.path.exists(job.home_directory)
def test_resize_with_unreadable_file(mocker): mocker.patch('upsies.utils.fs.assert_file_readable', side_effect=errors.ContentError('No')) mocker.patch('upsies.utils.subproc.run') with pytest.raises(errors.ImageResizeError, match=r'^No$'): image.resize('a.jpg', 10, 20)
file_list_mock = mocker.patch('upsies.utils.fs.file_list', return_value=files) queries = tuple(find._generate_episode_queries(path)) assert queries == exp_queries if exp_file_list_called: assert file_list_mock.call_args_list == [ call(path, extensions=constants.VIDEO_FILE_EXTENSIONS), ] else: assert file_list_mock.call_args_list == [] @pytest.mark.parametrize( argnames='raised_by_ReleaseInfo, raised_by_from_release, exp_exception', argvalues=( (None, None, None), (errors.ContentError('bad release name'), None, errors.SceneError('bad release name')), (None, errors.ContentError('something something'), errors.SceneError('something something')), ), ) def test_SceneQuery_from_string(raised_by_ReleaseInfo, raised_by_from_release, exp_exception, mocker): ReleaseInfo_mock = mocker.patch('upsies.utils.release.ReleaseInfo', side_effect=raised_by_ReleaseInfo) from_release_mock = mocker.patch('upsies.utils.scene.find.SceneQuery.from_release', side_effect=raised_by_from_release) if exp_exception: with pytest.raises(errors.SceneError, match=rf'^{re.escape(str(exp_exception))}$'): find.SceneQuery.from_string('foo') else: query = find.SceneQuery.from_string('foo') assert query.keywords == from_release_mock.return_value.keywords assert query.group == from_release_mock.return_value.group assert query.episodes == from_release_mock.return_value.episodes