def test_read_torrent_raises_TorrentError(mocker): import torf Torrent_read_mock = mocker.patch('torf.Torrent.read', side_effect=torf.TorfError('argh')) with pytest.raises(errors.TorrentError, match=r'^argh$'): btclients.ClientApiBase.read_torrent('path/to/file.torrent') assert Torrent_read_mock.call_args_list == [call('path/to/file.torrent')]
def test_create_handles_TorfError_from_torrent_write(torrent_provider, mocker, tmp_path): torrent_path = str(tmp_path / 'content.torrent'), exp_torrent = Mock( is_ready=True, write=Mock(side_effect=torf.TorfError('nope')), ) mocker.patch('upsies.utils.torrent._path_exists', return_value=False) mocker.patch('upsies.utils.torrent._store_generic_torrent') if torrent_provider == '_get_cached_torrent': mocker.patch('upsies.utils.torrent._get_cached_torrent', return_value=exp_torrent) mocker.patch('upsies.utils.torrent._get_generated_torrent', return_value=None) elif torrent_provider == '_get_generated_torrent': mocker.patch('upsies.utils.torrent._get_cached_torrent', return_value=None) mocker.patch('upsies.utils.torrent._get_generated_torrent', return_value=exp_torrent) else: raise RuntimeError(f'Unexpected torrent_provider: {torrent_provider!r}') with pytest.raises(errors.TorrentError, match=r'^nope$'): torrent.create( content_path=str(tmp_path / 'content'), torrent_path=torrent_path, announce='http://foo', source='ASDF', init_callback=Mock(), progress_callback=Mock(), info_callback=Mock(), exclude=('a', 'b', 'c'), )
def test_read_torrent_handles_TorfError_from_Torrent_read(mocker, tmp_path): content_path = str(tmp_path / 'content') exclude = ('foo', 'bar', 'baz') torrent_path = 'path/to/existing.torrent' Torrent_mock = mocker.patch('torf.Torrent', Mock(read=Mock(side_effect=torf.TorfError('nope')))) mocker.patch('upsies.utils.torrent._copy_torrent_info') mocker.patch('time.time', return_value='mock time') t = torrent._read_torrent(content_path, exclude=exclude, torrent_path=torrent_path) assert t is None assert Torrent_mock.call_args_list == [call( path=content_path, exclude_regexs=exclude, private=True, created_by=f'{__project_name__} {__version__}', creation_date='mock time', )] assert Torrent_mock.read.call_args_list == [call(torrent_path)] assert torrent._copy_torrent_info.call_args_list == []
def test_get_generated_torrent_handles_TorfError_from_Torrent_generate(mocker, tmp_path): content_path = str(tmp_path / 'content') exclude = ('foo', 'bar', 'baz') announce = 'http://announce.mock' source = 'ASDF', created_by = f'{__project_name__} {__version__}' time = 'mock time' init_callback = Mock(return_value=False) progress_callback = Mock() Torrent_mock = mocker.patch('torf.Torrent') Torrent_mock.return_value.generate.side_effect = torf.TorfError('nope') mocker.patch('upsies.utils.torrent._CreateTorrentCallbackWrapper') mocker.patch('upsies.utils.torrent._make_file_tree') mocker.patch('time.time', return_value=time) with pytest.raises(errors.TorrentError, match=r'^nope$'): torrent._get_generated_torrent( content_path=content_path, announce=announce, source=source, exclude=exclude, init_callback=init_callback, progress_callback=progress_callback, ) assert Torrent_mock.call_args_list == [call( path=content_path, exclude_regexs=exclude, trackers=((announce,),), private=True, source=source, created_by=created_by, creation_date=time, )] assert torrent._CreateTorrentCallbackWrapper.call_args_list == [call(progress_callback)] assert torrent._make_file_tree.call_args_list == [call(Torrent_mock.return_value.filetree)] assert init_callback.call_args_list == [call(torrent._make_file_tree.return_value)] assert Torrent_mock.return_value.generate.call_args_list == [call( callback=torrent._CreateTorrentCallbackWrapper.return_value, interval=1.0, )]
def test_get_cached_torrent_ignores_error_from_creating_Torrent(mocker): reuse_torrent_path = 'path/to/cache_directory' content_path = 'path/to/content' exclude = ('*.jpg',) cached_torrent = Mock(name='3.torrent object') Torrent_mock = mocker.patch('torf.Torrent', side_effect=torf.TorfError('no such path')) get_generic_torrent_path_mock = mocker.patch('upsies.utils.torrent._get_generic_torrent_path', return_value='path/to/generic.torrent') file_list_mock = mocker.patch('upsies.utils.fs.file_list', side_effect=( (f'{i}.torrent' for i in range(1, 10)), )) read_torrent_mock = mocker.patch('upsies.utils.torrent._read_torrent', side_effect=( None, None, cached_torrent, )) info_callback = Mock(return_value=False) t = torrent._get_cached_torrent( reuse_torrent_path=reuse_torrent_path, content_path=content_path, exclude=exclude, metadata={'tracker': ('http://localhost:123',), 'source': 'ASDF'}, info_callback=info_callback, ) assert t is cached_torrent assert Torrent_mock.call_args_list == [call(path='path/to/content', exclude_regexs=exclude)] assert get_generic_torrent_path_mock.call_args_list == [] assert file_list_mock.call_args_list == [ call('path/to/cache_directory', extensions=('torrent',)), ] assert info_callback.call_args_list == [call('1.torrent'), call('2.torrent'), call('3.torrent')] assert read_torrent_mock.call_args_list == [ call(content_path='path/to/content', exclude=exclude, torrent_path='1.torrent'), call(content_path='path/to/content', exclude=exclude, torrent_path='2.torrent'), call(content_path='path/to/content', exclude=exclude, torrent_path='3.torrent'), ]
def test_DownloadLocationJob_hidden(): assert download_location.DownloadLocationJob.hidden is True def test_DownloadLocationJob_cache_id(): assert download_location.DownloadLocationJob.cache_id is None @pytest.mark.parametrize( argnames= 'locations, default, torf_error, target_location, exp_get_target_location_calls, exp_errors, exp_output', argvalues=( ((), None, None, None, [], ('You must provide at least one location.', ), ()), (('a', 'b', 'c'), None, torf.TorfError('nope'), None, [], (torf.TorfError('nope'), ), ()), (( 'a', 'b', 'c', ), None, None, 'download/path', [call()], (), ('download/path', )), (( 'a', 'b', 'c', ), 'default/path', None, 'download/path', [call()], (), ('download/path', )), (( 'a', 'b',